java 如何在Struts2结果中返回excel?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1003892/
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-29 14:45:34  来源:igfitidea点击:

how to return excel in Struts2 result?

javastruts2

提问by Omnipresent

I am trying to return an Excel sheet from my struts2 action class.

我正在尝试从我的 struts2 操作类返回 Excel 表。

I am not sure what result-type should I be using? Has anyone tried to return an excel from struts2 action class?
I would like the user to be presented with open/save/cancel dialog box

我不确定应该使用什么结果类型?有没有人试图从 struts2 操作类返回一个 excel?
我希望向用户显示打开/保存/取消对话框

回答by rlovtang

Omnipresent covered what you need in struts.xml. I'm adding an example with the Action as well:

Omnipresent 涵盖了您在 struts.xml 中需要的内容。我还添加了一个带有 Action 的示例:

InputStream excelStream
String contentDisposition
String documentFormat = "xlsx"

String excel() {

    ServletContext servletContext = ServletActionContext.getServletContext()
    String filePath = servletContext.getRealPath("/WEB-INF/template/excel/mytemplate.${documentFormat}")

    File file = new File(filePath)
    Workbook wb = WorkbookFactory.create(new FileInputStream(file))

    Sheet sheet = wb.getSheetAt(0)

<write to excel file>

    ByteArrayOutputStream baos = new ByteArrayOutputStream()
    wb.write(baos)
    excelStream = new ByteArrayInputStream(baos.toByteArray())
    contentDisposition = "filename=\"myfilename.${documentFormat}\""

    return SUCCESS
}

String getExcelContentType() {
    return documentFormat == "xlsx" ? "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" : "application/vnd.ms-excel"
}

I'm using the poi model: org.apache.poi.ss.usermodel.

我正在使用 poi 模型:org.apache.poi.ss.usermodel。

You can replace "xlsx" with "xls" if you want.

如果需要,您可以将“xlsx”替换为“xls”。

struts.xml:

struts.xml:

<action name="myaction" class="com.example.MyAction" method="excel">
        <result type="stream">
            <param name="contentType">${excelContentType}</param>
            <param name="inputName">excelStream</param>
            <param name="contentDisposition">contentDisposition</param>
            <param name="bufferSize">1024</param>
        </result>
    </action>

(add semicolons and stuff to translate to valid Java)

(添加分号和内容以转换为有效的 Java)

回答by Omnipresent

You can utilize the Stream Resulttype

您可以使用Stream Result类型

an Example will look like this:

示例如下所示:

<result name="excel" type="stream">
    <param name="contentType">application/vnd.ms-excel</param>
    <param name="inputName">excelStream</param>
    <param name="contentDisposition">attachment; filename="${fileName}"</param>
    <param name="bufferSize">1024</param>
    <param name="contentLength">${contentLength}</param>
 </result>

excelStreamwill be a method in your action class, contentLengthwill be length of the stream, fileNamewill be a getter which will return back the name of the file.

excelStream将是您的操作类中的一个方法,contentLength将是流的长度,fileName将是一个将返回文件名称的 getter。