java 如何通过servlet打开Excel文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6841998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:32:32  来源:igfitidea点击:

How can I open Excel file through servlet

javajspservlets

提问by Yagnesh Agola

I have used jxls.jar library to export data in to excel format and stored in file with *.xls format. How can I open or promote dialog box for open or save this file after complete writing process into file using servlet the all proccess for writing in to file is done in separate function..

我使用 jxls.jar 库将数据导出为 excel 格式并以 *.xls 格式存储在文件中。在使用 servlet 完成写入文件的过程后,如何打开或提升对话框以打开或保存此文件,写入文件的所有过程都在单独的函数中完成。

回答by BalusC

I understand that you have the Excel file as a Fileobject and that you want to provide this as a download to the client. You need to set the Content-Dispositionheader to attachmentto let the client show a Save Asdialogue. You also need to set the Content-Typeheader as well to let the client know what file type it is so that it can eventually associate the right application with it for the case that the enduser would like to open it immediately. Finally, setting the Content-Lengthheader is preferably as it improves serving performance (otherwise the Servlet API will fall back to chunked encoding which requires a bit more bytes and processing time).

我了解您将 Excel 文件作为File对象,并且您希望将其作为下载提供给客户端。您需要将Content-Disposition标题设置attachment为让客户端显示另存为对话框。您还需要设置Content-Type标头,让客户端知道它是什么文件类型,以便最终用户可以将正确的应用程序与其相关联,以应对最终用户想要立即打开它的情况。最后,Content-Length最好设置标头,因为它可以提高服务性能(否则 Servlet API 将退回到需要更多字节和处理时间的分块编码)。

After setting the proper headers, it's just a matter of writing the InputStreamfrom the Fileto the OutputStreamof the HttpServletResponsethe usual Java IO way.

设置适当的标题后,它只是一个写的事InputStreamFileOutputStreamHttpServletResponse通常的Java IO方式。

private static final int DEFAULT_BUFFER_SIZE = 8192; // 8KB.
// ...

File file = createExcelFileSomehow();
// ...

response.reset();
response.setBufferSize(DEFAULT_BUFFER_SIZE);
response.setHeader("Content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Length", String.valueOf(file.length()));
response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");

BufferedInputStream input = null;
BufferedOutputStream output = null;

try {
    input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
    output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);

    byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
    for (int length; (length = input.read(buffer)) > -1;) {
        output.write(buffer, 0, length);
    }
} finally {
    if (output != null) try { output.close(); } catch (IOException ignore) {}
    if (input != null) try { input.close(); } catch (IOException ignore) {}
}

回答by Dmitri

Are you asking how to send the file to the user?

你问如何将文件发送给用户?

This may help: Servlet for serving static content

这可能有帮助:用于提供静态内容的 Servlet

Then just create an HTML link to the servlet from whatever you use for presentation.

然后只需从用于演示的任何内容创建到 servlet 的 HTML 链接。

回答by Amareswar

Add a header Content-Disposition: attachment

添加标题 Content-Disposition:attachment

回答by Balaji.N.S

This code snippet should help you. When you give Content disposition as inline in the IE browsers it will open the excel as such without prompting the dialog box.

此代码片段应该可以帮助您。当您在 IE 浏览器中将内容配置为内联时,它将打开 excel 而不提示对话框。

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition","inline;fileName=" + fileName);
final java.io.OutputStream os = response.getOutputStream();

call the createExcel function passing OutputStream Object

调用 createExcel 函数传递 OutputStream 对象

 
os.flush();
os.close();