Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save Excel document Apache POI

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

    //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.

like image 494
Luis A.G. Avatar asked Apr 30 '14 10:04

Luis A.G.


People also ask

Does Apache POI support XLS?

The Apache POI library supports both . xls and . xlsx files and is a more complex library than other Java libraries for working with Excel files.

Does XSSF support XLS?

XSSF is the POI Project's pure Java implementation of the Excel 2007 OOXML (. xlsx) file format. HSSF and XSSF provides ways to read spreadsheets create, modify, read and write XLS spreadsheets.


2 Answers

Uncompilable source code: you define variable out inside try-catch scope and then you use it inside another 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);
}
like image 86
Ivan Babanin Avatar answered Sep 29 '22 22:09

Ivan Babanin


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);
}
like image 25
tk_ Avatar answered Sep 29 '22 23:09

tk_