保存Excel文档 Apache POI

7

我需要从一个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);
        }
    }

问题在于Informe.xls文件是空的(文件大小为0 KB),Excel显示其已损坏或损坏。我想我没有很好地处理输出流和写入,但不知道该如何解决。

“我需要从Excel文档中创建表格”可能对您来说是非常精确的描述,但对外部世界来说意义不大。请您编辑您的帖子,使其更加精确,例如:我正在尝试加载现有的Excel文件,将第一张工作表中第2列/第3行的值更新为“BONANZA”,并存储更新后的文件。 - Norbert Radyk
这里的问题在于输出和文件写入,正如我所指出的描述。在我的代码中,更新值是“强制性”设置的,没有检查单元格类型是否相同以及更新值时应该完成的其他事情,因此我认为我应该保留标题和描述,以免混淆人们。 - Luis A.G.
2个回答

11

无法编译的源代码:您在try-catch作用域内定义变量out,然后在另一个try-catch中使用它。

尝试使用此代码:

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);
}

2
这个怎么样,
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);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接