JSF 1.2-导出到Excel并重命名文件
在旧式代码中,excel导出是通过呈现xhtml(jsf)页面并将contentType更改为“JSF 1.2-导出到Excel并重命名文件,jsf,jsf-1.2,Jsf,Jsf 1.2,在旧式代码中,excel导出是通过呈现xhtml(jsf)页面并将contentType更改为“application/vnd.ms excel”来完成的。该文件由浏览器自动下载,并且可以使用excel毫无问题地打开该文件 我想更改的是文件的扩展名。下载文件时,其扩展名为“xhtml”。我想使用适当的excel扩展名(“xls”) 下载的文件名为summaryTransactionReports.xhtml。我想成为summaryTransactionReports.xls 如何以最小的影响实现
application/vnd.ms 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"/>
<?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>
<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>
viewExcelSummaryTransactionReports.xhtml
<h:commandButton action="#{TransactionReports.createTransactionSummaryReportAction}"
value="#{msg.transactionReports_createReport}"
styleClass="form-button text-form-button"/>
<?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>
<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>
#{msg.transactionReportsSummary_Title}
事务启用码
这是标题
excelMainPage.xhtml
<h:commandButton action="#{TransactionReports.createTransactionSummaryReportAction}"
value="#{msg.transactionReports_createReport}"
styleClass="form-button text-form-button"/>
<?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>
<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>
默认标题
#{msg.productName}
内容在这里
这可以识别为MSIE特定的行为。该浏览器确实没有将内容处置
标题中的文件名用作另存为文件名。相反,它使用请求URI的最后一个路径中显示的文件名,在本例中,它是提交的
的URL
您最好的选择是让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中传递和/或通过会话传递。感谢您的回答,一如既往,非常快速!我现在正在做别的事情,所以我不能尝试你的建议。我希望过几天再回来找你。再次感谢你的时间。