Struts2 導出 Excel 報表
本文內容爲
Html button
綁定按鈕事件downLoadIframe
調取下載接口Struts2 Action
業務調用及封裝數據,構造 Excel 報表
html
按鈕事件
<input type="button" class="button" value="導出" onclick="exportExcel()" />
js
使用downLoadIframe
調用接口下載Excel文檔
iframe
標籤會創建包含另外一個文檔的內聯框架,跟當前window
框架是父子關係,利用iframe
,我們可以處理異步無刷新上傳、下載文件
<script type="text/javascript">
function exportExcel(){
var params = getConParams();
if(confirm("確定導出?")){
var url = 'exportContractList.action?' + params;
var ifr;
if (document.getElementById('downLoadIframe') == null) {
ifr = document.createElement("iframe");
ifr.id = "downLoadIframe";
ifr.style.display = "none";
document.body.appendChild(ifr);
}
else {
ifr = document.getElementById('downLoadIframe');
}
ifr.src = url;
}
}
</script>
Action
- 調取
service
,獲取數據 - 構造Excel
- 定義列名
- 封裝數據
public class UserAction extends ManagerBaseAction{
public void exportList() throws Exception{
String name = this.getRequest().getParameter("name");
String gender = this.getRequest().getParameter("gender");
Property values = new Property();
values.put("name", actname);
values.put("gender", gender);
json = userService.listItem(values);
List<Property> list = (List<Property>) JSONArray.toCollection(json.getJSONArray("list"), Property.class);
exportExcel(list);
}
private void exportExcel(List<Property> list) throws Exception{
String sheetName = "sheet";
String excelName = sheetName + ".xlsx";
// 列名
List<String> columnNames = getColumnNames();
// 結果封裝
List<List<String>> allRows = getAllRows(list);
OutputStream out = getResponse().getOutputStream();
renderExportFile(excelName, "application/vnd.ms-excel");
try {
Workbook wb = new SXSSFWorkbook();
ExcelUtil.exportExcel2007(wb, sheetName, columnNames, allRows, out);
wb.write(out);
out.flush();
} catch (Exception e) {
logger.warn("exportExcel2007 error.", e);
} finally {
out.close();
}
}
private List<String> getColumnNames() {
List<String> columnNames = new ArrayList<>();
columnNames.add("名稱");
columnNames.add("性別");
columnNames.add("暱稱");
columnNames.add("狀態");
return columnNames;
}
private List<List<String>> getAllRows(List<Property> list) {
List<List<String>> resList = new ArrayList<>();
for(Property p:list){
List<String> rows = new ArrayList<>();
rows.add(p.get("name"));
rows.add(p.get("gender"));
rows.add(p.get("nick"));
rows.add(p.get("status"));
resList.add(rows);
}
return resList;
}
}
BaseAction
response
設置Header
及Content-Type
public void renderExportFile(String fileName, String contentType) throws Exception {
HttpServletResponse response = getResponse();
response.setContentType(contentType);
response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"), "iso8859-1") + "");
response.setHeader("Cache-Control", "private");
response.flushBuffer();
}
Util
Excel
導出工具類
public static OutputStream exportExcel2007(Workbook wb, String sheetName, List<String> columnNames, List<List<String>> allRows, OutputStream out) throws Exception{
CreationHelper createHelper = wb.getCreationHelper();
Sheet sheet = wb.createSheet(sheetName);
short index = 0;
while(index < columnNames.size()) {
sheet.setColumnWidth(index, 6500);
index++;
}
Row row;
Cell cell;
row = sheet.createRow(0);
for(int j = 0; j < columnNames.size(); j ++){
cell = row.createCell(j);
cell.setCellValue(createHelper.createRichTextString(columnNames.get(j)));
}
for(int i = 1; i <= allRows.size(); i ++){
row = sheet.createRow(i);
List<String> rowData = allRows.get(i - 1);
for(int j = 0; j < rowData.size(); j ++){
cell = row.createCell(j);
String value = rowData.get(j);
cell.setCellValue(createHelper.createRichTextString(value));
}
}
return out;
}