0

在旧代码中,通过呈现 xhtml (jsf) 页面并将 contentType 更改为“ application/vnd.ms-excel”来完成 excel 导出,该文件由浏览器自动下载,并且可以使用 excel 打开该文件而没有任何问题。

我想更改的是文件的扩展名。下载文件时,它具有“xhtml”扩展名。我想使用适当的 excel 扩展名('xls')。

下载的文件名为summaryTransactionReports.xhtml. 我想成为summaryTransactionReports.xls

我怎样才能以最小的影响实现这一目标?

代码如下:

summaryTransactionReports.xhtml

<h:commandButton action="#{TransactionReports.createTransactionSummaryReportAction}"
                     value="#{msg.transactionReports_createReport}"
                     styleClass="form-button text-form-button"/>

TransactionReportsMBean.java

public String createTransactionSummaryReportAction()
{
    [...]
    Lots of thing here!!!
    [...]

    //Page we go to depends on the report type requested
    String nextPage;
    switch (reportType)
    {
        case REPORT_TYPE_HTML:
            nextPage = OutcomeConstants.VIEW_TRANSACTION_SUMMARY_REPORT;
            break;

        case REPORT_TYPE_EXCEL:
            nextPage = "viewExcelSummaryTransactionReport";
            break;

        case REPORT_TYPE_PRINTER_FRIENDLY:
            nextPage = OutcomeConstants.VIEW_TRANSACTION_SUMMARY_REPORT_PRINTER_FRIENDLY;
            break;

        default:
            nextPage = OutcomeConstants.VIEW_TRANSACTION_SUMMARY_REPORT;
            break;
    }

    return nextPage;
}

查看ExcelSummaryTransactionReports.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ac="http://aconitesolutions.com/jsf-custom">
    <body>

        <ui:composition template="excelMainPage.xhtml">
            <ui:define name="title">#{msg.transactionReportsSummary_Title}</ui:define>

            <ui:define name="productName">Transaction Enabler</ui:define>
            <ui:define name="content"><ui:include src="summaryTransactionReportsContent.xhtml" /></ui:define>
            <ui:define name="header">This is the header stuff</ui:define>

        </ui:composition>

    </body>
</html>

excelMainPage.xhtml

<f:view contentType="application/vnd.ms-excel"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <head>
        <title>
            <ui:insert name="title">Default title</ui:insert>
        </title>
    </head>
    <body>

    <f:loadBundle basename="TRxEMessageResource" var="msg"/>

    <!-- div for displaying tooltips ; manipulated by javascript  -->
    <div id="tooltip" style="position:absolute;visibility:hidden"></div>


    <table style="width: 100%">
        <tr>
            <td>
                <div id="header">
                    <h2>#{msg.productName}</h2>
                </div>
            </td>
        </tr>

        <tr>
            <td style="vertical-align: top; width: 100%;
                border-style: solid;
                border-bottom-width: 2px;
                border-left-width: 0px;
                border-right-width: 0px;
                border-top-width: 0px;
                border-color: #A9A9A9;
                margin-bottom: 5%">
                <div id="titleText">
                    <h2>
                        <ui:insert name="title"/>
                    </h2>

                    <ui:insert name="content">
                        <div>Content goes here</div>
                    </ui:insert>
                </div>
            </td>
        </tr>
    </table>
    </body>

</f:view>
4

1 回答 1

1

这可识别为 MSIE 特定行为。该浏览器确实不使用Content-Disposition标题中的文件名作为另存为文件名。相反,它使用出现在请求 URI 的最后一个路径中的文件名,在本例中是提交的<form action>.

最好的办法是让 JSF 将重定向发送到一个 servlet,该 servlet 又返回报告。然后,您可以按照 MSIE 期望的方式直接在 URL 中指定文件名。

ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.redirect(ec.getRequestContextPath() + "/reports/filename.xls");

servlet 可能如下所示:

@WebServlet("/reports/*")
public class ReportServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String filename = request.getPathInfo().substring(1);
        // ...
    }

}

生成报告所需的其他参数可以作为请求参数在重定向 URL 中和/或通过会话传递。

于 2013-09-16T13:33:39.587 回答