Hi~ Summers!!

Excel 연동 하기 본문

개발/JAVA

Excel 연동 하기

eNaNII 2006. 12. 8. 11:21

우선 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이 있으니 이걸 보시고 일기, 쓰기 다 쉽게 하실수 있을껍니다. 참고하세요..

자바로 엑셀을 핸들링 할 수 있는 방법은 크게 두가지로 나누어 진다.
1. Java Excel API
참조 :
http://www.andykhan.com/jexcelapi/
2. POI
참조 :
http://jakarta.apache.org/poi/index.html



흔히 POI를 엑셀을 핸들링 하기 위한 것으로만 오해하기 쉬운데,
POI 프로젝트는 마이크로소프트 OLE 2 복합도큐먼트포맷형식의 파일을 순수 자바를 이용하여 핸들링하는 APIs로 구성되어있다.
OLE 2 복합도큐먼트포맷형식의 파일은 마이크로소프트 엑셀 혹은 워드파일 등의 대부분의 오피스파일들을 나타낸다.



일반적으로 엑셀에 대한 핸들링만을 수행할 때에는 Jxl을 권장한다.
엑셀을 핸들링 할 때 엑셀에서 가장 작은 단위는 알고 있듯이 "셀"이다.
모든 작업은 이 셀을 중심으로 이루어진다.



주의할 점)
1) 엑셀 쉬트상에 "C15"라는 셀에 데이터가 있다고 가정하자.( 15행 C열을 나타낸다.)
이 때 Jxl에서는 A1( 1행 A열)부터 C15까지는 실제 데이터가 없을 경우에라도 null이 아닌 빈데이터가 있다고 인식한다.
즉 D열 이상이나, 16행 이상을 접근 할 때에 null로 인식한다.


하지만 POI에서는 C15 이내에 있다 하더라도 실제 데이터가 없을 때에는 null로 인식한다.


2) Jxl에서는 각 셀의 데이터 타입을 실제 엑셀 파일에서 지정된 셀의 타입을 따르고,
POI에서는 셀의 실제 데이터 형을 따른다.
예를 들어 특정 셀의 타입을 text 로 잡아놓고, 데이터를 1234로 입력하면
Jxl에서는 "12345"로 인식하고, POI에서는 12345.00 이런식으로 인식한다.



EX ) Java Excel API를 이용한 Excel 읽기


import jxl.*;


// .. 중간생략


Workbook workbook = null;
Sheet sheet = null;
Cell cell = null;


try
{
//엑셀파일을 인식
workbook = Workbook.getWorkbook( new File( szFileName));


//엑셀파일에 포함된 sheet의 배열을 리턴한다.
//workbook.getSheets();


if( workbook != null)
{
//엑셀파일에서 첫번째 Sheet를 인식
sheet = workbook.getSheet(0);


if( sheet != null)
{
//셀인식 Cell a1 = sheet.getCell( 컬럼 Index, 열 Index);
//셀 내용 String stringa1 = a1.getContents();


//기록물철의 경우 실제 데이터가 시작되는 Row지정
int nRowStartIndex = 5;
//기록물철의 경우 실제 데이터가 끝 Row지정
int nRowEndIndex = sheet.getColumn( 2).length - 1;


//기록물철의 경우 실제 데이터가 시작되는 Column지정
int nColumnStartIndex = 2;
//기록물철의 경우 실제 데이터가 끝나는 Column지정
int nColumnEndIndex = sheet.getRow( 2).length - 1;


String szValue = "";


for( int nRow = nRowStartIndex; nRow <= nRowEndIndex; nRow++ )
{
for( int nColumn = nColumnStartIndex; nColumn <= nColumnEndIndex ; nColumn++)
{
szValue = sheet.getCell( nColumn, nRow).getContents();


System.out.print( szValue);
System.out.print( "\t" );
}
System.out.println();
}
}
else
{
System.out.println( "Sheet is null!!" );
}
}
else
{
System.out.println( "WorkBook is null!!" );
}
}
catch( Exception e)
{
e.printStackTrace();
}
finally
{
if( workbook != null)
{
workbook.close();
}
}


