4
0

ExcelUtils.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package com.redxun.knowledge.analysis.utils;
  2. import com.redxun.common.tool.StringUtils;
  3. import com.redxun.knowledge.analysis.entity.enums.DownloadNameEnum;
  4. import com.redxun.knowledge.analysis.entity.enums.DownloadTypeEnum;
  5. import com.redxun.knowledge.analysis.entity.vo.DownloadInfoVo;
  6. import org.apache.poi.ss.formula.functions.T;
  7. import org.apache.poi.ss.usermodel.Cell;
  8. import org.apache.poi.ss.usermodel.Row;
  9. import org.apache.poi.xssf.usermodel.XSSFSheet;
  10. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  11. import org.springframework.util.CollectionUtils;
  12. import javax.servlet.ServletOutputStream;
  13. import javax.servlet.http.HttpServletResponse;
  14. import java.net.URLEncoder;
  15. import java.text.SimpleDateFormat;
  16. import java.util.Arrays;
  17. import java.util.Date;
  18. import java.util.List;
  19. import java.util.stream.Collectors;
  20. /**
  21. * 文件名: ExcelUtils
  22. * 作者: zizg
  23. * 时间: 2023/6/1
  24. * 描述:
  25. * 修改人:
  26. * 修改时间:
  27. * 修改内容:
  28. */
  29. public class ExcelUtils {
  30. private ExcelUtils(){
  31. }
  32. /**
  33. * 导出Excel
  34. * @param list 导出数据
  35. * @param name 导出模块
  36. * @param type 导出类型(1 人员 2 部门)
  37. * @param response
  38. */
  39. public static void downloadExcel(List<DownloadInfoVo> list,Integer name,Integer type,String createTime,String endTime,String dimension, HttpServletResponse response){
  40. try {
  41. //创建一个空的工作薄
  42. XSSFWorkbook workbook = new XSSFWorkbook();
  43. //在工作薄中创建一个工作表
  44. XSSFSheet sheet = workbook.createSheet("sheet1");
  45. //设置列宽
  46. sheet.setColumnWidth(0, 20 * 256);
  47. sheet.setColumnWidth(1, 80 * 256);
  48. sheet.setColumnWidth(2, 20 * 256);
  49. //处理标题
  50. String[] titles = new String[]{"名称", "部门路径", "数量"};
  51. //创建标题行
  52. Row titleRow = sheet.createRow(0);
  53. Cell cell = null;
  54. for (int i = 0; i < titles.length; i++) {
  55. cell = titleRow.createCell(i);
  56. cell.setCellValue(titles[i]);
  57. }
  58. //处理内容
  59. int rowIndex = 1;
  60. Row row = null;
  61. if (!CollectionUtils.isEmpty(list)){
  62. for (DownloadInfoVo info : list) {
  63. row = sheet.createRow(rowIndex);
  64. cell = row.createCell(0);
  65. cell.setCellValue(info.getPersonName());
  66. cell = row.createCell(1);
  67. cell.setCellValue(info.getDeptName());
  68. cell = row.createCell(2);
  69. cell.setCellValue(info.getNum());
  70. rowIndex++;
  71. }
  72. }
  73. //设置文件的打开方式和mime类型
  74. String fileName = null;
  75. if (createTime != null && endTime != null){
  76. createTime = createTime.replaceAll("-","");
  77. endTime = endTime.replaceAll("-","");
  78. if (name == 1 || name == 5){
  79. fileName = URLEncoder.encode(
  80. DownloadNameEnum.getMessage(name) + "-" + DownloadTypeEnum.getMessage(type) + "访问量" + "-"
  81. + createTime + "-" + endTime, "UTF-8").replaceAll("\\+", "%20");
  82. } else {
  83. fileName = URLEncoder.encode(
  84. DownloadNameEnum.getMessage(name) + "-" + DownloadTypeEnum.getMessage(type) + "创建量" + "-"
  85. + createTime + "-" + endTime, "UTF-8").replaceAll("\\+", "%20");
  86. }
  87. } else {
  88. if (name == 1 || name == 5){
  89. if (("total").equals(dimension)){
  90. fileName = URLEncoder.encode(
  91. DownloadNameEnum.getMessage(name) + "-" + DownloadTypeEnum.getMessage(type) + "访问量" + "-"
  92. + "全部", "UTF-8").replaceAll("\\+", "%20");
  93. } else if (("year").equals(dimension)){
  94. fileName = URLEncoder.encode(
  95. DownloadNameEnum.getMessage(name) + "-" + DownloadTypeEnum.getMessage(type) + "访问量" + "-"
  96. + "年", "UTF-8").replaceAll("\\+", "%20");
  97. } else if (("month").equals(dimension)){
  98. fileName = URLEncoder.encode(
  99. DownloadNameEnum.getMessage(name) + "-" + DownloadTypeEnum.getMessage(type) + "访问量" + "-"
  100. + "月", "UTF-8").replaceAll("\\+", "%20");
  101. }
  102. } else {
  103. if (("total").equals(dimension)){
  104. fileName = URLEncoder.encode(
  105. DownloadNameEnum.getMessage(name) + "-" + DownloadTypeEnum.getMessage(type) + "创建量" + "-"
  106. + "全部", "UTF-8").replaceAll("\\+", "%20");
  107. } else if (("year").equals(dimension)){
  108. fileName = URLEncoder.encode(
  109. DownloadNameEnum.getMessage(name) + "-" + DownloadTypeEnum.getMessage(type) + "创建量" + "-"
  110. + "年", "UTF-8").replaceAll("\\+", "%20");
  111. } else if (("month").equals(dimension)){
  112. fileName = URLEncoder.encode(
  113. DownloadNameEnum.getMessage(name) + "-" + DownloadTypeEnum.getMessage(type) + "创建量" + "-"
  114. + "月", "UTF-8").replaceAll("\\+", "%20");
  115. }
  116. }
  117. }
  118. response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
  119. response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  120. response.setCharacterEncoding("utf-8");
  121. response.addHeader("blob","true");
  122. ServletOutputStream outputStream = response.getOutputStream();
  123. workbook.write(outputStream);
  124. } catch (Exception e) {
  125. e.printStackTrace();
  126. }
  127. }
  128. }