写入xlsx Excel文件时出错。与apache poi

时间:2011-06-14 22:06:52

标签: excel api apache-poi

我在Excel xlsx文件上打开和写入,每件事似乎都运行正常, 但是当我打开文件以查看结果时,列没有信息 我期待。

只有前两列有信息。并且2秒列doest具有相应的值。

你能告诉我我的错误在哪里吗?

POI上有错误吗?

这是我的代码。


String exceloutp = "filename.xlsx"; //File name where to write the values.
             File fileop = new File(exceloutp);
         FileInputStream fs = new FileInputStream(exceloutp);  
         XSSFWorkbook wb = new XSSFWorkbook( fs );

             // get a reference to the worksheet
         XSSFSheet sheet = wb.getSheetAt(0);

             Iterator it = dataResultados.iterator();
             BeanResultadosPaginaFormato27 b = null;
             XSSFCell cell = null;

//Create style for the cells

             XSSFCellStyle normalFormat = wb.createCellStyle();              
             normalFormat.setBorderBottom(CellStyle.BORDER_THIN);
             normalFormat.setBorderTop(CellStyle.BORDER_THIN);
             normalFormat.setBorderLeft(CellStyle.BORDER_THIN);
             normalFormat.setBorderRight(CellStyle.BORDER_THIN);
             normalFormat.setAlignment( CellStyle.ALIGN_LEFT );

             XSSFFont normalFont = wb.createFont();
             normalFont.setFontHeightInPoints((short) 8 );
             normalFont.setFontName("Arial");

             normalFormat.setFont(normalFont);

//end create a style for the cells.                             


//Start filling the cells in every row  width data.
//starting from line 26.

             fila = 26;
             columna = 0;
             while( (fila < 47) &&  ( it.hasNext() ) )
             {
                 columna = 0;
                 b = (BeanResultadosPaginaFormato27) it.next();
                 cell = null;

                 cell = sheet.getRow(fila).createCell(columna++);
                 cell.setCellType( Cell.CELL_TYPE_STRING );
                 cell.setCellStyle( normalFormat );
                 cell.setCellValue( b.getParametro() );//String 1st column

                 cell = sheet.getRow(fila).createCell(columna++);
                 cell.setCellType( Cell.CELL_TYPE_STRING );
                 cell.setCellStyle( normalFormat );
                 cell.setCellValue( b.getUnidad() );//String 2nd column

                 cell = sheet.getRow(fila).createCell(columna++);
                 cell.setCellType( Cell.CELL_TYPE_STRING );
                 cell.setCellStyle( normalFormat );
                 cell.setCellValue( b.getMetodologia() );//String 3rd column

                 cell = sheet.getRow(fila).createCell(columna++);
                 cell.setCellType( Cell.CELL_TYPE_STRING );
                 cell.setCellStyle( normalFormat );
                 cell.setCellValue( b.getUsuario() );//String

                 cell = sheet.getRow(fila).createCell(columna++);
                 cell.setCellType( Cell.CELL_TYPE_STRING );
                 cell.setCellStyle( normalFormat );
                 cell.setCellValue( b.getConcentracion() );//String

                 cell = sheet.getRow(fila).createCell(columna++);
                 cell.setCellType( Cell.CELL_TYPE_STRING );
                 cell.setCellStyle( normalFormat );
                 cell.setCellValue( b.getLimites() ); //String

                 fila++; //Next row.

             }                           

//------End filling cells with data.

//Save the file and open.

             OutputStream out = response.getOutputStream();              
             response.setHeader("Pragma", "no-cache");
             response.setContentType("application/vnd.ms-excel");          
             response.setHeader("Content-Disposition", "attachment; filename=\""+  fileop.getName() +"\"");

             wb.write(out);
             out.flush();
             out.close();    

你看到错误吗?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我发现了问题所在。

我没有意识到存在影响结果的合并列。

所以,而不是做

cell = sheet.getRow(fila).createCell(columna++);

我将其更改为,例如

cell = sheet.getRow(fila).createCell( 7 );

其中7是列开始的实际列号。