2017-03-10 140 views
-1

這是在瀏覽器上下載excel文件的代碼,但是當我打開它時excel拋出一個錯誤「Excel無法打開文件Location.xlsx」,因爲文件格式或文件擴展名驗證文件沒有被破壞,並且文件的擴展名與文件的格式相匹配「但是當我簡單地創建這個文件時,它的打開只有當我執行下載操作時出現這個錯誤,並且我正在使用poi jar for excel文件創建...?excel無法打開通過jsp下載的文件

<html> 

    <body> 
     <% 
    LocationDownload.downloadLocation(); 
%> 
<% 
    String filename = "Location.xlsx"; 
    String filepath = "C:\\Users\\dsingh\\GlassFish_Server\\glassfish\\domains\\domain1\\config\\"; 
    response.setContentType("application/vnd.ms-excel"); 
    response.setHeader("Content-Disposition","attachment; filename=" + filename); 

    java.io.FileInputStream fileInputStream=new java.io.FileInputStream(filepath + filename); 

    int i; 
    while ((i=fileInputStream.read()) != -1) { 
    out.write(i); 
    } 
    fileInputStream.close(); 
%> 

    </body> 
</html> 
+1

而是在JSP這樣做的去做具備Servlet,JSP使用字符作家動態生成HTML而不是將損壞的Excel文件的二進制輸出流http://stackoverflow.com/questions/11226603/create -an-excel-file-for-users-to-download-using-apache-poi –

+0

[爲用戶創建一個excel文件使用Apache POI下載]可能的重複(http://stackoverflow.com/questions/11226603/創建一個excel文件爲用戶下載使用apache poi) –

回答

0

請先將文件下載到編碼的Servlet(不JSP文件中做)..

檢查這個代碼將會幫助..這是與我一起爲同樣的目的爲u要求。

package edu.zukrah.servlets; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 

import javax.servlet.ServletException; 
import javax.servlet.ServletOutputStream; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

/** 
* Servlet implementation class FileDownload 
*/ 
@WebServlet("/FileDownload") 
public class FileDownload extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     File f = new File ("E:/pdftest/" + request.getParameter("file")); 
     //set the content type(can be excel/word/powerpoint etc..) 

     String type = request.getParameter("type"); 
     response.setContentType (type); 

     //set the header and also the Name by which user will be prompted to save 
     response.setHeader ("Content-Disposition", "attachment; filename=\""+request.getParameter("file")+"\""); 

     //get the file name 
     String name = f.getName().substring(f.getName().lastIndexOf("/") + 1, f.getName().length()); 

     //OPen an input stream to the file and post the file contents thru the 
     //servlet output stream to the client m/c 
      InputStream in = new FileInputStream(f); 

     try{ 
       ServletOutputStream outs = response.getOutputStream(); 

       int bytesRead; 
       byte[] buf = new byte[4 * 1024]; // 4K buffer 

       try { 
        while ((bytesRead = in.read(buf)) != -1){ 
         outs.write(buf, 0, bytesRead); 
        } 

       } catch (IOException ioe) { 
        ioe.printStackTrace(System.out); 
       } 

       System.out.printf(name + ", %.2f kbs downloaded, %.2f mbs downloaded \n", 
           (f.length()/((double)(1024))), 
           (f.length()/((double)(1024*1024)))); 

       outs.flush(); 
       outs.close(); 
       in.close(); 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
    } 


} 
+0

這是JSP文件的編碼..它調用上面的servlet。 \t \t \t \t \t <! - 對於.pdf文件下載 - > \t \t \t \t <! - 對於.xlsx文件下載 - > myounism

相關問題