민서네집

poi 라이브러리로 Excel File Spread Sheet Cell 읽기 본문

Java

poi 라이브러리로 Excel File Spread Sheet Cell 읽기

브라이언7 2015. 3. 31. 16:27

식(formula)으로 된 엑셀 파일 안의 cell 값을 읽는 방법


그냥 cell.getStringCellValue()  이런 식으로 호출하면 Cannot get a text value from a numeric formula cell 에러가 난다.


cellValue = cell.getCellFormula(); 

이렇게 하면 "F2/D2" 이런 formula 문자열을 얻는다.


식(formula)를 계산한 값을 얻기 위해서는 다음과 같이 해야 한다.


import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.CellValue;

import org.apache.poi.ss.usermodel.FormulaEvaluator;

import org.apache.poi.ss.usermodel.Workbook;


FormulaEvaluator formulaEval = workbook.getCreationHelper().createFormulaEvaluator();

String cellValue = "";

if( cell != null ) {

CellValue evaluate = formulaEval.evaluate(cell);

if( evaluate != null )

cellValue = evaluate.formatAsString();

}


[출처] http://stackoverflow.com/questions/4183708/how-to-get-the-formula-cell-valuedata-using-apache-poi


[참고] http://stackoverflow.com/questions/6508203/when-getting-cell-content-using-apache-poi-library-i-get-both-cannot-get-a-num


Comments