2012-06-28 6 views
6

मैं प्रयोक्ता के लिए अपाचे पोई के माध्यम से डाउनलोड करने के लिए एक्सेल फ़ाइल बनाने का लक्ष्य रख रहा हूं।आउटपुट स्ट्रीम में एक पोई वर्कबुक लिखना अजीब मूल्यों को उत्पन्न कर रहा है

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"); 

समस्या यह है कि मैं इन अजीब मूल्यों हो रही है:

मैं अपने सर्वलेट में इस कोड है

`... MySheetŒ®üÿ» आइए डब्ल्यू © ñÒMbP du? * +, €% ÿÁƒ "¡" d ,, à? À?

किसी भी सुझाव?

उत्तर

7

क्या गलत पाया। HttpServletResponse पहले कुछ और पहले सेट किया जाना चाहिए

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

अधिक प्रकार की कोशिकाओंके लिए

cell.setCellType(Cell.CELL_TYPE_STRING); 

चेक एपीआई: आप प्रत्येक कोशिका के लिए कोशिका प्रकार निर्धारित करने की आवश्यकता, तुम इतनी का उपयोग कर सकते।

या आप इसके लिए DataFormatter का उपयोग कर सकते हैं।

+0

यह सही जवाब नहीं था, लेकिन मुझे एक और समस्या से मदद मिली। धन्यवाद :) – tarikki

संबंधित मुद्दे