2011-06-14 66 views
0

我打開和寫在一個Excel xlsx文件,並且每件事情似乎工作正常,但是當我打開文件以查看結果時,列沒有我期望的信息 。寫入xlsx Excel文件時出錯。與apache poi

只有前兩列有信息。而第二欄則有相應的值。

你能告訴我我的錯誤在哪裏嗎?

在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();  

你看到一個錯誤?

謝謝你的幫助。

回答

0

我發現問題出在哪裏。

我沒有意識到存在影響結果的合併列。

所以,與其做

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

我把它改成,例如

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

其中7是其中列開始真正的列數。

相關問題