簡體   English   中英

我如何使用jsp servlet從服務器端將Excel文件下載到客戶端

[英]how can i download an excel file from server side to client side using jsp servlet

我正在實現一個Web應用程序,需要將數據庫數據下載到excel中。 我將所有數據檢索到excel中,並保存在服務器端,但是如何將excel下載到客戶端?

我已經完成了以下代碼:

row = spreadsheet.createRow(p);
cell = row.createCell(1);
cell.setCellValue(m);
cell = row.createCell(2);
cell.setCellValue(emp_code);
cell = row.createCell(3);
cell.setCellValue(card_no);
// ... more code

String root = getServletContext().getRealPath("/");
File    path = new File(root + "/Downloads/ExcellFile");
f="/salary_Report.xls";
String n=path+f;
System.out.println(" File name"+f);
if (!path.exists()) {
    boolean status = path.mkdirs();
}

FileOutputStream output = new FileOutputStream(new File(path+f));
workbook.write(output);
output.close();

之后,我將如何下載該Excel文件?

使用通用的這樣的方法創建一個ExcelService類。 此方法可以用於寫入文件(這對於測試以及是否需要在服務器端存儲文件很有用),也可以使用此方法稍后再寫入servlet的輸出流。

還要注意,每個方法負責關閉資源 ,它已經打開(和只是資源) - writeToXlsx(OutputStream)WorkbookwriteToXlsx(String)OutputStream

/**
 * Create an Excel file (as OutputStream) with your data
 * 
 * @param output The OutputStream to which the method should write. 
 *        It must be opened before!
 *        Notice that although it is an <em>output</em> of the method,
 *        it is provided as an <em>input</em> parameter.
 *        The idea is: "Give me the data, and here is the place 
 *        you should put them to."
 * @throws IOException
 */
public void writeToXlsx(OutputStream output) throws IOException {
    try (final Workbook wb = new XSSFWorkbook();) {
        final Sheet sheet = wb.createSheet(...);
        // your code here to fill the sheet
        wb.write(output);
    }
}

為了您的方便(和測試),您可以創建一個寫入文件的特定方法。 但是,這不是客戶端應用程序要使用的方法!

/**
 * Create an Excel file (as file) with your data.
 * 
 * @param outputFile The name of the output file.
 * @throws IOException
 */
public void writeToXlsx(String outputFile) throws IOException {
    try (OutputStream output = new FileOutputStream(new File(outputFile))) {
        writeToXlsx(output);
    }
}

您的servlet可能看起來像這樣。 它使用您創建的服務。

@WebServlet("/content/data")
public class DataServlet extends HttpServlet {
    private static final long serialVersionUID = ....;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        writeHeader(response);
        writeBody(response);
    }

    private void writeBody(HttpServletResponse response) throws IOException {
        // Each servlet has the output stream which is directly 
        // streamed to the client browser as the response:
        final OutputStream output = response.getOutputStream();

        // Now your service creates the data and writes them to the 
        // output stream:
        final ExcelService service = new ExcelService();
        service.writeToXlsx(output);

        // To be sure that nothing is lost:
        output.flush();
    }

    private void writeHeader(HttpServletResponse response) {
        // Here you tell the browser that the result is not HTML, but Excel.
        // The browser uses this information to open an application
        // associated in the client operating system with this type
        // of data.
        response.setContentType("application/ms-excel");

        // The browser will pass this information to Excel
        // which will consider this as the file name
        response.setHeader("Content-Disposition", "attachment; filename=MyData.xlsx");
    }    
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM