2012-06-28 15 views
6

mi objetivo es crear un archivo de Excel para que el usuario lo descargue a través de apache poi.escribir un libro de trabajo poi para la secuencia de salida está generando valores extraños

tengo este código en mi servlet:

protected void doGet(HttpServletRequest request, 
       HttpServletResponse response) throws ServletException, IOException 
     { 

      // create a workbook , worksheet 
      Workbook wb = new HSSFWorkbook(); 
      Sheet sheet = wb.createSheet("MySheet"); 
      CreationHelper createHelper = wb.getCreationHelper(); 

      // Create a row and put some cells in it. Rows are 0 based. 
      Row row = sheet.createRow((short)0); 
      Cell cell = row.createCell(0); 
      cell.setCellValue(1); 
      row.createCell(1).setCellValue(1.2); 
      row.createCell(2).setCellValue(createHelper.createRichTextString("This is a string")); 
      row.createCell(3).setCellValue(true); 

      //write workbook to outputstream 
      ServletOutputStream out = response.getOutputStream(); 
      wb.write(out); 
      out.flush(); 
      out.close(); 

      //offer the user the option of opening or downloading the resulting Excel file 
      response.setContentType("application/vnd.ms-excel"); 
      response.setHeader("Content-Disposition", "attachment; filename=MyExcel.xls"); 

El problema es que estoy recibiendo estos valores extraños:

`... MySheetŒ®üÿ» IA w Dü © ñÒMbP? * +,% ÿÁƒ € “¡" D ,, A? A?

alguna sugerencia?

Respuesta

7

encontraron cuál es incorrecto. HttpServletResponse el primero se debe establecer antes que nada

//offer the user the option of opening or downloading the resulting Excel file 
     response.setContentType("application/vnd.ms-excel"); 
     response.setHeader("Content-Disposition", "attachment; filename=MyExcel.xls"); 
1

Es necesario configurar el tipo celular para cada celda, puede hacerlo a través de:

cell.setCellType(Cell.CELL_TYPE_STRING); 

Comprobar API para más tipos de células here.

O Puede usar DataFormatter para lo mismo.

+0

Esta no era la respuesta correcta, pero me ayudó con otro problema. Gracias :) – tarikki

Cuestiones relacionadas