1.spring控制器繼承的是AbstractJExcelView,並實現其buildExcelDocument方法。
2.下面是具體實現:
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.web.servlet.view.document.AbstractJExcelView;
import com.smartpay.commons.base.SystemException;
public class ListForExcelView extends AbstractJExcelView {
/**
* 不同交易類型對應標題欄信息映射
*/
private Map<String, ExcelColVO[]> excelColVOMap;
/**
* 頭信息在數據模型中的名稱
*/
private String headInfoName;
/**
* Excel頭信息數組
*/
private ExcelColVO[] headInfoRowArr;
/**
* 信息列表在數據模型中的名稱
*/
private String listNameInMode;
/**
* 列表類型名在數據模型中的名稱
*/
private String listTypeNameInMode;
/**
* 下載文件的名稱
*/
private String downloadFileName;
/**
* Excel表名稱
*/
private String sheetName;
@SuppressWarnings("unchecked")
@Override
protected void buildExcelDocument(Map mode, WritableWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
//取得交易信息
String transactionType = (String) mode.get(listTypeNameInMode);
List transactionInfoList = (List) mode.get(listNameInMode);
if (transactionInfoList == null) {
throw new SystemException("未找到查詢到的交易列表信息");
}
//設置文件響應信息
String showFileName = URLEncoder.encode(downloadFileName + ".xls", "UTF-8");
showFileName = new String(showFileName.getBytes("iso8859-1"), "gb2312");
response.setContentType("application/msexcel");// 定義輸出類型
response.setHeader("Pragma", "public");
response.setHeader("Cache-Control", "max-age=30");
response.setHeader("Content-disposition", "attachment; filename=" + new String(showFileName.getBytes("gb2312"), "iso8859-1"));
//初始化參數
WritableSheet sheet = workbook.createSheet(sheetName, 1);
int i = 0;//行計數器
//輸出Excel頭信息
WritableFont wfTitle = new WritableFont(WritableFont.ARIAL, 12);
WritableCellFormat contentFormat = new WritableCellFormat(wfTitle);
contentFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
if (headInfoName != null && headInfoRowArr != null) {
Object headInfoVO = mode.get(headInfoName);
for (int j = 0; j < headInfoRowArr.length; j++) {
ExcelColVO headInfoRow = headInfoRowArr[j];
String propertyValue = BeanUtils.getProperty(headInfoVO, headInfoRow.getColProperty());
sheet.addCell(new Label(0, i, headInfoRow.getColTitle(), contentFormat));
sheet.addCell(new Label(1, i, propertyValue, contentFormat));
i++;
}
i++;
}
//輸出標題欄信息
ExcelColVO[] excelColVOList = getExcelColVOArr(transactionType);
WritableFont wf = new WritableFont(WritableFont.ARIAL, 16, WritableFont.BOLD, false);
WritableCellFormat titleFormat = new WritableCellFormat(wf);
for (int j = 0; j < excelColVOList.length; j++) {
sheet.addCell(new Label(j, i, excelColVOList[j].getColTitle(), titleFormat));
}
//輸出內容體
for (Object transactionInfoVO : transactionInfoList) {
i++;
for (int j = 0; j < excelColVOList.length; j++) {
String propertyValue = BeanUtils.getProperty(transactionInfoVO, excelColVOList[j].getColProperty());
sheet.addCell(new Label(j, i, propertyValue, contentFormat));
}
}
}
/**
* 取得列信息
*
* @param transactionType
* @return
*/
private ExcelColVO[] getExcelColVOArr(String transactionType) {
ExcelColVO[] excelColVOList = excelColVOMap.get(transactionType);
if (excelColVOList == null) {
excelColVOList = excelColVOMap.get(null);//幫助類似於[TRANSACTION_DEPOSIT ...]等使用抽象交易信息的顯示
if (excelColVOList == null) {
throw new SystemException("無法取得交易類型[" + transactionType + "]對應的標題欄信息!");
}
}
return excelColVOList;
}
/**
* @return the excelColVOMap
*/
public Map<String, ExcelColVO[]> getExcelColVOMap() {
return excelColVOMap;
}
/**
* @param excelColVOMap the excelColVOMap to set
*/
public void setExcelColVOMap(Map<String, ExcelColVO[]> excelColVOMap) {
this.excelColVOMap = excelColVOMap;
}
/**
* @return the listNameInMode
*/
public String getListNameInMode() {
return listNameInMode;
}
/**
* @param listNameInMode the listNameInMode to set
*/
public void setListNameInMode(String listNameInMode) {
this.listNameInMode = listNameInMode;
}
/**
* @return the listTypeNameInMode
*/
public String getListTypeNameInMode() {
return listTypeNameInMode;
}
/**
* @param listTypeNameInMode the listTypeNameInMode to set
*/
public void setListTypeNameInMode(String listTypeNameInMode) {
this.listTypeNameInMode = listTypeNameInMode;
}
/**
* @return the downloadFileName
*/
public String getDownloadFileName() {
return downloadFileName;
}
/**
* @param downloadFileName the downloadFileName to set
*/
public void setDownloadFileName(String downloadFileName) {
this.downloadFileName = downloadFileName;
}
/**
* @return the sheetName
*/
public String getSheetName() {
return sheetName;
}
/**
* @param sheetName the sheetName to set
*/
public void setSheetName(String sheetName) {
this.sheetName = sheetName;
}
/**
* @return the headInfoColArr
*/
public ExcelColVO[] getHeadInfoRowArr() {
return headInfoRowArr;
}
/**
* @param headInfoColArr the headInfoColArr to set
*/
public void setHeadInfoRowArr(ExcelColVO[] headInfoColArr) {
this.headInfoRowArr = headInfoColArr;
}
/**
* @return the headInfoName
*/
public String getHeadInfoName() {
return headInfoName;
}
/**
* @param headInfoName the headInfoName to set
*/
public void setHeadInfoName(String headInfoName) {
this.headInfoName = headInfoName;
}
}
import java.beans.PropertyEditorSupport;
import com.smartpay.commons.base.SystemException;
import com.smartpay.commons.util.StringUtil;
public class ExcelColVOPropertyEditor extends PropertyEditorSupport {
/* (non-Javadoc)
* @see java.beans.PropertyEditorSupport#setAsText(java.lang.String)
*/
@Override
public void setAsText(String excelColInfo) throws IllegalArgumentException {
//驗證參數是否爲空
if (StringUtil.isEmptyStr(excelColInfo)) {
throw new SystemException("空信息無法轉換爲ExcelColVO對象");
}
//驗證參數格式是否正確(是否包含"=")
if (excelColInfo.indexOf('=') == -1) {
throw new SystemException("構造ExcelColVO對象的原信息格式爲(例:交易號=transactionId)樣式,您提供的信息有誤[" + excelColInfo + "]");
}
//驗證參數格式是否正確
String[] excelColInfoArr = excelColInfo.split("=");
if (excelColInfoArr.length != 2) {
throw new SystemException("信息[" + excelColInfo + "]使用'='分割出的字符數組長度不爲2!");
}
//定位各個域的值
String colTitle = excelColInfoArr[0];
String colProperty = excelColInfoArr[1];
//構造對象
ExcelColVO excelColVO = new ExcelColVO();
excelColVO.setColProperty(colProperty);
excelColVO.setColTitle(colTitle);
//設置對象信息
setValue(excelColVO);
}
}
public class ExcelColVO {
/**
* 列名稱
*/
private String colTitle;
/**
* 從行對象取得列信息的屬性
*/
private String colProperty;
/**
* @return the colName
*/
public String getColTitle() {
return colTitle;
}
/**
* @param colName the colName to set
*/
public void setColTitle(String colName) {
this.colTitle = colName;
}
/**
* @return the colProperties
*/
public String getColProperty() {
return colProperty;
}
/**
* @param colProperties the colProperties to set
*/
public void setColProperty(String colProperties) {
this.colProperty = colProperties;
}
}
spring 相關配置----------------------------------------------------------------------------------------------------------------------------------
<bean name="transactionQueryViewForExcel" class="com.smartpay.websharetools.springmvcview.excel.ListForExcelView">
<property name="downloadFileName" value="交易查詢"></property>
<property name="sheetName" value="交易查詢"></property>
<property name="listTypeNameInMode" value="transactionType"></property>
<property name="listNameInMode" value="transactionInfoList"></property>
<property name="excelColVOMap">
<map>
<entry>
<key>
<null></null>
</key>
<list >
<value>交易號=transactionId</value>
<value>創建時間=beginTime</value>
<value>結束時間=endTime</value>
<value>交易類型=transactionTypeName</value>
<value>交易對方=otherSideName</value>
<value>金額(元)=transactionAmountForShow</value>
<value>交易狀態=transactionStatusName</value>
</list>
</entry>
<entry key="TRANSACTION_TOPUP">
<list>
<value>交易號=id</value>
<value>充值類型=topupType</value>
<value>提交時間=checkinTime</value>
<value>完成時間=operatorTime</value>
<value>被充號碼=topupMp</value>
<value>請求(元)=topupAmount</value>
<value>實際(元)=factTopupAmount</value>
<value>支付(元)=transactionAmount</value>
<value>方式=channelTypeName</value>
<value>狀態=statusName</value>
</list>
</entry>
<entry key="TRANSACTION_PAYMENT">
<list>
<value>交易號=transactionId</value>
<value>訂單號=merchantTxSeqNo</value>
<value>商戶名稱=merchantName</value>
<value>商品名稱=itemName</value>
<value>交易開始時間=beginTime</value>
<value>交易結束時間=endTime</value>
<value>支付手機號=userMp</value>
<value>交易金額=transactionAmount</value>
<value>退款金額=paybackAmount</value>
<value>交易狀態=transactionStatusName</value>
</list>
</entry>
</map>
</property>
</bean>
<bean name="transferQueryViewForExcel" class="com.smartpay.websharetools.springmvcview.excel.ListForExcelView">
<property name="downloadFileName" value="賬務明細查詢"></property>
<property name="sheetName" value="賬務明細查詢"></property>
<property name="listTypeNameInMode" value="aaa"></property>
<property name="listNameInMode" value="transferQueryTOList"></property>
<property name="excelColVOMap">
<map>
<entry>
<key>
<null></null>
</key>
<list >
<value>日期和時間=transferTime</value>
<value>交易號=transactionId</value>
<value>交易對方=otherSideName</value>
<value>轉賬類型=transferTypeName</value>
<value>收入(元)=receiptAmountForShow</value>
<value>支出(元)=payoutAmountForShow</value>
<value>餘額(元)=balance</value>
</list>
</entry>
</map>
</property>
</bean>