Apache OFBiz中的Apache POI异常

时间:2011-02-02 03:54:47

标签: java apache-poi ofbiz

我在一个项目工作。它的服务器端应用程序是在ofbiz.I想要读取,编辑,更新等excel表。但在正常的java程序中它的工作。我使用apache poi API.But当放入ofbiz然后运行应用程序。然后有时会有一些例外。以下是

"
     [java] java.lang.IllegalStateException: Cannot get a text value from a nume
ric formula cell
     [java]     at org.apache.poi.hssf.usermodel.HSSFCell.typeMismatch(HSSFCell.
java:637)
     [java]     at org.apache.poi.hssf.usermodel.HSSFCell.checkFormulaCachedValu
eType(HSSFCell.java:642)
     [java]     at org.apache.poi.hssf.usermodel.HSSFCell.getRichStringCellValue
(HSSFCell.java:719)
     [java]
     [java]     at org.apache.poi.hssf.usermodel.HSSFCell.getStringCellValue(HSS
FCell.java:697)
     [java]     at org.ofbiz.productionmgntSystem.setTargetPlan.getRequiredData(
setTargetPlan.java:764)
     [java]     *****
     [java]     at org.ofbiz.productionmgntSystem.setTargetPlan.setTask(setTarge
tPlan.java:201)
     [java]     at org.ofbiz.productionmgntSystem.ThreadProcess.run(ThreadProces
s.java:113)

"

这里我在普通的java和&中使用相同的apache poi API。的ofbiz。

2 个答案:

答案 0 :(得分:2)

当读取整数交易号时,我遇到了同样的问题。我解决这个问题的自然方式是使用:

if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC) {
   String.valueOf(cell.getNumericCellValue());
}

这不起作用,因为12345转换为12345.0

获取与excel文档中显示的字符串值完全相同的解决方法是:

cell.setCellType(Cell.CELL_TYPE_STRING);
String cellValue=cell.getStringCellValue();

答案 1 :(得分:0)

Excel通常会将仅包含数字的单元格存储为数字单元格,而不是字符串单元格。您可能认为它不是数字单元格,但它是......

您可能需要以下内容:

DataFormatter formatter = new DataFormatter(Locale.US);

....

for(Cell cell : row) {
  String val;
  if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC) {
     // Format the number to look like it does in excel
     val = formatter.formatCellValue(cell);
  }
  if(cell.getCellType()==Cell.CELL_TYPE_STRING) {
     // Just get the string as-is
     val = cell.getRichStringCellValue().getString();
  }
}

查看POI quick guidehowto,以便开始使用POI。