2012-06-28 13 views
6

sto mirando a creare un file Excel per l'utente da scaricare tramite apache poi.scrivere una cartella di lavoro in uscita per generare flusso sta generando valori strani

Ho questo codice nel mio 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"); 

Il problema è che sto ottenendo questi valori strani:

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

qualche suggerimento?

risposta

7

trovato che cosa è sbagliato. Il HttpServletResponse deve prima essere impostata prima di ogni altra cosa

//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

È necessario impostare il tipo di cellule per ogni cella, è possibile farlo utilizzando:

cell.setCellType(Cell.CELL_TYPE_STRING); 

Controllare API per più tipi di cellule here.

Oppure È possibile utilizzare DataFormatter per lo stesso.

+0

Questa non era la risposta giusta, ma mi ha aiutato con un altro problema. Grazie :) – tarikki

Problemi correlati