简体   繁体   English

我如何使用jsp servlet从服务器端将Excel文件下载到客户端

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

I am implementing a web application where I need to download database data into excel. 我正在实现一个Web应用程序,需要将数据库数据下载到excel中。 I retrieve all the data into excel, and save on the server side, but how can I download that excel into client side? 我将所有数据检索到excel中,并保存在服务器端,但是如何将excel下载到客户端?

I have done this code: 我已经完成了以下代码:

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

After that what I will do to download that excel file? 之后,我将如何下载该Excel文件?

Create an ExcelService class with a method like this, which is universal. 使用通用的这样的方法创建一个ExcelService类。 This method can be used either for writing to a file (which is useful for testing and if you need to store the files on the server side), or using this method you may later write to the output stream of your servlet. 此方法可以用于写入文件(这对于测试以及是否需要在服务器端存储文件很有用),也可以使用此方法稍后再写入servlet的输出流。

Also notice that each method is responsible for closing the resource (and just that resource) which it has opened - writeToXlsx(OutputStream) for the Workbook , writeToXlsx(String) for the OutputStream . 还要注意,每个方法负责关闭资源 ,它已经打开(和只是资源) - 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);
    }
}

For your convenience (and testing), you may create a specific method which writes to a file. 为了您的方便(和测试),您可以创建一个写入文件的特定方法。 However this is not the method to be used by your client application! 但是,这不是客户端应用程序要使用的方法!

/**
 * 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);
    }
}

Your servlet may look like this. 您的servlet可能看起来像这样。 It uses the service you have created. 它使用您创建的服务。

@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.

相关问题 如何获取Java Servlet或JSP中客户端下载位置的路径? - How can i get the path of client side download location in java servlet or jsp? 如何在服务器端使用jsp在客户端运行applet - how to run the applet on client side using jsp in server side 如何从服务器端代码(JSP或Servlet)读取客户端本地磁盘上的文件 - How to read files on client local disk from server side code (JSP or Servlet) 如何将json对象从jsp页面的jsp部分传递到javascript部分,即从服务器端到客户端? - How to pass a json object from the jsp part of a jsp page to the javascript part i.e from server side to client side? 如何从客户端的控制器下载zip文件? 春季靴 - How can I download zip file from controller on client side? Spring boot 使用GWT将Excel文件从服务器端发送到客户端 - Send excel file from server-side to client-side using GWT 如何从服务器端将文件下载到HD - How to Download a File from the Server Side to the HD 使用rest服务将文件从服务器端发送到客户端 - send a file from the server side to the client side using rest service 如何在客户端加密密码并在服务器端解密密码? - How can I encrypt password on client side and decrypt it on server side? 如何将响应从servlet发送到客户端? - How to send response from servlet to client side?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM