Apache POI无法检测到散列格式的数字

我需要将通过xls / xlsx上传的电话号码读取到Javastringvariables中,尽可能接近在Excel文件中显示的内容。

所以我填写了这些数据: 在这里输入图像说明

正如你所看到的,单元格内的实际值是166609647 ,它的格式是60############# ,所以最后我们看到单元格上出现了60166609647

我想要捕获string中的单元格内容为60166609647 ,但到目前为止,我只能设法捕获166609647 ,任何人都可以启发我什么是错的?

注意:如果我将格式从60############更改为60000000000 ,我可以捕获60166609647而没有任何问题,但是Excel通过公共站点上传,因此我无法执行该操作。

代码如下所示:

 Cell cell = getTheCell(); // Got this after reading the sheets and rows DataFormatter df = new DataFormatter(); String value = df.formatCellValue(cell); // Here in value // If format is 600000000, I can get 60166609647 (right) // If format is 60#######, I get 166609647 (wrong) 

我正在使用的库:

  • poi(poi)3.17
  • poi(poi-ooxml)3.17
  • poi(poi-ooxml-schemas)3.17
  • Java 7

任何人都知道我需要做什么才能做到这一点?

谢谢。

问题是多维的。

起初,数字格式60############不能应用usig Java 。 它导致java.lang.IllegalArgumentException: Malformed pattern "60############"使用DecimalFormat java.lang.IllegalArgumentException: Malformed pattern "60############"

但是,如果需要每个数字前缀为“60”,那么Excel数字格式\6\0#"60"#应该是可能的,并且应该被翻译成DecimalFormat模式'60'# 。 但是apache poiDataFormatter没有,因为它只是从Excel的格式string中删除所有的引用,这导致了60#也是格式不正确。

问题出在DataFormatter.java:671ff 。

我已经在我的MyDataFormatter修补了这个:

 ... // Now, handle the other aspects like // quoting and scientific notation for(int i = 0; i < sb.length(); i++) { char c = sb.charAt(i); /* // remove quotes and back slashes if (c == '\\' || c == '"') { sb.deleteCharAt(i); i--; */ // handle quotes and back slashes if (c == '\\') { sb.setCharAt(i, '\''); sb.insert(i+2, '\''); i+=2; } else if (c == '"') { sb.setCharAt(i, '\''); // for scientific/engineering notation } else if (c == '+' && i > 0 && sb.charAt(i - 1) == 'E') { sb.deleteCharAt(i); i--; } } formatStr = sb.toString(); formatStr = formatStr.replace("''", ""); return formatStr; } ... 

在这个例子中使用这个:

 import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.*; import java.io.FileInputStream; import java.lang.reflect.Method; class ExcelDataformatterExample { public static void main(String[] args) throws Exception { Workbook wb = WorkbookFactory.create(new FileInputStream("ExcelExample.xlsx")); DataFormatter df = new DataFormatter(); MyDataFormatter mydf = new MyDataFormatter(); Sheet sheet = wb.getSheetAt(0); for (Row row : sheet) { for (Cell cell : row) { if (cell.getCellTypeEnum() == CellType.NUMERIC) { CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex()); System.out.println("Cell " + cellRef.formatAsString()); System.out.print("Excel's data format string: "); String formatStr = cell.getCellStyle().getDataFormatString(); System.out.println(formatStr); System.out.print("Value using poi's data formatter: "); Method cleanFormatForNumber = DataFormatter.class.getDeclaredMethod("cleanFormatForNumber", String.class); cleanFormatForNumber.setAccessible(true); String cleanFormatStr = (String)cleanFormatForNumber.invoke(df, formatStr); System.out.print("using poi's cleanFormatStr: "); System.out.print(cleanFormatStr + " result: "); String value = df.formatCellValue(cell); System.out.println(value); System.out.print("Value using my data formatter: "); cleanFormatForNumber = MyDataFormatter.class.getDeclaredMethod("cleanFormatForNumber", String.class); cleanFormatForNumber.setAccessible(true); cleanFormatStr = (String)cleanFormatForNumber.invoke(mydf, formatStr); System.out.print("using my cleanFormatStr: "); System.out.print(cleanFormatStr + " result: "); value = mydf.formatCellValue(cell); System.out.println(value); } } } wb.close(); } } 

它导致以下输出,如果在Excel格式化单元格A1A4值,如下所示:

 Cell A1 Excel's data format string: \60########## Value using poi's data formatter: using poi's cleanFormatStr: 60########## result: 166609647 Value using my data formatter: using my cleanFormatStr: '6'0########## result: 166609647 Cell A2 Excel's data format string: \60000000000 Value using poi's data formatter: using poi's cleanFormatStr: 60000000000 result: 60166609647 Value using my data formatter: using my cleanFormatStr: '6'0000000000 result: 60166609647 Cell A3 Excel's data format string: "60"# Value using poi's data formatter: using poi's cleanFormatStr: 60# result: 166609647 Value using my data formatter: using my cleanFormatStr: '60'# result: 60166609647 Cell A4 Excel's data format string: \6\0# Value using poi's data formatter: using poi's cleanFormatStr: 60# result: 166609647 Value using my data formatter: using my cleanFormatStr: '60'# result: 60166609647