本文整理匯總了Java中org.apache.poi.ss.usermodel.FormulaEvaluator類的典型用法代碼示例。如果您正苦於以下問題:Java FormulaEvaluator類的具體用法?Java FormulaEvaluator怎麽用?Java FormulaEvaluator使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
FormulaEvaluator類屬於org.apache.poi.ss.usermodel包,在下文中一共展示了FormulaEvaluator類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getValueOrEmptyAsObject
import org.apache.poi.ss.usermodel.FormulaEvaluator; //導入依賴的package包/類
public static Object getValueOrEmptyAsObject(FormulaEvaluator evaluator, Cell cell) {
final CellValue cellValue = evaluator.evaluate(cell);
if (cellValue == null) {
return "";
}
switch (cellValue.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
return Boolean.valueOf(cellValue.getBooleanValue());
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
return cell.getDateCellValue();
}
BigDecimal bd = BigDecimal.valueOf(cell.getNumericCellValue()).setScale(DECIMAL_PRECISION, BigDecimal.ROUND_HALF_UP);
return bd.stripTrailingZeros();
case Cell.CELL_TYPE_STRING:
return cellValue.getStringValue();
case Cell.CELL_TYPE_ERROR:
return "ERRO";
default:
return "";
}
}
示例2: parseCurrentSheet
import org.apache.poi.ss.usermodel.FormulaEvaluator; //導入依賴的package包/類
private List<T> parseCurrentSheet() throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {
List<T> list = new ArrayList<>();
workbook = getWorkbook();
Sheet sheet = workbook.getSheetAt(this.sheetNumber);
final FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
Map<Method, TableCellMapping> cellMappingByMethod = ImporterUtils.getMappedMethods(clazz, group);
final Constructor<T> constructor = clazz.getDeclaredConstructor();
constructor.setAccessible(true);
int i = 0;
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
i++;
if (i <= headersRows) {
continue;
}
T instance = constructor.newInstance();
for (Entry<Method, TableCellMapping> methodTcm : cellMappingByMethod.entrySet()) {
TableCellMapping tcm = methodTcm.getValue();
ImporterUtils.parseSpreadsheetCell(tcm.converter(), evaluator, row.getCell(tcm.columnIndex()), methodTcm.getKey(), instance, exporterFormatter, useFormatterToParseValueAsString);
}
list.add(instance);
}
return list;
}
示例3: parseCurrentSheetAsStringList
import org.apache.poi.ss.usermodel.FormulaEvaluator; //導入依賴的package包/類
private List<List<String>> parseCurrentSheetAsStringList() {
workbook = getWorkbook();
Sheet sheet = workbook.getSheetAt(getSheetNumber());
final FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
Iterator<Row> rowIterator = sheet.iterator();
List<List<String>> list = new ArrayList<>();
int i = 0;
while (rowIterator.hasNext()) {
List<String> rowList = new ArrayList<>();
Row row = rowIterator.next();
i++;
if (i <= headersRows) {
continue;
}
Iterator<Cell> cells = row.cellIterator();
while (cells.hasNext()) {
Cell cell = cells.next();
rowList.add(ImporterUtils.getValueOrEmpty(evaluator, cell, exporterFormatter));
}
list.add(rowList);
}
return list;
}
示例4: evaluateFormulas
import org.apache.poi.ss.usermodel.FormulaEvaluator; //導入依賴的package包/類
private void evaluateFormulas(Sheet sheet) {
FormulaEvaluator evaluator = sheet.getWorkbook().getCreationHelper()
.createFormulaEvaluator();
Row row = null;
Cell cell = null;
for (int i = 0; i < 100; i++) {
row = sheet.getRow(i);
if (row == null)
row = sheet.createRow(i);
for (int j = 0; j < 50; j++) {
cell = row.getCell(j);
if (cell == null)
cell = row.createCell(j);
evaluator.evaluateFormulaCell(cell);
}
}
}
示例5: evaluateAllCell
import org.apache.poi.ss.usermodel.FormulaEvaluator; //導入依賴的package包/類
public static void evaluateAllCell(FormulaEvaluator evaluator, Sheet sheet) {
if ((evaluator == null) || (sheet == null)) {
return;
}
for (int r = 0; r <= sheet.getLastRowNum(); r++) {
Row row = sheet.getRow(r);
if (row == null) {
continue;
}
for (int c = 0; c <= row.getLastCellNum(); c++) {
Cell cell = row.getCell(c);
if (cell == null) {
continue;
}
switch (cell.getCellType()) {
case CELL_TYPE_FORMULA: {
evaluator.evaluateInCell(cell);
}
}
}
}
}
示例6: openReferencedWorkbooks
import org.apache.poi.ss.usermodel.FormulaEvaluator; //導入依賴的package包/類
private void openReferencedWorkbooks(Integer maxWorkbookSize, Workbook workbook, FormulaEvaluator evaluator) {
Map<String,FormulaEvaluator> workbooks = new HashMap<String, FormulaEvaluator>();
if(workbook instanceof XSSFWorkbook) {
if(((XSSFWorkbook)workbook).getExternalLinksTable()!=null) {
for (ExternalLinksTable sheet : ((XSSFWorkbook)workbook).getExternalLinksTable()) {
String file = sheet.getLinkedFileName();
File f = resolver.resolve(file);
if(f!=null) {
try {
WorkbookFile book = openWorkbook(f, maxWorkbookSize, false, false);
workbooks.put(file, book.getWorkbook().getCreationHelper().createFormulaEvaluator());
} catch(Exception e) {
logger.error("An error occured while opening referenced workbook '"+file+"'. Main workbook: '"+mainWorkbookFile+"'");
}
} else {
logger.warn("Unable to resolve external workbook '"+file+"'. Main workbook: '"+mainWorkbookFile+"'");
}
}
}
}
workbooks.put("this", evaluator);
evaluator.setupReferencedWorkbooks(workbooks);
}
示例7: getFormattedText
import org.apache.poi.ss.usermodel.FormulaEvaluator; //導入依賴的package包/類
/**
* Format the cell content as a String, with number and date format applied.
* @return cell content with format applied
*/
private String getFormattedText(Locale locale) {
// is there a special date format?
if (getResultType()==CellType.DATE) {
DateTimeFormatter df = getCellStyle().getLocaleAwareDateFormat(locale);
if (df != null) {
return df.format(getDateTime());
}
}
// if not, let POI do the formatting
FormulaEvaluator evaluator = getWorkbook().evaluator;
DataFormatter dataFormatter = getWorkbook().getDataFormatter(locale);
try {
return dataFormatter.formatCellValue(poiCell, evaluator);
} catch (Exception ex) {
return Cell.ERROR_TEXT;
}
}
示例8: getValue
import org.apache.poi.ss.usermodel.FormulaEvaluator; //導入依賴的package包/類
private Object getValue( Cell cell, FormulaEvaluator evaluator ) {
switch( cell.getCellType() ) {
case Cell.CELL_TYPE_FORMULA :
if( StringUtil.isNotEmpty( cell ) ) {
switch( evaluator.evaluateFormulaCell(cell) ) {
case Cell.CELL_TYPE_NUMERIC :
return getNumericCellValue( cell );
case Cell.CELL_TYPE_BOOLEAN :
return cell.getBooleanCellValue();
case Cell.CELL_TYPE_STRING :
return cell.getStringCellValue();
}
}
return cell.getStringCellValue();
case Cell.CELL_TYPE_NUMERIC :
return getNumericCellValue( cell );
case Cell.CELL_TYPE_BOOLEAN :
return cell.getBooleanCellValue();
default :
return cell.getStringCellValue();
}
}
示例9: testRead_FORMULA_NUMERIC
import org.apache.poi.ss.usermodel.FormulaEvaluator; //導入依賴的package包/類
@Test
public void testRead_FORMULA_NUMERIC() throws IOException {
try (Workbook workbook = new XSSFWorkbook()) {
// 準備
Sheet sheet = workbook.createSheet();
Row row0 = sheet.createRow(0);
row0.createCell(0).setCellFormula("1200+34");
row0.createCell(1).setCellFormula("1200+34.56");
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
evaluator.evaluateAll();
// 実行&検証
try (ExcelReader reader = new ExcelReader(workbook)) {
String[] r0 = reader.read();
assertNotNull(r0);
assertEquals(2, r0.length);
assertEquals("1234", r0[0]);
assertEquals("1234.56", r0[1]);
assertNull(reader.read());
}
}
}
示例10: testRead_FORMULA_STRING
import org.apache.poi.ss.usermodel.FormulaEvaluator; //導入依賴的package包/類
@Test
public void testRead_FORMULA_STRING() throws IOException {
try (Workbook workbook = new XSSFWorkbook()) {
// 準備
Sheet sheet = workbook.createSheet();
Row row0 = sheet.createRow(0);
row0.createCell(0).setCellFormula("\"CELL\"&\"00\"");
row0.createCell(1).setCellFormula("\"CELL\"&\"01\"");
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
evaluator.evaluateAll();
// 実行&検証
try (ExcelReader reader = new ExcelReader(workbook)) {
String[] r0 = reader.read();
assertNotNull(r0);
assertEquals(2, r0.length);
assertEquals("CELL00", r0[0]);
assertEquals("CELL01", r0[1]);
assertNull(reader.read());
}
}
}
示例11: testRead_FORMULA_BOOLEAN
import org.apache.poi.ss.usermodel.FormulaEvaluator; //導入依賴的package包/類
@Test
public void testRead_FORMULA_BOOLEAN() throws IOException {
try (Workbook workbook = new XSSFWorkbook()) {
// 準備
Sheet sheet = workbook.createSheet();
Row row0 = sheet.createRow(0);
row0.createCell(0).setCellFormula("1=1");
row0.createCell(1).setCellFormula("1=0");
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
evaluator.evaluateAll();
// 実行&検証
try (ExcelReader reader = new ExcelReader(workbook)) {
String[] r0 = reader.read();
assertNotNull(r0);
assertEquals(2, r0.length);
assertEquals("true", r0[0]);
assertEquals("false", r0[1]);
assertNull(reader.read());
}
}
}
示例12: testRead_FORMULA_ERROR
import org.apache.poi.ss.usermodel.FormulaEvaluator; //導入依賴的package包/類
@Test
public void testRead_FORMULA_ERROR() throws IOException {
try (Workbook workbook = new XSSFWorkbook()) {
// 準備
Sheet sheet = workbook.createSheet();
Row row0 = sheet.createRow(0);
row0.createCell(0).setCellErrorValue((byte) 0);
row0.createCell(1).setCellFormula("A1");
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
evaluator.evaluateAll();
// 実行&検証
try (ExcelReader reader = new ExcelReader(workbook)) {
String[] r0 = reader.read();
assertNotNull(r0);
assertEquals(2, r0.length);
assertNull(r0[0]);
assertNull(r0[1]);
assertNull(reader.read());
}
}
}
示例13: doFormats
import org.apache.poi.ss.usermodel.FormulaEvaluator; //導入依賴的package包/類
private void doFormats(XSSFDataValidationHelper dvHelper, XSSFSheet sheetTracing, int rowIndex, FormulaEvaluator evaluator) {
insertCondition(dvHelper, sheetTracing, rowIndex, 3, "1", "31");
insertCondition(dvHelper, sheetTracing, rowIndex, 4, "1", "12");
insertCondition(dvHelper, sheetTracing, rowIndex, 5, "1900", "3000");
insertCondition(dvHelper, sheetTracing, rowIndex, 6, "1", "31");
insertCondition(dvHelper, sheetTracing, rowIndex, 7, "1", "12");
insertCondition(dvHelper, sheetTracing, rowIndex, 8, "1900", "3000");
insertDecCondition(dvHelper, sheetTracing, rowIndex, 9);
insertDropBox(dvHelper, sheetTracing, rowIndex, 10, "=Units");
insertDropBox(dvHelper, sheetTracing, rowIndex, 11, "=StationIDs");
//XSSFRow row = sheetTracing.getRow(rowIndex);
//XSSFCell cell = row.getCell(12);
//cell.setCellFormula("INDEX(Companies,MATCH(L" + (row.getRowNum() + 1) + ",StationIDs,0),1)");
//evaluator.evaluateFormulaCell(cell);
insertDropBox(dvHelper, sheetTracing, rowIndex, 0, "=LotNumbers");
}
示例14: formatCellValue
import org.apache.poi.ss.usermodel.FormulaEvaluator; //導入依賴的package包/類
public FormattedValue formatCellValue(Cell cell, FormulaEvaluator evaluator) {
FormattedValue ret = doFormatCellValue(cell, evaluator);
if (cell != null && cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
ret.setFormula(cell.getCellFormula());
}
if (DEBUG && cell != null && cell.getCellStyle() != null) {
int cellType = cell.getCellType();
if (cellType == Cell.CELL_TYPE_FORMULA) {
cellType = cell.getCachedFormulaResultType();
}
if (cellType == Cell.CELL_TYPE_NUMERIC) {
System.out.println("Format: " +
cell.getCellStyle().getDataFormat() + ": " +
cell.getCellStyle().getDataFormatString() + " --- " +
ExcelUtils.isCellDateFormatted(cell) + ":" +
DateUtil.isCellDateFormatted(cell) + ":" +
DateUtil.isInternalDateFormat(cell.getCellStyle().getDataFormat()) + " --- " +
ret);
}
}
return ret;
}
示例15: parse
import org.apache.poi.ss.usermodel.FormulaEvaluator; //導入依賴的package包/類
@Override
protected List<Object> parse(From fixture) {
InputStream inputStream = fixture.getInputStream();
Workbook workbook = createWorkbook(inputStream);
DataFormatter dataFormatter = new DataFormatter();
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
List<Object> mappings = new LinkedList<Object>();
Helpers helpers = new Helpers();
helpers.dataFormatter = dataFormatter;
helpers.evaluator = evaluator;
Parameters parameters = new Parameters();
parameters.mode = fixture.getMode();
for (int sheetIndex = 0; sheetIndex < workbook.getNumberOfSheets(); sheetIndex++) {
Sheet sheet = workbook.getSheetAt(sheetIndex);
mappings.addAll(parseSheet(sheet, helpers, parameters));
}
return mappings;
}