월요일, 9월 11, 2006

java execl에 내용 읽어오기.

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

Workbook workbook = Workbook.getWorkbook(new File("fileName"));
Sheet sheet = workbook.getSheet(0); //0번째 시트
for (int i=1; i < sheet.getRows() ; i++ )
{
totalCount++;
Cell[] cells = sheet.getRow(i);
}

* 참고 사이트
http://jxl.sourceforge.net/

Struts 1.1 File upload

다음은 동적 개수의 파일을 업로드 하는 예제이다 (** Struts 1.1 정식 부터는 위와 같은 배열로도 가능하다.!!)

1.입력 Page

<form action=""/upload.do" enctype="multipart/form-data" method="post">
<input name="file[0]" size="20" type="file">
<input name="file[1]" size="20" type="file">
<input value="OK" type="submit">


2.ActionForm private Vector vector = new Vector();

public FormFile getFile(int i) { return (FormFile)vector.elementAt(i); }
public void setFile(int i, FormFile f) { vector.add(i, f); }


3.Action
Class public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception {

ActionErrors errors = new ActionErrors();
ActionForward forward = new ActionForward(); // return value
InputForm inputForm = (InputForm) form;
try {
FormFile file1 = inputForm.getFile(0);
FormFile file2 = inputForm.getFile(1);
save(file1); save(file2); }
catch (Exception e) { e.printStackTrace(); }

forward = mapping.findForward("result");
return (forward); }

private void save(FormFile file) {
byte[] buffer = new byte[2048];
FileOutputStream fos = null;
System.out.println(file.getFileName());
try {
fos = new FileOutputStream(new File("d:\\" + file.getFileName()));
BufferedInputStream bis = new BufferedInputStream(file.getInputStream());
int size = 0;
while ((size = bis.read(buffer)) != -1)
fos.write(buffer, 0, size);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(fos != null)
try {
fos.close();
} catch(Exception e2)
{}
}
}


------
구글에서 찾은 내용입니다.

요약하면
1. Form에는 FormFile 형태로 선언해야함.
2. action에서 file.getInputStream()으로 하면 받은 파일내용을 읽을 수 있음.
3. file.getFileName() 파일 이름을 알 수 있음.

Struts 1.1 File upload

다음은 동적 개수의 파일을 업로드 하는 예제이다 (** Struts 1.1 정식 부터는 위와 같은 배열로도 가능하다.!!)

1.입력 Page

/upload.do" enctype="multipart/form-data" method="post">





2.ActionForm private Vector vector = new Vector();

public FormFile getFile(int i) { return (FormFile)vector.elementAt(i); }
public void setFile(int i, FormFile f) { vector.add(i, f); }


3.Action
Class public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception {

ActionErrors errors = new ActionErrors();
ActionForward forward = new ActionForward(); // return value
InputForm inputForm = (InputForm) form;
try {
FormFile file1 = inputForm.getFile(0);
FormFile file2 = inputForm.getFile(1);
save(file1); save(file2); }
catch (Exception e) { e.printStackTrace(); }

forward = mapping.findForward("result");
return (forward); }

private void save(FormFile file) {
byte[] buffer = new byte[2048];
FileOutputStream fos = null;
System.out.println(file.getFileName());
try {
fos = new FileOutputStream(new File("d:\\" + file.getFileName()));
BufferedInputStream bis = new BufferedInputStream(file.getInputStream());
int size = 0;
while ((size = bis.read(buffer)) != -1)
fos.write(buffer, 0, size);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(fos != null)
try {
fos.close();
} catch(Exception e2)
{}
}
}


------
구글에서 찾은 내용입니다.

요약하면
1. Form에는 FormFile 형태로 선언해야함.
2. action에서 file.getInputStream()으로 하면 받은 파일내용을 읽을 수 있음.
3. file.getFileName() 파일 이름을 알 수 있음.