java 如何在java中打开和保存excel文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38750780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:43:15  来源:igfitidea点击:

How to open and save excel file in java

javaapache-poi

提问by Rajeev

I have one excel file (test.xlsx) which contain a column, say id, in which I have applied a formula and when I open it manually, value of all ids changes automatically, because of it when I close it, it ask for save the changes.

我有一个 excel 文件 (test.xlsx),其中包含一列,比如 id,我在其中应用了一个公式,当我手动打开它时,所有 id 的值都会自动更改,因为当我关闭它时,它要求保存更改。

same I have tried with java code as given below

同样,我尝试使用下面给出的 java 代码

public static void main(String[] args) {

        String str1 = "c:/file/test.xlsx";
        try {


            //open file
            XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(new File(str1)));
            System.out.println(wb.getSheetAt(0).getRow(1).getCell(0).getDateCellValue().toString());

            //save file
            FileOutputStream out = new FileOutputStream(str1);
            wb.write(out);
            out.close();

            wb = new XSSFWorkbook(new FileInputStream(new File(str1)));
            System.out.println(wb.getSheetAt(0).getRow(1).getCell(0).getDateCellValue().toString());

        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }




output:
Thu Aug 04 01:08:34 IST 2016
Thu Aug 04 01:08:34 IST 2016

after executing above code I am not getting any exception

执行上述代码后,我没有收到任何异常

I have verified the result by reading file with help of apache POI.

我已经通过在 apache POI 的帮助下读取文件来验证结果。

I want, after saving the file with code new ids should be in Id column as it happening in case of manually open and saving file.

我想,在使用代码保存文件后,新 id 应该在 Id 列中,因为它发生在手动打开和保存文件的情况下。

For me it is not working with code.

对我来说,它不适用于代码。

please suggest

请建议

If you are not able to understand the problem then create file like below and open it and close it manually, after that try with code, you will understand the things

如果您无法理解问题,请创建如下所示的文件并手动打开并关闭它,然后尝试使用代码,您将了解这些内容

enter image description here

在此处输入图片说明

回答by Rajeev

I have found the solution, in above code I have to add only one line, please check below

我找到了解决方案,在上面的代码中我只需要添加一行,请检查下面

public static void main(String[] args) {

        String str1 = "c:/file/test.xlsx";
        try {


            //open file
            XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(new File(str1)));
            System.out.println(wb.getSheetAt(0).getRow(1).getCell(0).getDateCellValue().toString());

            //FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();

            XSSFFormulaEvaluator.evaluateAllFormulaCells(wb);

            //save file
            FileOutputStream out = new FileOutputStream(str1);
            wb.write(out);
            out.close();

            wb = new XSSFWorkbook(new FileInputStream(new File(str1)));
            System.out.println(wb.getSheetAt(0).getRow(1).getCell(0).getDateCellValue().toString());

        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }