简体   繁体   English

如何将使用jxl.jar生成的Excel文件从服务器计算机发送到客户端

[英]How to send excel file which is generated using jxl.jar from server machine to client

I am using struts to develop my web application. 我正在使用struts开发我的Web应用程序。 I wrote a method which generates excel file and saves it based on the data model I pass.. Works great on my local machine since tomcat is present locally.. But now I've moved my application to a central server.. Now if I use the method it stores generates the excel file in the server not in the client machine which are hitting the server.. I need to pass it from server to client through http.. How do I do that? 我写了一种方法,可以生成excel文件并根据传递的数据模型保存该文件。由于tomcat在本地,因此在本地计算机上运行良好。使用它存储的方法在服务器中而不是在正在打服务器的客户端计算机中生成excel文件。我需要通过http将其从服务器传递到客户端。我该怎么做?

public static void populateExcelDoc(List<ColumnList> listOfColumns,RowList rowlist,String filename)
{
    try
    {
        WorkbookSettings ws = new WorkbookSettings();
        ws.setLocale(new Locale("en", "EN"));

        WritableWorkbook workbook = Workbook.createWorkbook(new File(filename+".xls"), ws);
        WritableSheet s = workbook.createSheet("Sheet1", 1);
        writeDataSheetifx1(s,listOfColumns,rowlist);
        workbook.write();
        workbook.close();

        Process p = 
                  Runtime.getRuntime()
                   .exec("rundll32 url.dll,FileProtocolHandler " + filename+".xls");
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
    catch(WriteException e)
    {
        e.printStackTrace();
    }
}  

This is what my current code looks like.. What change do I need to make? 这就是我当前的代码。.我需要进行哪些更改? Should I be using HttpServletResponse response? 我应该使用HttpServletResponse响应吗?

In Struts2 there is stream result for sending data to the HttpServletResponse. 在Struts2中,存在将数据发送到HttpServletResponse的stream结果。

In your struts.xml configure your action to use stream result. struts.xml将操作配置为使用stream结果。

<action name="exceldoc" method="populateExcelDoc" class="ExcelDocAction">
  <result name="success" type="stream">
    <param name="inputName">inputStream</param>
    <param name="contentType">application/vnd.ms-excel</param>
    <param name="contentDisposition">attachment;filename=excel.xls</param>
  </result>
</action>

And inside your action create private InputStream inputStream; 并在您的操作内部创建private InputStream inputStream; with getters/setters and write your file to it inside your method. 使用getter / setter并在您的方法中将文件写入其中。

You have several options, two of them are: 您有几种选择,其中两个是:

1) Save the file in a place where a client browser can access it 1)将文件保存在客户端浏览器可以访问的位置

To do this, you will have to find out a decent directory to store the file. 为此,您将必须找到一个不错的目录来存储文件。 When you are using a Servlet, you can create an absolute file systems path from a relative URL path using this method: 使用Servlet时,可以使用以下方法从相对URL路径创建绝对文件系统路径:

String filePath = getServletContext().getRealPath("/MyFile.xls");
WritableWorkbook workbook = Workbook.createWorkbook(new File(filePath), ws);
...

Then users can access it from the browser using a URL like: http://host:port/AppName/MyFile.xls 然后用户可以使用以下URL从浏览器访问它: http://host:port/AppName/MyFile.xls

You could also modify the response header, so that the browser has to read the file: 您还可以修改响应头,以便浏览器必须读取文件:

response.setHeader("Content-Disposition", "attachment;filename=/MyFile.xls");

Disadvantage of this method is that the file will be accessible for everybody and that you probably will have to think about generating unique file names, if there are multiple users generating the file again and again. 此方法的缺点是,每个人都可以访问该文件,并且如果有多个用户一次又一次地生成文件,则可能必须考虑生成唯一的文件名。 Then you will also have to implement a delete routine for old files. 然后,您还必须实现旧文件的删除例程。

2) Send the file directly from the servlet to the browser. 2)将文件直接从servlet发送到浏览器。

If the response of your Servlet request can be the created Excel file, this is probably the best way. 如果您的Servlet请求的响应可以是创建的Excel文件,则这可能是最好的方法。

Set the response header accordingly to the file type and size: 根据文件类型和大小设置响应头:

response.setContentLength(length);
response.setContentType("application/vnd.ms-excel");
response.getOutputStream().write(...);
response.getOutputStream().flush();

After writing the data, the file can be deleted from the servers file system. 写入数据后,可以从服务器文件系统中删除该文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM