Java 保存 Excel 文档 Apache POI

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23386091/
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-08-13 22:21:39  来源:igfitidea点击:

Save Excel document Apache POI

javaexcelapache-poi

提问by Luis A.G.

I need to create informs from an excel document, I'm using Java and Apache POI. Here is my code:

我需要从 excel 文档创建通知,我使用的是 Java 和 Apache POI。这是我的代码:

    //Get path with JFileChooser
    public static String LeeRuta(){
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        chooser.showDialog(chooser, "Seleccionar");
        File f =chooser.getSelectedFile();
        File camino = f.getAbsoluteFile();
        String ruta = camino.getAbsolutePath();
        return ruta;
    }

  //main
  public static void main(String args[]) {
    String ruta=LeeRuta();

    /* Don't know if neccesary, but it didn't works with or without it
    InputStream inp;
    try {
        inp = new FileInputStream(ruta);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(PruebaExcel.class.getName()).log(Level.SEVERE, null, ex);
    }
    */

    Workbook exceldoc = null;

    // Opening file
      try {
         exceldoc = WorkbookFactory.create(new File(ruta));
        //wb = WorkbookFactory.create(new File(ruta));
    } catch (InvalidFormatException | IOException e) {
        e.printStackTrace();
    }


    //Selecting the sheet
    String nombredoc = exceldoc.getSheetName(0);
    Sheet hoja = exceldoc.getSheet(nombredoc);


        //Selecting the cell
        Cell celda = hoja.getRow(1).getCell(1);
//        System.out.println(celda);
        System.out.println(hoja.getRow(2).getCell(3));

        //Variables
        int anyo = 2014;
        String ota = "OTa 158";

        //Setting Cells value
        hoja.getRow(2).getCell(4).setCellValue(anyo);
        hoja.getRow(2).getCell(5).setCellValue(ota);

       //If I print here cell values, I see that values has been set.

        //Saving
        try {
            //hoja.getRow(3).getCell(4).setCellValue(fecha);
            FileOutputStream out = new FileOutputStream("C:\Documents and Settings\INGENIERIA2\Mis documentos\Informe.xls");
        } catch (FileNotFoundException ex) {
            Logger.getLogger(PruebaExcel.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            exceldoc.write(out);
        } catch (IOException ex) {
            Logger.getLogger(PruebaExcel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

The problem is that Informe.xls file is empty (file size = 0 KB) and Excel says its corrupted or damaged. I suppose I'm not doing well the output stream and the write, but don't know how to fix it.

问题是 Informe.xls 文件是空的(文件大小 = 0 KB)并且 Excel 说它已损坏或损坏。我想我在输出流和写入方面做得不好,但不知道如何解决。

采纳答案by Ivan Babanin

Uncompilable source code: you define variable outinside try-catchscope and then you use it inside another try-catch.

不可编译源代码:您定义变量里面的try-catch范围,然后你使用它里面的另一个的try-catch

Try this code:

试试这个代码:

try {
    FileOutputStream out = new FileOutputStream("C:\Documents and Settings\INGENIERIA2\Mis documentos\Informe.xls");
    exceldoc.write(out);
    out.close();
} catch (FileNotFoundException ex) {
    Logger.getLogger(PruebaExcel.class.getName()).log(Level.SEVERE, null, ex);
}

回答by tk_

How about this,

这个怎么样,

try (FileOutputStream out = new FileOutputStream("C:\file\path\here\Informe.xls")) {
    exceldoc.write(out);
} catch (FileNotFoundException ex) {
    Logger.getLogger(PruebaExcel.class.getName()).log(Level.SEVERE, null, ex);
}