【问题标题】:Unable to read date value as a string from excel sheet calculated by formula using Apache POI, What is missing?无法从使用 Apache POI 的公式计算的 Excel 表中读取日期值作为字符串,缺少什么?
【发布时间】:2017-07-26 11:31:02
【问题描述】:

所以我的 Java 代码是这样读取 excel 表单元格值的,但我无法将日期的值作为字符串获取,而是自动更正为数字。另请注意,所有这些 excel 值都是通过各自单元格中的公式计算的。缺少什么?

public class DatasheetReader {
public static void main(String args[]) throws Exception {
    String path = "D:\\Workspace\\Analytics\\TestDataSheets\\IRPTestData.xlsx";
    String sheet = "CompleteSimulationData";
    getCellData(path, sheet);
}

public static Object[][] getCellData(String datSheetPath, String sheetName)throws Exception {

    FileInputStream fis = new FileInputStream(new java.io.File(datSheetPath));
    XSSFWorkbook workbook = new XSSFWorkbook(fis);
    XSSFSheet sheet = workbook.getSheet(sheetName);

    int rowCount = sheet.getPhysicalNumberOfRows();
    int columnCount = sheet.getRow(0).getPhysicalNumberOfCells();

    // System.out.println(rowCount);
    // System.out.println(columnCount);

    Object[][] cellData = new Object[rowCount][columnCount];
    Object[][] dataProviderArray = new Object[rowCount - 1][columnCount];

    for (int i = 0; i < rowCount; i++) {
        System.out.println(" ");
        for (int j = 0; j < columnCount; j++) {
            // IF - for blanks in excel
            if (i != 0) {
                // IF - for not null values
                if (sheet.getRow(i).getCell(j) != null) {
                    Cell cell = sheet.getRow(i).getCell(j);
                    int cellType = cell.getCachedFormulaResultType();
                    if(cellType==Cell.CELL_TYPE_NUMERIC){
                         System.out.println(cell.getNumericCellValue()+" "+cell.getCachedFormulaResultType());
                    }else if(cellType==Cell.CELL_TYPE_STRING){
                         System.out.println(cell.getRichStringCellValue()+" "+cell.getCachedFormulaResultType());
                    }
                } else {
                    cellData[i][j] = "";
                }

            } else {
                continue;
            }
            //System.out.println(cellData[i][j]);
        }
    }

    for (int i = 1; i < rowCount; i++) {
        for (int j = 0; j < columnCount; j++) {
            //System.out.print(cellData[i][j] + " ");
            dataProviderArray[i - 1][j] = cellData[i][j];
        }
    }
    workbook.close();
    return dataProviderArray;
}}

所有这些值都是根据公式计算的缓存值。
除日期外,所有值都按预期获得,它来自42887.0 而不是Jun-2017 基本上所有值都不是excel单元格中的单独值,而是从公式计算的值,因为日期POI返回其类型为CELL_TYPE_NUMERIC ,不明白如何处理这个问题,因为无法使用 POI 的DataFormat,因为我要读取的所有值都作为公式?

【问题讨论】:

标签: java excel-formula apache-poi


【解决方案1】:

以下代码应该能够读取任何类型的Excel 单元格内容。我希望我没有忘记什么。

Busy Developers' Guide: Getting the cell contents的改动主要有:

使用具有FormulaEvaluatorDataFormatter

如果cell.getCellTypeEnum()CellType.FORMULA,则再次检查cell.getCachedFormulaResultTypeEnum() 并根据情况做出反应。

使用最新的稳定版本Apache POI 3.16 工作。不使用较低版本的作品。

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
import org.apache.poi.ss.usermodel.CellType.*;

import java.io.FileInputStream;

class ReadExcelExample {

 public static void main(String[] args) throws Exception {

  Workbook wb  = WorkbookFactory.create(new FileInputStream("ExcelExample.xlsx"));

  DataFormatter formatter = new DataFormatter();
  FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();

  Sheet sheet = wb.getSheetAt(0);
  for (Row row : sheet) {
   for (Cell cell : row) {
    CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());
    System.out.print(cellRef.formatAsString());
    System.out.print(" - ");

    // get the text that appears in the cell by getting the cell value and applying any data formats (Date, 0.00, 1.23e9, $1.23, etc)
    String text = "";
    try {
     text = formatter.formatCellValue(cell, evaluator);
    } catch (org.apache.poi.ss.formula.eval.NotImplementedException ex) {
     text = "Formula not implemented";
    }
    System.out.println(text);

    // Alternatively, get the value and format it yourself
    switch (cell.getCellTypeEnum()) {
     case STRING:
      System.out.println(cell.getRichStringCellValue().getString());
      break;
     case NUMERIC:
      if (DateUtil.isCellDateFormatted(cell)) {
       System.out.println(cell.getDateCellValue());
      } else {
       System.out.println(cell.getNumericCellValue());
      }
      break;
     case BOOLEAN:
      System.out.println(cell.getBooleanCellValue());
      break;
     case FORMULA:
      System.out.println(cell.getCellFormula());
      switch (cell.getCachedFormulaResultTypeEnum()) {
       case STRING:
        System.out.println(cell.getRichStringCellValue().getString());
        break;
       case NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
         System.out.println(cell.getDateCellValue());
        } else {
         System.out.println(cell.getNumericCellValue());
        }
        break;
       case BOOLEAN:
        System.out.println(cell.getBooleanCellValue());
        break;
       case ERROR:
       System.out.println(cell.getErrorCellValue());
        break;
       default:
        System.out.println("default formula cell"); //should never occur
      }
      break;
     case ERROR:
      System.out.println(cell.getErrorCellValue());
      break;
     case BLANK:
      System.out.println("blank");
      break;
     default:
      System.out.println("default cell"); //should never occur
    }
   }
  }

  wb.close();

 }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多