프로그래밍/Java 공부

자바_POI활용, JXL활용 엑셀 문서로 저장하기

개발계발게발 2021. 6. 23. 17:14
반응형

자바_POI활용, JXL활용 엑셀 문서로 저장하기

 

https://poi.apache.org/

 

Apache POI - the Java API for Microsoft Documents

Apache POI - the Java API for Microsoft Documents Project News 20 January 2021 - POI 5.0.0 available The Apache POI team is pleased to announce the release of 5.0.0. This release features full JPMS support, updated ECMA-376 OOXML schemas, various rendering

poi.apache.org

poi-5.0.0.jar, poi-ooxml-5.0.0.jar, commons-math3-3.6.1.jar 파일 클래스패스 추가

 

자바_POI활용

자료를 excel 파일로 출력

import java.io.File;
import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;

public class Excel01 {
	public static void main(String[] args) {
		// Workbook 생성
		Workbook wb1 = new HSSFWorkbook();
        
		// Sheet
		Sheet sheet1 = wb1.createSheet();
        
		// Row, Cell
		Row row = null;
		Cell cell = null;
        
		row = sheet1.createRow(0);	//createRow 머릿글
		cell = row.createCell(0);
		cell.setCellValue("이름");
		cell = row.createCell(1);	
		cell.setCellValue("나이");
		cell = row.createCell(2);
		cell.setCellValue("사는곳");
		
		//데이터 입력
		row = sheet1.createRow(1);
		cell = row.createCell(0);
		cell.setCellValue("홍길동");
		cell = row.createCell(1);
		cell.setCellValue(132);
		cell = row.createCell(2);
		cell.setCellValue("서울");
		
		
		row = sheet1.createRow(3);
		cell = row.createCell(3);		
		cell.setCellValue("이름");
		cell = row.createCell(4);	
		cell.setCellValue("나이");
		cell = row.createCell(5);
		cell.setCellValue("사는곳");
		
		row = sheet1.createRow(5);
		cell = row.createCell(5);	
		cell.setCellValue("이름");
		cell = row.createCell(1);	
		cell.setCellValue("나이");
		cell = row.createCell(2);
		cell.setCellValue("사는곳");
		
		File xlsFile = new File("c:/temp/test.xls");

		try {
			FileOutputStream fos = new FileOutputStream(xlsFile);
			wb1.write(fos);
			System.out.println("출력완료");
		} catch (Exception e) {
			e.printStackTrace();
		}		
    }
}

 

test.xls 실행 결과

 

 

A Java library for reading/writing Excel - Browse Files at SourceForge.net

 

A Java library for reading/writing Excel - Browse Files at SourceForge.net

×

sourceforge.net

jxl.jar 파일 클래스패스 추가

 

자바_JXL활용

자료를 excel 파일로 출력

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

//jxl.jar와 연동
public class Excel02 {
	public static void main(String[] args) throws IOException, RowsExceededException, WriteException {
		
		WritableWorkbook wb = null;
		WritableSheet sheet = null;
		File file = new File("c:/temp/excle2.xls");
		Label label = null;
		
		HashMap<String, String> map01 = new HashMap<String, String>();
		map01.put("이름", "홍길동");
		map01.put("나이", "150");
		map01.put("사는곳", "한양");
		
		HashMap<String, String> map02 = new HashMap<String, String>();
		map02.put("이름", "홍홍홍");
		map02.put("나이", "172");
		map02.put("사는곳", "서울");
		
		ArrayList<HashMap<String, String>> list =
				new ArrayList<HashMap<String,String>>();
		
		list.add(map01);
		list.add(map02);
		//워크북 만들기
		wb = Workbook.createWorkbook(file);
		wb.createSheet("시트1", 0);
		sheet = wb.getSheet(0);
		//시트 만들기
		label = new Label(0, 0, "이름");
		sheet.addCell(label);
		
		label = new Label(1, 0, "나이");
		sheet.addCell(label);
		
		label = new Label(2, 0, "사는곳");
		sheet.addCell(label);
		//row만들기
		
		//cell 만들기		
		for (int i = 0; i < list.size(); i++) {
			HashMap<String, String> rs = list.get(i);
			label = new Label(0, (i + 1), rs.get("이름"));
			sheet.addCell(label);
			
			label = new Label(1, (i + 1), rs.get("나이"));
			sheet.addCell(label);
			
			label = new Label(2, (i + 1), rs.get("사는곳"));
			sheet.addCell(label);
		}
        
		wb.write();
		wb.close();
		System.out.println("저장완료");
	}

}

 

excel2.xls 실행 결과

 

 

반응형

'프로그래밍 > Java 공부' 카테고리의 다른 글

Java_DTO  (0) 2021.06.23
Java_PreparedStatement  (0) 2021.06.23
Java_IO(파일 입출력)  (0) 2021.06.22
Java_Inner Class 내부 클래스  (0) 2021.06.22
Java와 MariaDB 연동(INSERT, UPDATE, READ, DELETE) 예제  (0) 2021.06.17