You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@poi.apache.org by "Daniel O." <da...@gmail.com> on 2009/05/04 15:48:37 UTC

excel file generated with errors and repared on Excel 2003

Hi, im generating an excel file in Java with data from a db. I have no
problems opening it on Open Office, but when 

i open it on Office 2003, an error occurs, and the file is "repared" (in the
proccess of recuperation the excel 

restarts). Then the data is showed correctly, with apparently no errors or
modifications.
The problem is that some users in production may have Office instead Open
Office, and we dont want this error (in 

spite of it works) in the production environment.
If I open the file, on Excel or Calc, and i save it, then i dont have the
problem nevermore.
I think that is the way of the data is saved into the XLS file.
Note that i need to show a save us dialog to the user (i have a little
problem with that, but its solved), and 

because of that i have to do it in this way.

Here is part of the code:

Transaction tx = manager.getTransaction();
response.setContentType( "application/vnd.ms-excel" );
response.setHeader("Content-Disposition","attachment;
filename=cocherias_excel" + ".xls");
ArrayList lsCoc = (ArrayList)session.getAttribute("ls_cocheria"); 
ArrayList lsEmp = (ArrayList)session.getAttribute("ls_empleado");

//generates the first sheet
 if (lsCoc != null && lsEmp != null) {

        HSSFWorkbook libro = new HSSFWorkbook();
        HSSFSheet hoja1 = libro.createSheet();
        HSSFRow fila1 = hoja1.createRow((short)0);
		Iterator it = lsCoc.iterator();
        Iterator itE = lsEmp.iterator();
        COCHERIA cocheria = null;
        EMPLEADO empleado = null;
        int n = 1;

        HSSFCell celda1 = fila1.createCell((short) 0);

        celda1.setCellValue("Numero");
        celda1 = fila1.createCell((short)1);
        celda1.setCellValue("Razón Social");
        celda1 = fila1.createCell((short)2);
        celda1.setCellValue("Habilitada");

        while (it.hasNext()) {

            cocheria = (COCHERIA)it.next();

            fila1 = hoja1.createRow((short)n);
            celda1 = fila1.createCell((short) 0);
            celda1.setCellValue(cocheria.getCCA_ID().longValue());
            celda1 = fila1.createCell((short)1);
            celda1.setCellValue(cocheria.getCCA_RAZON_SOCIAL());
            celda1 = fila1.createCell((short)2);
            celda1.setCellValue(cocheria.getCCA_HABILITADA());

            n++;
        };
        n = 1;

//generates the second sheet

        HSSFSheet hoja2 = libro.createSheet();
        HSSFRow fila2 = hoja2.createRow((short)0);

        HSSFCell celda2 = fila2.createCell((short) 0);

        celda2.setCellValue("Numero de Cocheria");
        celda2 = fila2.createCell((short)1);
        celda2.setCellValue("Apellido");
        celda2 = fila2.createCell((short)2);
        celda2.setCellValue("Nombre");
        celda2 = fila2.createCell((short)3);
        celda2.setCellValue("Tipo de Doc.");
        celda2 = fila2.createCell((short)4);
        celda2.setCellValue("Nro. de Documento");
        celda2 = fila2.createCell((short)5);
        celda2.setCellValue("Habilitado");

        while (itE.hasNext()) {

            empleado = (EMPLEADO)itE.next();
            fila2 = hoja2.createRow((short)n);

            celda2 = fila2.createCell((short)0);
            celda2.setCellValue(empleado.getEPO_CCA_ID().longValue());
            celda2 = fila2.createCell((short)1);
            celda2.setCellValue(empleado.getEPO_APELLIDO());
            celda2 = fila2.createCell((short)2);
            celda2.setCellValue(empleado.getEPO_NOMBRE());
            celda2 = fila2.createCell((short)3);
            celda2.setCellValue(empleado.getEPO_TIPO_DOCUMENTO());
            celda2 = fila2.createCell((short)4);
            if (empleado.getEPO_NRO_DOCUMENTO() != null){
               
celda2.setCellValue(empleado.getEPO_NRO_DOCUMENTO().longValue());
            } else {
                celda2.setCellValue("");
            }
            celda2 = fila2.createCell((short)5);
            celda2.setCellValue(empleado.getEPO_HABILITADO());

            n++;
        };

        byte[] wbBytes = libro.getBytes();

        response.setContentLength(wbBytes.length);
        response.setBufferSize(wbBytes.length);
        response.getOutputStream().write(wbBytes,0,wbBytes.length);
        response.getOutputStream().flush();


Thanks for your future help.
-- 
View this message in context: http://www.nabble.com/excel-file-generated-with-errors-and-repared-on-Excel-2003-tp23367850p23367850.html
Sent from the POI - Dev mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org