Java Excel文件使用sjxlsx读取日期列

时间:2016-07-13 07:36:11

标签: java excel date

我有一个很大的excel文件,我发现sjxlsx库是一个很好的读取我的文件(因为阅读速度)。

我有一个问题,无法找到解决方法。当我尝试在Excel文件中具有日期格式的单元格上获取getValue()时,获取响应时的数字,但与excel文件中的日期数据无关。

在Excel文件中,我的格式为日期:m / d / Y. 执行cell.getValue() - 返回与日期无关的数字。

如何解决问题? 还有另一个很好的java库来解析excel文件吗?

以下是我用来从Excel文件中读取的代码(我确定我的日期列是B):

 SimpleXLSXWorkbook workbook = new SimpleXLSXWorkbook(companiesFileResource.getFile());
    HSSFWorkbook hsfWorkbook = new HSSFWorkbook();
org.apache.poi.ss.usermodel.Sheet hsfSheet = hsfWorkbook.createSheet();
    com.incesoft.tools.excel.xlsx.Sheet sheetToRead = workbook.getSheet(0, false);
    com.incesoft.tools.excel.xlsx.Sheet.SheetRowReader reader = sheetToRead.newReader();

    System.out.println("Preparing to start saving data");
    com.incesoft.tools.excel.xlsx.Cell[] row;
    int rowPos = 0;
    while ((row = reader.readRow()) != null) {
        if(row[0].getValue() == null) continue;

        SimpleDateFormat dateFormat = new SimpleDateFormat("m/d/Y");
        try {
            System.out.println("DATE: " + row[1].toString());
            companie.setRegisterDate(dateFormat.parse(row[1].toString()));
        } catch (ParseException e) {

        }

1 个答案:

答案 0 :(得分:0)

请勿尝试解析该值,它不是您在Excel中读取的POI读取的格式化数值。使用DateUtils

if (DateUtil.isCellDateFormatted(row[1])) {
                    System.out.println(row[1].getDateCellValue());

查看更多https://poi.apache.org/spreadsheet/quick-guide.html