java导入org.apache_java使用org.apache.poi读取与保存EXCEL文件

1 XSSFWorkbook wb=newXSSFWorkbook();2 DeviceAlarmUtil outputResult = newDeviceAlarmUtil();3 wb = outputResult.outConfigExceptionResult(wb, "配置告警信息", configAlarmList);4 response.setContentType("application/msexcel");5 response.setHeader( "Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1") );6

7 ServletOutputStream os = null;8 try{9 os =response.getOutputStream();10 //写到输出流

11 wb.write(os);12} catch(FileNotFoundException e) {13 e.printStackTrace();14 } catch(IOException e) {15 e.printStackTrace();16 } catch(Throwable e) {17 e.printStackTrace();18 } finally{19 if (wb != null) {20 try{21 wb.close();22 } catch(IOException e) {23 throw new Exception("IO错误,无法导出结果");24 }25 }26 if (os != null) {27 try{28 os.flush();29 os.close();30 } catch(IOException e) {31 throw new Exception("IO错误,无法导出结果");32 }33 }34 }35

36

37 public XSSFWorkbook outConfigExceptionResult(XSSFWorkbook wb, String sheetName, List>sheetResult){38 XSSFSheet sheet=wb.createSheet(sheetName);39 XSSFRow row=sheet.createRow(0);40 CellStyle cellStyle =wb.createCellStyle();41 //设置这些样式

42 cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);43 cellStyle.setFillForegroundColor(HSSFColor.LIGHT_ORANGE.index);44 cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);45 cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);46 cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);47 cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);48 cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);49 XSSFFont font =wb.createFont();50 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);51 cellStyle.setFont(font);52

53 XSSFCell cell = row.createCell((short) 0);54 cell.setCellValue("客户名称");55 cell.setCellStyle(cellStyle);56 sheet.setColumnWidth(0, 5500);57 cell = row.createCell((short) 1);58 cell.setCellValue("AC MAC");59 cell.setCellStyle(cellStyle);60 sheet.setColumnWidth(1, 5000);61 cell = row.createCell((short) 2);62 cell.setCellValue("检查项");63 cell.setCellStyle(cellStyle);64 sheet.setColumnWidth(2, 5000);65 cell = row.createCell((short) 3);66 cell.setCellValue("检查次数");67 cell.setCellStyle(cellStyle);68 sheet.setColumnWidth(3, 5000);69 cell = row.createCell((short) 4);70 cell.setCellValue("检查时间");71 cell.setCellStyle(cellStyle);72 sheet.setColumnWidth(4, 5000);73 cell = row.createCell((short) 5);74 cell.setCellValue("记录更新时间");75 cell.setCellStyle(cellStyle);76 sheet.setColumnWidth(5, 5000);77 cell = row.createCell((short) 6);78 cell.setCellValue("当前状态");79 cell.setCellStyle(cellStyle);80 sheet.setColumnWidth(6, 4000);81 cell = row.createCell((short) 7);82 cell.setCellValue("异常等级");83 cell.setCellStyle(cellStyle);84 sheet.setColumnWidth(7, 4000);85 cell = row.createCell((short) 8);86

87 wb.setSheetName(0, sheetName);88 if(sheetResult.size() == 0){89 XSSFRow rows=sheet.createRow(1);90 rows.setHeight((short) 500);91 rows.createCell(0).setCellValue("没有查询结果!");92 returnwb;93 }94

95 for(int info = 0; info < sheetResult.size(); info ++){96 XSSFRow rows=sheet.createRow(info+1);97 rows.setHeight((short) 500);98 Map map =sheetResult.get(info);99

100 rows.createCell(0).setCellValue(map.get("name") + "");101 rows.createCell(1).setCellValue(map.get("ac_mac") + "");102 rows.createCell(2).setCellValue(map.get("item_code") + "");103 rows.createCell(3).setCellValue(map.get("check_times") + "");104 rows.createCell(4).setCellValue(map.get("check_time") + "");105 rows.createCell(5).setCellValue(map.get("update_time") + "");106 rows.createCell(6).setCellValue(map.get("status") + "");107 rows.createCell(7).setCellValue(map.get("exception_level") + "");108 //多插一行,避免单元格溢出

109 rows.createCell(8).setCellValue(" ");110 }111

112 returnwb;113 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值