7

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.

Shaeldon
  • 873
  • 4
  • 18
  • 28
Luis A.G.
  • 1,017
  • 2
  • 15
  • 23
  • 'I need to create informs from an excel document' might be a very precise description for you, but has not much meaning for the outside world. Can you please edit your post to be more precise i.e. I'm trying to load existing Excel file, update the value in 2nd column/3rd row of the first sheet to 'BONANZA' and store the updated file. – Norbert Radyk Apr 30 '14 at 11:01
  • The problem here was with the output and writting of the file as I pointed at the description that I thought. In my showed code the updating values are set "by force", without checking if the cell type is the same and other things that should be done when updating values, so I think that I should keep the tittle and description in order not to confuse people – Luis A.G. Apr 30 '14 at 11:17

2 Answers2

11

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);
}
Ιναη ßαbαηιη
  • 3,410
  • 3
  • 20
  • 41
2

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);
}
tk_
  • 16,415
  • 8
  • 80
  • 90