Springboot를 통해 웹페이지 개발중 엑셀을 다루기 위해서
아파치의 poi 라이브러리를 사용하기로 했다.
1. POI 라이브러리 넣기(Maven 사용)
-위치: pom.xml
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
poi 라이브러리중 3.17버전을 사용했다.
자세한내용은( https://poi.apache.org/apidocs/index.html ) 를 참조하면 된다.
Apache POI - Javadocs
Apache POI - Javadocs Apache POI Javadocs The Javadocs for the latest (development) version of Apache POI can be accessed online here, or build from a source code checkout by running the javadocs Ant task. The latest (development) Javadocs are generally up
poi.apache.org
2. 간단 사용법은 https://poi.apache.org/text-extraction.html 에보면
간단하게는 행(row) 셀(cell) 시트(Sheet) 요렇게 3개만 알면됩니당
스타일관련된건 스타일 쪽 doc을 보고 개발 ㄱㄱ!
3. 읽기 팁
읽기 위해서 기본 설정되있는 cell type 의 Enum 을 사용합니다.
_NONE
@Internal (since ="POI 3.15 beta 3") public static final CellType _NONE Unknown type, used to represent a state prior to initialization or the lack of a concrete type. For internal use only.
NUMERIC
public static final CellType NUMERIC Numeric cell type (whole numbers, fractional numbers, dates)
STRING
public static final CellType STRING String (text) cell type
FORMULA
public static final CellType FORMULA Formula cell typeSee Also: FormulaType
BLANK
public static final CellType BLANK Blank cell type
BOOLEAN
public static final CellType BOOLEAN Boolean cell type
ERROR
public static final CellType ERROR Error cell typeSee Also: FormulaError
요렇게 되어있는 문서를 참조해보면
시트안에있는 로우의 특정 셀마다 값을 읽을때 마다 case형식으로 처리를 한다(보통..?)
FileInputStream fileData = new FileInputStream("불러올 파일 경로/이름.xlsx" );
HSSFWorkbook workbook = new HSSFWorkbook(fis);
HSSFSheet sheet = workbook.getSheetAt(0 ); //시트 수
int rows = sheet.getPhysicalNumberOfRows(); // 행의 수
를 통해서 반복문을 작성하고
행에서 시트를 읽을때
whitch(cell.getCellType()){
//숫자경우
case HSSHSSFCell .CELL_TYPE_NUMERIC :
value = cell.getNumericCellValue();
brea;
case HSSHSSFCell .CELL_TYPE_STRING :
value = cell.getStringCellValue();
brea;
.....생략
}
이런식으로 특정 셀의 형식에 맞게 값을 뽑아준다.
4. 쓰기 팁!
다음글로!