首页 > 代码库 > Struts2 导出excel完整版
Struts2 导出excel完整版
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";
}
Struts2 导出excel完整版