2011-08-03 122 views

回答

8

您需要設置標題和內容類型是這樣的:

response.setHeader "Content-disposition", "attachment;filename=myExcel.xls" 
    response.contentType = "application/vnd.ms-excel" 

然後流中的響應內容。

編輯:如果您需要設置內容長度:

response.contentLength = 100 

記錄在javadoc

+0

作爲網站的用戶,我想使用內容長度頭的好處。 – Basilevs

2
response.setContentType("APPLICATION/OCTET-STREAM"); 
response.setHeader("Content-Disposition", "Attachment;Filename=\"MyFile.xls\""); 
0

你必須添加jxl jar

嘗試內容長度此代碼:

@WebServlet("/Reportexel") 
public class Reportexel extends HttpServlet 
{ 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    { 
     try 
     { 
      response.setHeader("Content-disposition", "attachment; filename=kbdemo.xls"); 
      response.setContentType("application/vnd.ms-excel"); 
      WritableWorkbook workbook = Workbook.createWorkbook(response.getOutputStream());  
      WritableSheet worksheet = workbook.createSheet("Sheet 1",0); 

      Label lbl = new Label(1,1,"Hello"); 
      Label lbl1 = new Label(1,2,"Hi..."); 

      worksheet.addCell(lbl); 
      worksheet.addCell(lbl1); 

      workbook.write(); 
      workbook.close(); 
     } 
     catch(Exception e) 
     { 
      System.err.println("Main Error : "+e); 
     } 
    } 
}