且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在apache POI中阅读excel文件的精确单元格内容

更新时间:2022-10-19 18:12:46

不需要显式格式化, apache-poi 提供 DataFormatter 类作为实用程序,以利用Excel上出现的内容的格式。您也可以选择自定义格式,一个简单的例子是(单元格参考您的 XSSFCell 对象):

  System.out.println(new DataFormatter()。formatCellValue(cell)); 

Excel表格如下所示:



img src =https://i.stack.imgur.com/iCsoL.pngalt =enter image description here>



使用 DataFormatter (上面的sop语句)打印:

  50%
$ 1,200
12/21/14

您正常格式化的打印方式:

  0.5 
1200.0
2014年12月21日


when I read a content of cell for e.g. if it is in date format it casts into some another value like 12/31/2099 -> 46052 and $50.00 -> 50 and 50.00% -> 0.5.

But what I want is to get the exact string value for every cell.

My Code is like this:

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

No need for explicit formatting, apache-poi provides for DataFormatter class as utility to leverage the format of the content as it appears on the excel. You can choose custom formats too, a simple example would be (cell is reference to your XSSFCell object):

System.out.println(new DataFormatter().formatCellValue(cell));

Excel sheet looks like:

Using DataFormatter (sop statement above) prints:

50%
$ 1,200
12/21/14

Where your normal formatting would print:

0.5
1200.0
21-Dec-2014