2013-07-04 45 views
0

我能夠使用apache poi創建excel文件,但我將如何讓用戶下載將在服務器上創建的excel文件?我如何使用戶能夠下載服務器上創建的excel文件

public String export(UserDetails user) throws Exception { 
    HSSFWorkbook wb = new HSSFWorkbook(); 
    HSSFSheet sheet = wb.createSheet("new sheet"); 
    List<UserDetails> lstUserDetail = getUserDetailList(); 

    for (short RowNum=1; RowNum<lstUserDetail.size();RowNum++){ 
     HSSFRow row = sheet.createRow(RowNum); 
     UserDetails userDetail = lstUserDetail.get(RowNum); 

     HSSFCell cell1 = row.createCell((short)0); 
     HSSFCell cell2 = row.createCell((short)1); 
     HSSFCell cell3 = row.createCell((short)2); 
     HSSFCell cell4 = row.createCell((short)3); 

     cell2.setCellValue(userDetail.getStrUserName()); 
     cell3.setCellValue(userDetail.getStrEMail()); 
     cell4.setCellValue(userDetail.getStrUserPhone()); 
     cell5.setCellValue(userDetail.getStrUserAddress()); 
    } 

    FileOutputStream fileOut = new FileOutputStream("E:\\workbook.xls"); 
    wb.write(fileOut); 
    fileOut.close(); 

    return null; 
} 

回答

0

嘗試下面,你會做出FileDownload的servlet一旦用戶點擊通話的下載鏈接

public class FileDownload extends HttpServlet { 
    public void doGet(HttpServletRequest request,HttpServletResponse response) 
      throws ServletException, IOException { 

     String filename = "E:\\workbook.xls"; 


     ServletOutputStream out = response.getOutputStream(); 
     FileInputStream in = new FileInputStream(filename); 

     response.setContentType("application/vnd.ms-excel"); 
     response.addHeader("content-disposition", 
       "attachment; filename=" + filename); 

     int octet; 
     while((octet = in.read()) != -1) 
      out.write(octet); 

     in.close(); 
     out.close(); 
    } 


} 
+0

但是當代碼將在服務器上進行部署,我不知道該文件的位置....「E:\\ workbook.xls」是我在我的硬盤上的位置...如果我不知道創建文件的位置,我該怎麼辦? – user2550165

+0

PLZ幫助我關於以上? – user2550165

相關問題