본문 바로가기

Programming/JAVA

java에서 Excel Read/Write

http://www.andykhan.com/jexcelapi/download.html 에 가셔서 jexcelapi_2_4_5.tar.gz 파일을 다운로드 하시고 압축파일안에 보시면 jxl.jar파일이 있습니다. 이걸 C:\j2sdk1.4.1_01\jre\lib\ext 에 복사해서 넣으시구요.. 아래 코드를 컴파일해서 실행하면 생성이 될껍니다.

 

http://www.andykhan.com/jexcelapi/tutorial.html로 가시면 tutorial이 있으니 이걸 보시고 일기, 쓰기 다 쉽게 하실수 있을껍니다. 참고하세요..

 

/**************************************************/

import java.io.*;
import java.util.*;
import jxl.*;
import jxl.write.*;
import jxl.format.*;

public class ExcelTest2
{

public ExcelTest2()
{
super();
}
public static void main(String[] argv) throws FileNotFoundException, IOException,WriteException
{
WritableWorkbook workbook = Workbook.createWorkbook(new File("c:/myExcelFile.xls")); // 엑셀 파일 저장 루트
WritableSheet sheet = workbook.createSheet("Sheet1", 0);

jxl.write.WritableCellFormat format= new WritableCellFormat();
jxl.write.WritableCellFormat format0= new WritableCellFormat();

format.setBackground(jxl.format.Colour.GRAY_25 );
format.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN );
format.setAlignment(jxl.format.Alignment.CENTRE);

format0.setBackground(jxl.format.Colour.WHITE );
format0.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN );
format0.setAlignment(jxl.format.Alignment.CENTRE);
sheet.setColumnView(0,8);

jxl.write.Label label =null;
jxl.write.Blank blank=null;

label = new jxl.write.Label(0,0,"결재란",format);
sheet.addCell(label);
label = new jxl.write.Label(1,0,"테스트",format);
sheet.addCell(label);
label = new jxl.write.Label(2,0,"결재일",format);
sheet.addCell(label);
label = new jxl.write.Label(3,0,"테스트",format);
sheet.addCell(label);
blank = new jxl.write.Blank(4,0,format0);
sheet.addCell(blank );

for(int h=1;h<51;h++)
{
for(int i=0;i<10;i++)
{
label = new jxl.write.Label(i, h, "("+i+","+h+")",format0);
sheet.addCell(label);
}
}
workbook.write();
workbook.close();
}
}

/******************************************************************/