1、Struts2 配置
2、BaseAction<action name="exportConsume" class="consumeStatAction" method="exportConsume" >
<result name="success" type="stream">
<!-- 指定文件類型 -->
<param name="contentType">application/vnd.ms-excel</param>
<!-- 指定顯示的文件名 -->
<param name="contentDisposition">attachment;filename="${downloadFileName}"</param>
<!-- 指定文件輸入流 -->
<param name="inputName">excelFile</param>
</result>
</action>
//導出
protected InputStream excelFile;
protected String downloadFileName;//生成set 和 get方法
public InputStream getExcelFile() {
return excelFile;
}
public void setExcelFile(InputStream excelFile) {this.excelFile = excelFile;
3、Action 調用}
public String getDownloadFileName() {
String downloadFileName = (this.downloadFileName + DatetimeUtils.formatDate(new Date()) + ".xls");
try {//不同瀏覽器設置有所不同, 如果這樣設置了還不行,要兼容所有瀏覽器, 可參考”JSP、Struts2下載中文文件名亂碼問題“
downloadFileName = URLEncoder.encode(downloadFileName, "UTF-8");
//downloadFileName = new String(downloadFileName.getBytes("utf-8"), "iso-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return downloadFileName;
}
public void setDownloadFileName(String downloadFileName) {
this.downloadFileName = downloadFileName;
}
public String exportTaskKey(){
//設置下載文件的名稱
setDownloadFileName("激活碼");
//查詢條件
Map<String,String> queryMap = new HashMap<String,String>();
String sTime = getRequest().getParameter("startTime");
String eTime = getRequest().getParameter("endTime");
String bNo = getRequest().getParameter("batchNo");
String tName = getRequest().getParameter("taskName");
queryMap.put("startTime", DatetimeUtils.date2DateTime(sTime, DatetimeUtils.START_TIME));
queryMap.put("endTime", DatetimeUtils.date2DateTime(eTime, DatetimeUtils.START_TIME));
queryMap.put("batchNo", bNo);
queryMap.put("taskName", tName);
Integer appId = this.loadCurAppId();
queryMap.put("appId", appId.toString());
queryMap.put("queryType", ConfigureConstants.QUERY_TYPE_EXPORT);
Paginator pageObject = new Paginator();
List<TaskKey> list = new ArrayList<TaskKey>();
try {//查詢數據
this.paginator = this.taskKeyServiceImpl.queryListPage(pageObject, queryMap);
list = (List<TaskKey>)paginator.getPageableList();
} catch (BusinessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//構造並導出
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet_0 = wb.createSheet();
sheet_0.setDefaultColumnWidth((short)20);
sheet_0.setDefaultRowHeight((short)15);
HSSFRow row_0 = sheet_0.createRow(0);//標題,注:可構造字符串數組,動態生成標題
HSSFCell cell_0_0 = row_0.createCell((short) 0);
HSSFRichTextString hts = new HSSFRichTextString("序號");
cell_0_0.setCellValue(hts);
HSSFCell cell_0_1 = row_0.createCell((short) 1);
hts = new HSSFRichTextString("禮包");
cell_0_1.setCellValue(hts);
HSSFCell cell_0_2 = row_0.createCell((short) 2);
hts = new HSSFRichTextString("激活碼");
cell_0_2.setCellValue(hts);
HSSFCell cell_0_3 = row_0.createCell((short) 3);
hts = new HSSFRichTextString("生成批號");
cell_0_3.setCellValue(hts);
HSSFCell cell_0_4 = row_0.createCell((short) 4);
hts = new HSSFRichTextString("開始時間");
cell_0_4.setCellValue(hts);
HSSFCell cell_0_5 = row_0.createCell((short) 5);
hts = new HSSFRichTextString("結束時間");
cell_0_5.setCellValue(hts);
HSSFCell cell_0_6 = row_0.createCell((short) 6);
hts = new HSSFRichTextString("生成時間");
cell_0_6.setCellValue(hts);
//填充數據 , 跟上方的標題要保持一致
//List<Article> articleList = this.listArticles();
for(int i=0; i<list.size(); i++){
TaskKey info = list.get(i);
HSSFRow row = sheet_0.createRow(i+1);
HSSFCell cell_0 = row.createCell((short) 0);
cell_0.setCellValue(i+1);
HSSFCell cell_1 = row.createCell((short) 1);
cell_1.setCellValue(info.getTaskId());
HSSFCell cell_2 = row.createCell((short) 2);
hts = new HSSFRichTextString(info.getBarCode());
cell_2.setCellValue(hts);
HSSFCell cell_3 = row.createCell((short) 3);
hts = new HSSFRichTextString(info.getBatchNo());
cell_3.setCellValue(hts);
HSSFCell cell_4 = row.createCell((short) 4);
hts = new HSSFRichTextString(DatetimeUtils.formatDate(info.getStartTime()));
cell_4.setCellValue(hts);
HSSFCell cell_5 = row.createCell((short) 5);
hts = new HSSFRichTextString(DatetimeUtils.formatDate(info.getEndTime()));
cell_5.setCellValue(hts);
HSSFCell cell_6 = row.createCell((short) 6);
hts = new HSSFRichTextString(DatetimeUtils.formatDate(info.getCreateTime()));
cell_6.setCellValue(hts);
}
//將HSSFWorkbook對象輸出到字節數組輸出流
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
wb.write(os);
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//構造字節數字輸入流返回
excelFile = new ByteArrayInputStream(os.toByteArray());return "success";
}