EX ) POI를 이용한 Excel 파일 읽기


import org.apache.poi.hssf.usermodel.*;


// .. 중간 생략


HSSFWorkbook workbook = null;
HSSFSheetsheet = null;
HSSFRow row = null;
HSSFCell cell = null;


try
{
workbook = new HSSFWorkbook( new FileInputStream( new File( szFileName)));


if( workbook != null)
{
sheet = workbook.getSheetAt( 0);


if( sheet != null)
{


//기록물철의 경우 실제 데이터가 시작되는 Row지정
int nRowStartIndex = 5;
//기록물철의 경우 실제 데이터가 끝 Row지정
int nRowEndIndex = sheet.getLastRowNum();


//기록물철의 경우 실제 데이터가 시작되는 Column지정
int nColumnStartIndex = 2;
//기록물철의 경우 실제 데이터가 끝나는 Column지정
int nColumnEndIndex = sheet.getRow( 2).getLastCellNum();


String szValue = "";


for( int i = nRowStartIndex; i <= nRowEndIndex ; i++)
{
row = sheet.getRow( i);


for( int nColumn = nColumnStartIndex; nColumn <= nColumnEndIndex ; nColumn++)
{
cell = row.getCell(( short ) nColumn);

if( cell == null)
{
continue;
}
if( cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)
{
szValue = String.valueOf( cell.getNumericCellValue());
}
else
{
szValue = cell.getStringCellValue();
}


System.out.print( szValue);
System.out.print( "\t" );
}
System.out.println();
}
}
else
{
System.out.println( "Sheet is null!!" );
}
}
else
{
System.out.println( "WorkBook is null!!" );
}


}catch(Exception e){
e.printStackTrace();
}



EX ) Jxl을 이용하여 Excel에 데이터 저장하기


import jxl.*;


// .. 중간생략


WritableWorkbook workbook = null;
WritableSheetsheet = null;


File excelFile = new File( szFileName);
Label label = null;


long start = 0;
long end = 0;

try
{
for(int i = 0 ; i < 10; i++)
{
workbook = Workbook.createWorkbook( excelFile);
workbook.createSheet("sheet1", 0);
sheet = workbook.getSheet(0);
for( int j = 0; j < 10000; j++){
label = new Label( j, 0, "test cell");
sheet.addCell( label);
}


kidsbook.write();
kidsbook.close();
}
}
catch( Exception e)
{
}


// .. 중간생략



EX ) POI를 이용한 브라우저에서 Excel에 데이터 저장하여 보여주기


import org.apache.poi.hssf.usermodel.*;


// ... 생략


public void writeStream( PTSEvaluation[] arrPTSEvaluation) throws Exception
{
try
{


HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet( "new sheet");
HSSFRow row = null;
HSSFCell cell = null;
HSSFCellStyle style = null;


ServletOutputStream excelOut = ServiceContext.getResponse().getOutputStream();
ServiceContext.getResponse().setHeader( "Content-Disposition", "attachment;filename=EvaluationCompensationList.xls");
ServiceContext.getResponse().setContentType( MimeType.getMimeType( "xls"));


//로우 생성
row = sheet.createRow( ( short)0);
row.setHeightInPoints( 30);


//셀에 적용한 스타일을 생성한다.
style = PTSUtil.setExcelHeaderStyle( wb);


// 셀 생성
cell = row.createCell( (short)0);


//한글 처리
cell.setEncoding( HSSFCell.ENCODING_UTF_16);


//셀에 데이터 입력하기
cell.setCellValue( "값");


//셀에 스타일 적용하기
cell.setCellStyle(style);


//.. 중간생략 ( 이런 방식으로 로우와 셀을 증가 시키면서 작업을 수행하면 된다.


wb.write( excelOut);
excelOut.flush();
}
catch( Exception e)
{
e.printStackTrace();
throw e;
}
finally
{
ServiceContext.getResponse().getOutputStream().close();
}
}


// ... 생략

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

