在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

springmvc实现导出数据信息为excle表格示例代码

2017-3-7 12:50| 发布者: zhangjf| 查看: 933| 评论: 0

摘要: 1.项目增加导出日志信息 2.项目中导入poi-*.jar等操作excel文件的jar文件 poi-3.7-20120326.jar poi-excelant-3.7-20101029.jar poi-ooxml-3.7.jar poi-ooxml-schemas-3.7.jar Excel导出就是根据前台条件 ...

1.项目增加导出日志信息

2.项目中导入poi-*.jar等操作excel文件的jar文件

  • poi-3.7-20120326.jar
  • poi-excelant-3.7-20101029.jar
  • poi-ooxml-3.7.jar
  • poi-ooxml-schemas-3.7.jar

Excel导出就是根据前台条件将参数传到controller,根据参数去数据库中进行查询,查询出list集合,将list集合生成excle数据下载。

代码片段:

Contorller.Java

  1. /**
  2. * 导出信息
  3. * @param model
  4. */
  5. @RequestMapping("exportCustomer.do")
  6. @SystemControllerLog(description = "数据库表单导出Excle")
  7. public void exportCustomer(ModelMap model) {
  8. //TODO 如需添加条件
  9. //model.addAttribute("username", nameStr);
  10. //获取需要导出的数据List
  11. List<CMcustomer> cusList=customerService.exportCustomer(model);
  12. //使用方法生成excle模板样式
  13. HSSFWorkbook workbook = customerService.createExcel(cusList, request);
  14. SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); // 定义文件名格式
  15. try {
  16. //定义excle名称 ISO-8859-1防止名称乱码
  17. String msg = new String(
  18. ("客户信息_" + format.format(new Date()) + ".xls").getBytes(),
  19. "ISO-8859-1");
  20. // 以导出时间作为文件名
  21. response.setContentType("application/vnd.ms-excel");
  22. response.addHeader("Content-Disposition", "attachment;filename="
  23. + msg);
  24. workbook.write(response.getOutputStream());
  25. } catch (IOException e) {
  26. logger.error(e);
  27. }
  28. }
复制代码

