java新建excel文件导出(HSSFWorkbook)

  1 public ActionForward exportExcel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
  2         HttpSession session = request.getSession(true);
  3   4         String tile = "用户操作日志";
  5         try{
  6             HSSFWorkbook workbook = new HSSFWorkbook();
  7             //添加Worksheet(不添加sheet时生成的xls文件打开时会报错)
  8             HSSFSheet sheet = workbook.createSheet("Sheet1");
  9             HSSFCellStyle cellStyle = workbook.createCellStyle();
 10             cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("@"));
 11             HSSFRow row = sheet.createRow(0);
 12             /**  
 13              * 合并单元格  
 14              *    第一个参数:第一个单元格的行数(从0开始)  
 15              *    第二个参数:第二个单元格的行数(从0开始)  
 16              *    第三个参数:第一个单元格的列数(从0开始)  
 17              *    第四个参数:第二个单元格的列数(从0开始)  
 18              */
 19             sheet.addMergedRegion(new Region(0, (short)0, 0, (short)4)); 
 20             HSSFCell cell = row.createCell((short)0);
 21             // 定义单元格为字符串类型
 22             cell.setCellType(HSSFCell.CELL_TYPE_STRING);
 23             cell.setEncoding(HSSFCell.ENCODING_UTF_16);
 24             cell.setCellValue(tile);
 25             row = sheet.createRow(1);
 26             HSSFCell cell1 = row.createCell((short)0);
 27             HSSFCell cell2 = row.createCell((short)1);
 28             HSSFCell cell3 = row.createCell((short)2);
 29             HSSFCell cell4 = row.createCell((short)3);
 30             HSSFCell cell5 = row.createCell((short)4);
 31             HSSFCell cell6 = row.createCell((short)5);
 32             HSSFCell cell7 = row.createCell((short)6);
 33             HSSFCell cell8 = row.createCell((short)7);
 34             // 定义单元格为字符串类型
 35             cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
 36             cell2.setCellType(HSSFCell.CELL_TYPE_STRING);
 37             cell3.setCellType(HSSFCell.CELL_TYPE_STRING);
 38             cell4.setCellType(HSSFCell.CELL_TYPE_STRING);
 39             cell5.setCellType(HSSFCell.CELL_TYPE_STRING);
 40             cell6.setCellType(HSSFCell.CELL_TYPE_STRING);
 41             cell7.setCellType(HSSFCell.CELL_TYPE_STRING);
 42             cell8.setCellType(HSSFCell.CELL_TYPE_STRING);
 43            
 44 
 45             cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
 46             cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
 47             cell3.setEncoding(HSSFCell.ENCODING_UTF_16);
 48             cell4.setEncoding(HSSFCell.ENCODING_UTF_16);
 49             cell5.setEncoding(HSSFCell.ENCODING_UTF_16);
 50             cell6.setEncoding(HSSFCell.ENCODING_UTF_16);
 51             cell7.setEncoding(HSSFCell.ENCODING_UTF_16);
 52             cell8.setEncoding(HSSFCell.ENCODING_UTF_16);
 53            
 54 
 55             // 在单元格中输入数据
 56             cell1.setCellValue("序号");
 57             cell2.setCellValue("账号");
 58             cell3.setCellValue("操作类型");
 59             cell4.setCellValue("一级模块");
 60             cell5.setCellValue("二级模块");
 61             cell6.setCellValue("操作对象");
 62             cell7.setCellValue("操作时间");
 63             cell8.setCellValue("操作结果");
 64             
 65             //sheet.setDefaultColumnWidth((short) 20);  //设置默认宽度
 66             sheet.setColumnWidth((short) 4, (short) 5120);  
 67             sheet.setColumnWidth((short) 5, (short) 10240);  
 68             sheet.setColumnWidth((short) 6, (short) 5120); 
 69             
 70             HttpSession sessionold = request.getSession(true);
 71             AccountInfo accountInfo = (AccountInfo) sessionold.getAttribute("accountInfo");    
 72             OperationLogForm logForm = (OperationLogForm) form;
 73             LogService logSer = new LogService();
 74             String accountName = logForm.getAccountName();
 75             Vector<Hashtable<String, String>> logList = logSer.queryLog(logForm,accountName, -1,accountInfo.getAccountType());
 76             Hashtable data = null;
 77             for (int i = 0; i < logList.size(); i++){
 78                 data = logList.get(i);
 79                 row = sheet.createRow((short)(i + 2));
 80                 cell1 = row.createCell((short)0);
 81                 cell2 = row.createCell((short)1);
 82                 cell3 = row.createCell((short)2);
 83                 cell4 = row.createCell((short)3);
 84                 cell5 = row.createCell((short)4);
 85                 cell6 = row.createCell((short)5);
 86                 cell7 = row.createCell((short)6);
 87                 cell8 = row.createCell((short)7);
 88                 // 定义单元格为字符串类型
 89                 cell1.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
 90                 cell2.setCellType(HSSFCell.CELL_TYPE_STRING);
 91                 cell3.setCellType(HSSFCell.CELL_TYPE_STRING);
 92                 cell4.setCellType(HSSFCell.CELL_TYPE_STRING);
 93                 cell5.setCellType(HSSFCell.CELL_TYPE_STRING);
 94                 cell6.setCellType(HSSFCell.CELL_TYPE_STRING);
 95                 cell7.setCellType(HSSFCell.CELL_TYPE_STRING);
 96                 cell8.setCellType(HSSFCell.CELL_TYPE_STRING);
 97                
 98 
 99                 cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
100                 cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
101                 cell3.setEncoding(HSSFCell.ENCODING_UTF_16);
102                 cell4.setEncoding(HSSFCell.ENCODING_UTF_16);
103                 cell5.setEncoding(HSSFCell.ENCODING_UTF_16);
104                 cell6.setEncoding(HSSFCell.ENCODING_UTF_16);
105                 cell7.setEncoding(HSSFCell.ENCODING_UTF_16);
106                 cell8.setEncoding(HSSFCell.ENCODING_UTF_16);
107                 
108                 cell1.setCellValue(i+1);
109                 cell2.setCellValue(data.get("username").toString());
110                 if("1".equals(data.get("operatetype").toString())){
111                     cell3.setCellValue("新增");
112                 }else if("2".equals(data.get("operatetype").toString())){
113                     cell3.setCellValue("修改");
114                 }else if("4".equals(data.get("operatetype").toString())){
115                     cell3.setCellValue("审批");
116                 }else if("5".equals(data.get("operatetype").toString())){
117                     cell3.setCellValue("否决");
118                 }else if("6".equals(data.get("operatetype").toString())){
119                     cell3.setCellValue("分配角色");
120                 }else if("7".equals(data.get("operatetype").toString())){
121                     cell3.setCellValue("分配资源");
122                 }else{
123                     cell3.setCellValue("删除");
124                 }
125                 cell4.setCellValue(data.get("fmodulename").toString());
126                 cell5.setCellValue(data.get("smodulename").toString());
127                 cell6.setCellValue(data.get("operobj").toString());
128                 cell7.setCellValue(data.get("operatetime1").toString());
129                 if("0".equals(data.get("operateresult").toString())){
130                     cell8.setCellValue("成功");
131                 }else{
132                     cell8.setCellValue("失败");
133                 }
134             }
135             response.reset();
136             response.setContentType("application/octet-stream;charset=GB2312");
137             response.setHeader("Content-Disposition", "attachment; filename=\"OperationLog.xls\"");
138             OutputStream out = response.getOutputStream();
139             workbook.write(out);
140             out.flush();
141             out.close();
142         }
143         catch (Exception e)
144         {
145             e.printStackTrace();
146         }
147         return null;
148     }

猜你喜欢

转载自www.cnblogs.com/penghq/p/9225129.html