public ActionForward exportExcel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
HttpSession session = request.getSession(true);
String tile = "用户操作日志";
try{
HSSFWorkbook workbook = new HSSFWorkbook();
//添加Worksheet(不添加sheet时生成的xls文件打开时会报错)
HSSFSheet sheet = workbook.createSheet("Sheet1");
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("@"));
HSSFRow row = sheet.createRow(0);
/**
* 合并单元格
* 第一个参数:第一个单元格的行数(从0开始)
* 第二个参数:第二个单元格的行数(从0开始)
* 第三个参数:第一个单元格的列数(从0开始)
* 第四个参数:第二个单元格的列数(从0开始)
*/
sheet.addMergedRegion(new Region(0, (short)0, 0, (short)4));
HSSFCell cell = row.createCell((short)0);
// 定义单元格为字符串类型
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(tile);
row = sheet.createRow(1);
HSSFCell cell1 = row.createCell((short)0);
HSSFCell cell2 = row.createCell((short)1);
HSSFCell cell3 = row.createCell((short)2);
HSSFCell cell4 = row.createCell((short)3);
HSSFCell cell5 = row.createCell((short)4);
HSSFCell cell6 = row.createCell((short)5);
HSSFCell cell7 = row.createCell((short)6);
HSSFCell cell8 = row.createCell((short)7);
// 定义单元格为字符串类型
cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
cell2.setCellType(HSSFCell.CELL_TYPE_STRING);
cell3.setCellType(HSSFCell.CELL_TYPE_STRING);
cell4.setCellType(HSSFCell.CELL_TYPE_STRING);
cell5.setCellType(HSSFCell.CELL_TYPE_STRING);
cell6.setCellType(HSSFCell.CELL_TYPE_STRING);
cell7.setCellType(HSSFCell.CELL_TYPE_STRING);
cell8.setCellType(HSSFCell.CELL_TYPE_STRING); cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
cell3.setEncoding(HSSFCell.ENCODING_UTF_16);
cell4.setEncoding(HSSFCell.ENCODING_UTF_16);
cell5.setEncoding(HSSFCell.ENCODING_UTF_16);
cell6.setEncoding(HSSFCell.ENCODING_UTF_16);
cell7.setEncoding(HSSFCell.ENCODING_UTF_16);
cell8.setEncoding(HSSFCell.ENCODING_UTF_16); // 在单元格中输入数据
cell1.setCellValue("序号");
cell2.setCellValue("账号");
cell3.setCellValue("操作类型");
cell4.setCellValue("一级模块");
cell5.setCellValue("二级模块");
cell6.setCellValue("操作对象");
cell7.setCellValue("操作时间");
cell8.setCellValue("操作结果"); //sheet.setDefaultColumnWidth((short) 20); //设置默认宽度
sheet.setColumnWidth((short) 4, (short) 5120);
sheet.setColumnWidth((short) 5, (short) 10240);
sheet.setColumnWidth((short) 6, (short) 5120); HttpSession sessionold = request.getSession(true);
AccountInfo accountInfo = (AccountInfo) sessionold.getAttribute("accountInfo");
OperationLogForm logForm = (OperationLogForm) form;
LogService logSer = new LogService();
String accountName = logForm.getAccountName();
Vector<Hashtable<String, String>> logList = logSer.queryLog(logForm,accountName, -1,accountInfo.getAccountType());
Hashtable data = null;
for (int i = 0; i < logList.size(); i++){
data = logList.get(i);
row = sheet.createRow((short)(i + 2));
cell1 = row.createCell((short)0);
cell2 = row.createCell((short)1);
cell3 = row.createCell((short)2);
cell4 = row.createCell((short)3);
cell5 = row.createCell((short)4);
cell6 = row.createCell((short)5);
cell7 = row.createCell((short)6);
cell8 = row.createCell((short)7);
// 定义单元格为字符串类型
cell1.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cell2.setCellType(HSSFCell.CELL_TYPE_STRING);
cell3.setCellType(HSSFCell.CELL_TYPE_STRING);
cell4.setCellType(HSSFCell.CELL_TYPE_STRING);
cell5.setCellType(HSSFCell.CELL_TYPE_STRING);
cell6.setCellType(HSSFCell.CELL_TYPE_STRING);
cell7.setCellType(HSSFCell.CELL_TYPE_STRING);
cell8.setCellType(HSSFCell.CELL_TYPE_STRING); cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
cell3.setEncoding(HSSFCell.ENCODING_UTF_16);
cell4.setEncoding(HSSFCell.ENCODING_UTF_16);
cell5.setEncoding(HSSFCell.ENCODING_UTF_16);
cell6.setEncoding(HSSFCell.ENCODING_UTF_16);
cell7.setEncoding(HSSFCell.ENCODING_UTF_16);
cell8.setEncoding(HSSFCell.ENCODING_UTF_16); cell1.setCellValue(i+1);
cell2.setCellValue(data.get("username").toString());
if("1".equals(data.get("operatetype").toString())){
cell3.setCellValue("新增");
}else if("2".equals(data.get("operatetype").toString())){
cell3.setCellValue("修改");
}else if("4".equals(data.get("operatetype").toString())){
cell3.setCellValue("审批");
}else if("5".equals(data.get("operatetype").toString())){
cell3.setCellValue("否决");
}else if("6".equals(data.get("operatetype").toString())){
cell3.setCellValue("分配角色");
}else if("7".equals(data.get("operatetype").toString())){
cell3.setCellValue("分配资源");
}else{
cell3.setCellValue("删除");
}
cell4.setCellValue(data.get("fmodulename").toString());
cell5.setCellValue(data.get("smodulename").toString());
cell6.setCellValue(data.get("operobj").toString());
cell7.setCellValue(data.get("operatetime1").toString());
if("0".equals(data.get("operateresult").toString())){
cell8.setCellValue("成功");
}else{
cell8.setCellValue("失败");
}
}
response.reset();
response.setContentType("application/octet-stream;charset=GB2312");
response.setHeader("Content-Disposition", "attachment; filename=\"OperationLog.xls\"");
OutputStream out = response.getOutputStream();
workbook.write(out);
out.flush();
out.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}

最新文章

  1. ASP.Net MVC开发基础学习笔记:五、区域、模板页与WebAPI初步
  2. 微软Edge 内嵌的JavaScript 引擎即将开源
  3. linux下搭建svn代码库
  4. Linux User&#39;s Manual IOSTAT
  5. Asynchronous socket communication
  6. 深入理解JAVA多态原理
  7. DevOps探索
  8. VueJS实现一个货币结算自定义控件
  9. selenium+java破解极验滑动验证码
  10. spotlight 索引重建
  11. HDU-2222文字检索
  12. .net core控制台应用程序初识
  13. C# 正则表达式应用
  14. MySQL、Mariadb 复制原理
  15. solrj索引操作
  16. using directive 使用指令,与using declaration使用声明。
  17. ES6-Array
  18. BZOJ3052:[WC2013]糖果公园(树上莫队)
  19. hadoop 安装笔记
  20. Java网络编程和NIO详解3:IO模型与Java网络编程模型

热门文章

  1. redis mongodb持久化的方式
  2. Scratch-介绍“克隆”
  3. LeetCode 1103. Distribute Candies to People
  4. office2019激活码 最新各个版本激活码
  5. Nginx与负载均衡
  6. 初学java1 数据类型
  7. Python考试_第一次
  8. [书籍翻译] 《JavaScript并发编程》第三章 使用Promises实现同步
  9. oracle exists和 not exists 的用法
  10. Endless looping of packets in TCP/IP networks (Routing Loops)