아래 포스트에서 jakarta POI에 대해 간단히 소개했었다.

이제는 HSSF를 이용해 자바에서 간단한 엑셀파일을 생성하는 방법을 살펴보도록 하겠다.

HSSF를 사용하기 위해서는 먼저 POI라이브러리를 다운받아야 하는데 아래의 주소에가서

라이브러리를 다운받은 후 클래스 패스를 잡아주면 된다.

http://jakarta.apache.org/site/downloads/downloads_poi.cgi

아래의 소스는 간단한 엑셀파일 생성에 관한 소스이다.

---------------------------------------------------------------------------------------

import java.io.*;
import org.apache.poi.hssf.usermodel.*;

public class NumberForm {

public static void main(String[] args) throws IOException, InterruptedException {

// 먼저 파일이 되는 workbook부분을 생성한다.
HSSFWorkbook workbook = new HSSFWorkbook();

// workbook부분에 sheet를 새로 생성한다.

HSSFSheet sheet = workbook.createSheet("new");
HSSFRow row = sheet.createRow((short)0); // sheet부분에 행을 새로 생성한다.
HSSFCell cell = row.createCell((short)0); //새로생성된 행 부분에 셀을 추가한다.
cell.setCellValue(1); // 셀 값을 집어넣는다(숫자형)

row.createCell((short)1).setCellValue(1.2);
row.createCell((short)2).setCellValue("string"); // 셀 값을 집어넣는다(문자형)
row.createCell((short)3).setCellValue(true); // 셀 값을 집어넣는다(boolean형)

FileOutputStream outFile = new FileOutputStream("newexcelfile.xls");
workbook.write(outFile);
outFile.close();
}
}

---------------------------------------------------------------------------------------
새로이 Excel파일을 만드는 방법은 기본적으로 다음과 같다.

1. WorkBook를 만든다(즉, 파일이 될 부분을 만든다)

2. WorkBook안에 sheet를 만든다

3. sheet안에 행을 만든다

4. 행 안에 기본단위인 셀을 만든다

** 이때 하늘색 바탕으로 표시된 부분이 인텍스 값이 되며, 인덱스는 0부터 시작한다.

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

이번 예제에서 사용한 POI는 Jakarta의 프로젝트로 Microsoft Format Files에 대하여 자바에서 접근하기 위한 라이브러리를 제공 합니다. (엑셀, 워드파일에 대한 read, write)

관련 자료는
http://jakarta.apache.org/poi/index.html에서 확인 하시어 공부 하시기 바라며 아래 예제처럼 Excel 파일을 만들기 위해서는 http://www.apache.org/dist/jakarta/poi/release/bin 에서 zip 파일을 다운받아 jar 파일을 /WEB-INF/lib 폴더에 위치 시켜야 합니다.

아래의 예제를 참고 바랍니다.

==============================
JSP에서 Excel 파일 Write 하기
==============================
<%@ page pageEncoding="euc-kr" %>
<%@ page import="java.io.*, org.apache.poi.hssf.usermodel.*" %>

<%

try {
//Excel Workbook을 하나 만듭니다. 이건 하나의 엑셀 파일에 해당
HSSFWorkbook workbook = new HSSFWorkbook();

//Shett를 만듭니다.
HSSFSheet sheet = workbook.createSheet("first sheet");

//sheet에 행을 하나 만듭니다.
HSSFRow row = sheet.createRow((short)0);

//행에 셀을 3개 만든 후 값 대입
HSSFCell cell1 = row.createCell((short)0);
cell1.setCellValue("number1");

HSSFCell cell2 = row.createCell((short)1);
cell2.setCellValue("number2");

HSSFCell cell3 = row.createCell((short)2);
cell3.setCellValue("number3");

//엑셀 파일을 만듬
FileOutputStream fileOutput = new FileOutputStream("d:/myFirstWorkBook.xls");

workbook.write(fileOutput);
fileOutput.close();

out.println("Excel File 생성 OK");
}
catch(Exception e) {
out.println(e);
}
%>

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Comments