使用= now()公式

时间:2018-03-15 19:22:30

标签: java excel date apache-poi locale

所以我试图用java使用POI读取excel文件,所有这些都很好,直到日期出现。 excel文件是我无法更改的模板,其中的日期使用 = now()公式。

当我查看单元格格式时,语言环境设置为English India. 例如,这会扰乱时间转换 16-03-2018成为2015-11-17 check this.

我通过反复试验找到的唯一解决方案是,只有将语言环境更改为英语美国

时,代码才有效

Download the Excel file

And here is how it looks in my system (Locale India).

    case Cell.CELL_TYPE_FORMULA:

                    SimpleDateFormat DtFormat = new SimpleDateFormat("yyyy-MM-dd");
                    Date date = cell.getDateCellValue();
                    System.out.println(DtFormat.format(date));
                    break;

完整的java代码

public static void main(String[] args) throws IOException
{
    String excelFilePath = "C:\\Users\\oi309\\Documents\\Projects\\AOI\\AOI Standard Format1.xlsx";
    FileInputStream inputStream = new FileInputStream(new File(excelFilePath));

    Workbook workbook = null;

    if (excelFilePath.endsWith("xlsx"))
    {
        workbook = new XSSFWorkbook(inputStream);
    }
    else if (excelFilePath.endsWith("xls"))
    {
        workbook = new HSSFWorkbook(inputStream);
    }
    else
    {
        throw new IllegalArgumentException("The specified file is not Excel file");
    }

    Sheet firstSheet = workbook.getSheetAt(0);
    Iterator<Row> iterator = firstSheet.iterator();

    while (iterator.hasNext())
    {
        Row nextRow = iterator.next();
        Iterator<Cell> cellIterator = nextRow.cellIterator();

        while (cellIterator.hasNext())
        {
            Cell cell = cellIterator.next();

            if (cell.getCellType() != Cell.CELL_TYPE_BLANK)
            {

                switch (cell.getCellType())
                {

                case Cell.CELL_TYPE_STRING:
                    if (!cell.getStringCellValue().contains("NVS"))

                        System.out.print(cell.getStringCellValue());
                    break;
                case Cell.CELL_TYPE_FORMULA:

                    SimpleDateFormat DtFormat = new SimpleDateFormat("yyyy-MM-dd");
                    Date date = cell.getDateCellValue();
                    System.out.println(DtFormat.format(date));
                    break;
                case Cell.CELL_TYPE_NUMERIC:

                    System.out.print(cell.getNumericCellValue());
                    break;

                }

            }
        }
        System.out.println();

    }

    workbook.close();
    inputStream.close();
}

提前谢谢你。

1 个答案:

答案 0 :(得分:1)

您遇到的问题与单元格格式或区域设置无关。存储在文件中的单元格值与公式=NOW()一致:

<c r="B4" s="5"><f ca="1">NOW()</f><v>42325.30892476852</v></c>

这是2015-11-17(2015年11月17日上午07:24:51)。这是日期值apache poi将从文件中读取而不重新评估公式。

如果您查看文档属性(/docProps/core.xml),您会发现:

<dcterms:modified xsi:type="dcterms:W3CDTF">2015-11-17T13:25:31Z</dcterms:modified>

所以2015-11-17是这个文件最后一次在Excel打开并重新计算的日期。在此日期=NOW()的日期是2015-11-17。

所以我怀疑这个文件是一个模板,创建于2015-11-17,然后用Excel之外的值填充,无需重新计算。如果您要在Excel中打开此文件,则会重新计算=NOW(),因为它是易失性函数。然后日期将是当前日期。如果您在没有进一步更改的情况下重新保存文件,则存储在文件中的单元格值=NOW()也是当前日期时间。在此apache poi之后将读取此日期。

因此,为了在apache poi之前使用Excel读取此文件而不在... workbook.getCreationHelper().createFormulaEvaluator().evaluateAll(); Sheet firstSheet = workbook.getSheetAt(0); Iterator<Row> iterator = firstSheet.iterator(); while (iterator.hasNext()) ... 中重新开始,需要重新评估公式。最简单的方法:

apache poi

请阅读Formula Evaluation,了解使用SELECT * FROM `activities` ORDER BY id = 4 DESC LIMIT 0,4 评估公式的其他可能性。