2.Service中createExcel方法

  1. public HSSFWorkbook createExcel(List<CMcustomer> cusList,
  2. HttpServletRequest request) {
  3. // 创建一个webbook,对应一个excel文件
  4. HSSFWorkbook workbook = new HSSFWorkbook();
  5. // 在webbook中添加一个sheet,对应excel文件中的sheet
  6. HSSFSheet sheet = workbook.createSheet("客户信息表");
  7. // 设置列宽
  8. sheet.setColumnWidth(0, 25 * 100);
  9. sheet.setColumnWidth(1, 35 * 100);
  10. sheet.setColumnWidth(2, 35 * 100);
  11. sheet.setColumnWidth(3, 40 * 100);
  12. sheet.setColumnWidth(4, 45 * 100);
  13. sheet.setColumnWidth(5, 45 * 100);
  14. sheet.setColumnWidth(6, 50 * 100);
  15. sheet.setColumnWidth(7, 80 * 100);
  16. sheet.setColumnWidth(8, 35 * 100);
  17. sheet.setColumnWidth(9, 40 * 100);
  18. // 在sheet中添加表头第0行
  19. HSSFRow row = sheet.createRow(0);
  20. // 创建单元格,并设置表头,设置表头居中
  21. HSSFCellStyle style = workbook.createCellStyle();
  22. // 创建一个居中格式
  23. style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  24. // 带边框
  25. style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  26. // 生成一个字体
  27. HSSFFont font = workbook.createFont();
  28. // 字体增粗
  29. font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  30. // 字体大小
  31. font.setFontHeightInPoints((short) 12);
  32. // 把字体应用到当前的样式
  33. style.setFont(font);
  34. // 单独设置整列居中或居左
  35. HSSFCellStyle style1 = workbook.createCellStyle();
  36. style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  37. HSSFCellStyle style2 = workbook.createCellStyle();
  38. style2.setAlignment(HSSFCellStyle.ALIGN_LEFT);
  39. HSSFCellStyle style3 = workbook.createCellStyle();
  40. style3.setAlignment(HSSFCellStyle.ALIGN_LEFT);
  41. HSSFFont hssfFont = workbook.createFont();
  42. hssfFont.setColor(HSSFFont.COLOR_RED);
  43. hssfFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  44. style3.setFont(hssfFont);
  45. HSSFCellStyle style4 = workbook.createCellStyle();
  46. style4.setAlignment(HSSFCellStyle.ALIGN_LEFT);
  47. HSSFFont hssfFont1 = workbook.createFont();
  48. hssfFont1.setColor(HSSFFont.COLOR_NORMAL);
  49. hssfFont1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  50. style4.setFont(hssfFont1);
  51. HSSFCell cell = row.createCell(0);
  52. cell.setCellValue("序号");
  53. cell.setCellStyle(style);
  54. cell = row.createCell(1);
  55. cell.setCellValue("客户姓名");
  56. cell.setCellStyle(style);
  57. cell = row.createCell(2);
  58. cell.setCellValue("性别");
  59. cell.setCellStyle(style);
  60. cell = row.createCell(3);
  61. cell.setCellValue("状态");
  62. cell.setCellStyle(style);
  63. cell = row.createCell(4);
  64. cell.setCellValue("电话");
  65. cell.setCellStyle(style);
  66. cell = row.createCell(5);
  67. cell.setCellValue("邮箱");
  68. cell.setCellStyle(style);
  69. cell = row.createCell(6);
  70. cell.setCellValue("地址");
  71. cell.setCellStyle(style);
  72. for (int i = 0; i < cusList.size(); i++) {
  73. String logTypeDis = "";
  74. row = sheet.createRow(i + 1);
  75. CMcustomer cMcustomer = cusList.get(i);
  76. // 创建单元格,并设置值
  77. // 编号列居左
  78. HSSFCell c1 = row.createCell(0);
  79. c1.setCellStyle(style2);
  80. c1.setCellValue(i);
  81. HSSFCell c2 = row.createCell(1);
  82. c2.setCellStyle(style1);
  83. c2.setCellValue(cMcustomer.getCustomername());//客户姓名
  84. String sexStr = cMcustomer.getSex();//性别 0:女,1:男
  85. String sex="";
  86. if ("1".equals(sexStr)) {
  87. sex="男";
  88. }
  89. if ("0".equals(sexStr)) {
  90. sex="女";
  91. }
  92. HSSFCell c3 = row.createCell(2);//性别
  93. c3.setCellStyle(style1);
  94. c3.setCellValue(sex);
  95. String statusStr = cMcustomer.getStatus();//客户状态1.在职,2.离职
  96. String status="";
  97. if ("1".equals(statusStr)) {
  98. status="在职";
  99. }
  100. if ("2".equals(statusStr)) {
  101. status="离职";
  102. }
  103. HSSFCell c4 = row.createCell(3);//状态
  104. c4.setCellStyle(style1);
  105. c4.setCellValue(status);
  106. String customerid = cMcustomer.getCustomerid();//客户id
  107. List<CMphone> phoneList = cMphoneMapper.selectByCustomerid(customerid);
  108. String phone="";
  109. if (phoneList!=null&&phoneList.size()>0) {
  110. for (int j = 0; j < phoneList.size(); j++) {
  111. phone = phoneList.get(j).getPhone();
  112. }
  113. }
  114. HSSFCell c5 = row.createCell(4);//电话
  115. c5.setCellStyle(style1);
  116. c5.setCellValue(phone);
  117. List<CMemail> emailList = cMemailMapper.selectAll(customerid);
  118. String email="";
  119. if (emailList!=null&&emailList.size()>0) {
  120. for (int j = 0; j < emailList.size(); j++) {
  121. email = emailList.get(j).getEmail();
  122. }
  123. }
  124. HSSFCell c6 = row.createCell(5);//邮箱
  125. c6.setCellStyle(style1);
  126. c6.setCellValue(email);
  127. CMaddress cMaddress=new CMaddress();
  128. cMaddress.setCustomerid(customerid);
  129. List<CMaddress> adderssList = cMaddressMapper.selectAll(cMaddress);
  130. String adderss="";
  131. if (adderssList!=null&&adderssList.size()>0) {
  132. for (int j = 0; j < adderssList.size(); j++) {
  133. adderss = adderssList.get(j).getAddress();
  134. }
  135. }
  136. HSSFCell c7 = row.createCell(6);//地址
  137. c7.setCellStyle(style1);
  138. c7.setCellValue(adderss);
  139. //使用默认格式
  140. row.createCell(1).setCellValue(cMcustomer.getCustomername());
  141. row.createCell(2).setCellValue(sex);
  142. row.createCell(3).setCellValue(status);
  143. row.createCell(4).setCellValue(phone);
  144. row.createCell(5).setCellValue(email);
  145. row.createCell(6).setCellValue(adderss);
  146. }
  147. return workbook;
  148. }
复制代码

3.页面jsp调用

  1. //导出信息
  2. function exporBtn(){
  3. $.ajax({
  4. type:"POST",
  5. url:"<%=path%>/customer/exportCustomer.do",
  6. success:function(data){
  7. window.open('<%=path%>/customer/exportCustomer.do');
  8. }
  9. });
  10. }
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-5-4 02:20

Copyright 2015-2025 djqfx

返回顶部