반응형

아파치 POI란? (https://ko.wikipedia.org/wiki/%EC%95%84%ED%8C%8C%EC%B9%98_POI)

아파치 POI(Apache POI)는 아파치 소프트웨어 재단에서 만든 라이브러리로서 마이크로소프트 오피스 파일 포맷을 순수 자바 언어로서 읽고 쓰는 기능을 제공한다. 주로 워드엑셀파워포인트와 파일을 지원하며 최근의 오피스 포맷인 Office Open XML File Formats [1] (OOXML, 즉 xml 기반의 *.docx, *.xlsx, *.pptx 등) 이나 아웃룩비지오퍼블리셔 등으로 지원 파일 포맷을 늘려가고 있다.


아파치 POI 다운로드 경로 : https://www.apache.org/dyn/closer.lua/poi/release/bin/poi-bin-3.15-20160924.tar.gz

Binary Distribution > poi-bin-3.15 버전 다운로드 

21 September 2016 - POI 3.15 available#

The Apache POI team is pleased to announce the release of 3.15. Featured are a handful of new areas of functionality and numerous bug fixes.

A summary of changes is available in the Release Notes. A full list of changes is available in the change log. People interested should also follow the dev list to track progress.

The POI source release as well as the pre-built binary deployment packages are listed below. Pre-built versions of all POI components are available in the central Maven repository under Group ID "org.apache.poi" and Version "3.15".



라이브러리 내용 : https://poi.apache.org/overview.html#components)

 파일명

 내용

 비고

 poi-3.15.jar

 

 

 poi-examples-3.15.jar

 

 

 poi-excelant-3.15.jar

 

 

 poi-ooxml-3.15.jar

 MS Office Open XML (https://en.wikipedia.org/wiki/Office_Open_XML)

 MS사의 XML 기반의 파일형식을 읽는데 필요한 lib

ooxml-lib\curvesapi-1.04.jar

ooxml-lib\xmlbeans-2.6.0.jar

commons-collections4-x.x.jar 

(https://commons.apache.org/proper/commons-collections/download_collections.cgi)

poi-ooxml-schemas-3.15.jar


위에 있는 lib 파일을 모두 import 해야 함..

디펜던시 ㅡㅡ 

 poi-ooxml-schemas-3.15.jar

 

 

 poi-scratchpad-3.15.jar

 

 


2003 Excel 버전은 XSSF(확장자가 xls)로 읽고

2007 Excel 버전 이후는 HSSF(확장자가 xlsx)를 통해서 파일을 읽고 쓸 수 있다. 


참고 예제 : 

https://www.mkyong.com/java/apache-poi-reading-and-writing-excel-file-in-java/

http://www.programcreek.com/java-api-examples/index.php?api=org.apache.poi.ss.usermodel.WorkbookFactory

파일 읽기

<기존 - 각각 읽어들이는 방법>

HSSFWorkbook wb = new HSSFWorkbook(new File("파일명.xls"));

XSSFWorkbook wb = new XSSFWorkbook(new File("파일명.xlsx"));


<WorkbookFactory를 통해선 구분없이 읽어들이는 방법> : https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/WorkbookFactory.html

Workbook wb = WorkbookFactory.create( File객체 );

Workbook wb = WorkbookFactory.create( File객체);


첫번째 Sheet 불러오기

Sheet sf = wb.getSheetAt(0); 


첫번째 Sheet의 rows수 가져오기

sf.getLastRowNum();


* 최종

 try {


            FileInputStream excelFile = new FileInputStream(new File(FILE_NAME));

            Workbook workbook = new XSSFWorkbook(excelFile);

            Sheet datatypeSheet = workbook.getSheetAt(0);

            Iterator<Row> iterator = datatypeSheet.iterator();


            while (iterator.hasNext()) {


                Row currentRow = iterator.next();

                Iterator<Cell> cellIterator = currentRow.iterator();


                while (cellIterator.hasNext()) {


                    Cell currentCell = cellIterator.next();

                    //getCellTypeEnum shown as deprecated for version 3.15

                    //getCellTypeEnum ill be renamed to getCellType starting from version 4.0

                    if (currentCell.getCellTypeEnum() == CellType.STRING) {

                        System.out.print(currentCell.getStringCellValue() + "--");

                    } else if (currentCell.getCellTypeEnum() == CellType.NUMERIC) {

                        System.out.print(currentCell.getNumericCellValue() + "--");

                    }


                }

                System.out.println();


            }

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }


외 참고사항

MultipartFile to File로 변경하는 방법 : http://stackoverflow.com/questions/24339990/how-to-convert-a-multipart-file-to-file

File convFile = new File(multipart.getOriginalFilename());

multipart.transferTo(convFile); 

return convFile;



반응형

+ Recent posts