在旧代码中,通过呈现 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>