Java 无法从使用Apache POI的公式计算的excel工作表中读取日期值作为字符串,缺少什么?
因此,我的Java代码是这样读取excel表格单元格值的,但我无法以字符串形式获取日期值,而是将其自动更正为数字。还要注意,excel的所有这些值都是通过公式在各自的单元格中计算的。少了什么Java 无法从使用Apache POI的公式计算的excel工作表中读取日期值作为字符串,缺少什么?,java,excel-formula,apache-poi,Java,Excel Formula,Apache Poi,因此,我的Java代码是这样读取excel表格单元格值的,但我无法以字符串形式获取日期值,而是将其自动更正为数字。还要注意,excel的所有这些值都是通过公式在各自的单元格中计算的。少了什么 public class DatasheetReader { public static void main(String args[]) throws Exception { String path = "D:\\Workspace\\Analytics\\TestDataSheets\\IRPT
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;
}}
公共类数据表{
公共静态void main(字符串args[])引发异常{
String path=“D:\\Workspace\\Analytics\\TestDataSheets\\IRPTestData.xlsx”;
字符串表=“CompleteSimulationData”;
getCellData(路径、表格);
}
公共静态对象[][]getCellData(字符串datSheetPath,字符串sheetName)引发异常{
FileInputStream fis=新的FileInputStream(新的java.io.File(datSheetPath));
XSSF工作簿=新XSSF工作簿(fis);
XSSFSheet sheet=workbook.getSheet(sheetName);
int rowCount=sheet.getPhysicalNumberOfRows();
int columnCount=sheet.getRow(0.getPhysicalNumberOfCells();
//System.out.println(行计数);
//System.out.println(columnCount);
Object[][]cellData=新对象[rowCount][columnCount];
Object[][]dataProviderArray=新对象[rowCount-1][columnCount];
对于(int i=0;i
所有这些值都是根据公式计算的缓存值。除日期外,所有值均按预期获得,它是
42887.0
而不是Jun-2017
基本上所有值都不是excel单元格中的单独值,而是根据公式计算的值,因为日期POI返回其类型为单元格类型数值
,不知道如何处理,由于无法使用POI,因此我将读取的所有值都将作为公式获取?以下代码应该能够读取任何类型的Excel
单元格内容。我希望我没有忘记什么
主要是:
使用DataFormatter
具有FormulaEvaluator
如果cell.getCellTypeEnum()
是CellType.FORMULA
,则再次检查cell.getCachedFormulaResultTypeEnum()
,并根据情况做出反应
使用最新的稳定版本ApachePOI3.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();
}
}
以下代码应该能够读取任何类型的
Excel
单元格内容。我希望我没有忘记什么
主要是:
使用DataFormatter
具有FormulaEvaluator
如果cell.getCellTypeEnum()
是CellType.FORMULA
,则再次检查cell.getCachedFormulaResultTypeEnum()
,并根据情况做出反应
使用最新的稳定版本ApachePOI3.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();
}
}
是否要Excel中显示的值或其他内容?检查类似问题:是否要Excel中显示的值或其他内容?检查类似问题: