1、在action中添加字段:filename
private String filename;
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
2、在action中写个返回 InputStream 的public方法,名称以get开头:getImportActLog。
</pre><p><span style="font-size:18px"><strong><span style="color:#ff0000; background-color:rgb(255,255,255)"></span></strong></span></p><p><span style="font-size:18px"><strong><span style="color:#ff0000; background-color:rgb(255,255,255)"><span style="white-space:pre"></span></span></strong></span></p><pre code_snippet_id="610206" snippet_file_name="blog_20150302_2_7951902" name="code" class="java">/**
*
* 方法名: getImportActLog
* 方法描述: (获取导出知识操作日志)
* 创建人:liyafei
* 修改时间:2015-3-2 上午11:35:13
* 参数 @return 参数说明
* 返回类型 InputStream 返回类型
* @throws
*/
public InputStream getImportActLog(){
// 第二步:构建excel表格,封装数据到excel
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
HSSFCellStyle setBorder = workbook.createCellStyle();
//设置边框:
setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
setBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
setBorder.setWrapText(true);//设置自动换行
HSSFRow row = sheet.createRow(0);// 创建第一行
HSSFCell cell = null;// 第一列
cell = row.createCell(0); // 创建第1列
cell.setCellValue("序号");
cell = row.createCell(1); // 创建第2列
cell.setCellValue("知识标题");
cell = row.createCell(2); // 创建第3列
cell.setCellValue("操作用户");
cell = row.createCell(3); // 创建第4列
cell.setCellValue("操作类型");
cell = row.createCell(4); // 创建第5列
cell.setCellValue("操作时间");
cell = row.createCell(5); // 创建第6列
cell.setCellValue("操作结果");
cell = row.createCell(6); // 创建第7列
cell.setCellValue("详细描述");
//查询数据
List<HashMap<String, String>> data = knowledgeActLogService.getShowList(model);
for (int i =0 ;i<data.size();i++){
HashMap<String, String> map = data.get(i);
row = sheet.createRow(i+1);// 创建第i+1行
cell = row.createCell(0);// 创建第一列
cell.setCellValue(i + 1);
cell = row.createCell(1);// 创建第二列
cell.setCellValue(map.get("K_NAME"));
cell = row.createCell(2);
cell.setCellValue(map.get("STAFF_NAME"));
cell = row.createCell(3);// 创建第三列
cell.setCellValue(map.get("ACTTYPE_NAME"));
cell = row.createCell(4);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String acttime = format.format(map.get("ACT_TIME"));
if(acttime.length()>19){
acttime = acttime.substring(0, 19);
}
cell.setCellValue(acttime);
cell = row.createCell(5);// 创建第四列
Object ob = map.get("ISSUCCESS");
String issuccess = ob.toString().trim();
if(issuccess.equals("1")){
cell.setCellValue("成功");
}else {
cell.setCellValue("失败");
}
cell = row.createCell(6);
cell.setCellValue(map.get("ACT_DESC"));
}
//调整列宽
for (int i = 0 ;i<row.getLastCellNum();i++){
sheet.autoSizeColumn((short)i);
}
// 第三步:写入输出流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
workbook.write(baos);// 写入
} catch (IOException e) {
e.printStackTrace();
}
byte[] ba = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(ba);
return bais;
}
3、在action中写个方法用于处理导出:getImport
/**
*
* 方法名: getImport
* 方法描述: (导出知识操作日志)
* 创建人:liyafei
* 修改时间:2015-3-2 上午10:47:08
* 参数 @return
* 参数 @throws Exception 参数说明
* 返回类型 String 返回类型
* @throws
*/
public String getImport() throws Exception {
this.setFilename("知识操作日志.xls");
filename = URLEncoder.encode(filename, "utf-8");
return "import";
}
4、在配置action文件
<result name="import" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="contentDisposition">attachment;filename=${filename}</param>
<param name="inputName">importActLog</param>
</result>