<dependency> <groupId>net.sourceforge.jexcelapi</groupId> <artifactId>jxl</artifactId> <version>2.6.12</version> <scope>compile</scope> </dependency>
//生成excel public static ByteArrayInputStream createExcel(List<AlarmContentEmailVo> emailVoList) { try { // 1、创建一个流文件 ByteArrayOutputStream excel = new ByteArrayOutputStream(); //创建一个excel WritableWorkbook workbook = Workbook.createWorkbook(excel); // 2、创建一个Excel的工作表sheet WritableSheet sheet = workbook.createSheet("预警内容", 0); // 3、样式设置 WritableFont bold = new WritableFont(WritableFont.createFont("宋体"), 12, WritableFont.BOLD); WritableFont noBold = new WritableFont(WritableFont.createFont("宋体"), 12, WritableFont.NO_BOLD); WritableCellFormat titleFormate = new WritableCellFormat(bold); // 设置单元格中的内容水平方向居中、垂直方向居中设置边框 titleFormate.setAlignment(jxl.format.Alignment.CENTRE); titleFormate.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); titleFormate.setBorder(Border.ALL, BorderLineStyle.THIN); // 设置正文内容样式,单元格样式控制对象 WritableCellFormat textFormat = new WritableCellFormat(noBold); // 单元格中的内容水平方向居中、垂直方向居中、设置边框 textFormat.setAlignment(jxl.format.Alignment.CENTRE); textFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); textFormat.setBorder(Border.ALL,BorderLineStyle.THIN); // 3.4、窗口冻结第一行 sheet.getSettings().setVerticalFreeze(1); //sheet.getSettings().setHorizontalFreeze(2);//冻结 2列两行 // 3.5、设置行高--第一行标题行 sheet.setRowView(0,500); // // 3.6、设置列宽 // sheet.setColumnView(0,15); // sheet.setColumnView(1,25); // sheet.setColumnView(2,30); // sheet.setColumnView(3,15); // sheet.setColumnView(4,15); // sheet.setColumnView(5,20); // sheet.setColumnView(6,20); // sheet.setColumnView(7,20); // 4、构造表头 List<String> titleList = Arrays.asList("运营商", "项目", "企业名称", "预警类别", "预警说明", "预警等级","预警时间","预警状态"); for (int i = 0; i < titleList.size(); i++) { Label label_00 = new Label(i,0, titleList.get(i), titleFormate); sheet.addCell(label_00); } // 5、填充数据 //开始行数 int startRow=1; //开始列数 int startcell=0; for (int i = 0; i < emailVoList.size(); i++, startRow++) { startcell=0; AlarmContentEmailVo vo= emailVoList.get(i); Label label = new Label(startcell++, startRow, vo.getDeptName(), textFormat); sheet.addCell(label); Label labe2 = new Label(startcell++, startRow, vo.getProjectName(), textFormat); sheet.addCell(labe2); Label labe3 = new Label(startcell++, startRow, vo.getCompanyName(), textFormat); sheet.addCell(labe3); Label labe4 = new Label(startcell++, startRow, vo.getAlarmCategoryName(), textFormat); sheet.addCell(labe4); Label labe5 = new Label(startcell++, startRow, vo.getAlarmDetail(), textFormat); sheet.addCell(labe5); Label labe6 = new Label(startcell++, startRow, vo.getAlarmLevelName().toString(), textFormat); sheet.addCell(labe6); Label labe7 = new Label(startcell++, startRow, vo.getAlarmTime().toString(), textFormat); sheet.addCell(labe7); Label labe8 = new Label(startcell++, startRow, vo.getStatusName(), textFormat); sheet.addCell(labe8); } workbook.write(); workbook.close(); // 将流文件存到字节数组缓冲区 ByteArrayInputStream excelAttachment = new ByteArrayInputStream(excel.toByteArray()); return excelAttachment; }catch (Exception e){ throw new ServiceException("创建Excel失败"); } }
发送邮件
public void sendAlarmMail(String to, String subject, List<AlarmContentEmailVo emailVoList) {
try {
//邮件可自行生成,主要看附件
MimeMessage mailMessage = javaMailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true, "utf-8");
messageHelper.setFrom(fromMail);
messageHelper.setTo(to);
messageHelper.setSubject(subject);
messageHelper.setText(contextText(emailVoList), true);
// 邮件附件
ByteArrayDataSource file = new ByteArrayDataSource(createExcel(emailVoList), "application/vnd.ms-excel;charset=UTF-8");
messageHelper.addAttachment(MimeUtility.encodeWord("预警内容.xls","utf-8","B"),file);
// FileSystemResource file = new FileSystemResource(new File("D:\\1.xlsx"));
// messageHelper.addAttachment("预警内容.xlsx", file);
javaMailSender.send(mailMessage);
} catch (Exception e) {
throw new ServiceException("发送邮件失败");
}
}