BaseApplicationTests.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package com.dayou;
  2. import cn.hutool.core.lang.Console;
  3. import cn.hutool.http.HttpRequest;
  4. import com.alibaba.excel.EasyExcel;
  5. import com.alibaba.excel.ExcelWriter;
  6. import com.alibaba.excel.write.metadata.WriteSheet;
  7. import org.apache.poi.ss.usermodel.*;
  8. import org.apache.poi.ss.util.CellRangeAddressList;
  9. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  10. import org.junit.jupiter.api.Test;
  11. import org.springframework.boot.test.context.SpringBootTest;
  12. import java.io.*;
  13. import java.nio.file.Files;
  14. import java.nio.file.Paths;
  15. import java.util.HashMap;
  16. import java.util.Map;
  17. @SpringBootTest
  18. class BaseApplicationTests {
  19. @Test
  20. void contextLoads() {
  21. }
  22. // @Test
  23. void hutoolPostTest(){
  24. //链式构建请求
  25. String result = HttpRequest.post("localhost:8088/api/auth/checkOaToken?token=eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxIiwiZXhwIjoxNzI5MzE3MzUyLCJpYXQiOjE3MjkyMzA5NTJ9.6zLI3TXlNGD2XCEMwl7vyYRDKFMRAfxhr8TaC1GEnug4eTnJZTJQRUeLBH7pymniKlQDVtrZD_ZGd359vZid7Q")
  26. .timeout(20000)//超时,毫秒
  27. .execute().body();
  28. Console.log(result);
  29. }
  30. /**
  31. * 测试使用EasyExcel填充值到带公式的excel模板
  32. */
  33. // @Test
  34. void testEasyExcel(){
  35. // 模板文件路径
  36. String templateFilePath = "E:\\test\\input.xlsx";
  37. // 输出文件路径
  38. String outputPath = "E:\\test\\output.xlsx";
  39. // 创建一个Map来存储要填充的数据
  40. Map<String, Object> dataMap = new HashMap<>();
  41. dataMap.put("a", 13.7);
  42. dataMap.put("b", 46.9);
  43. // 使用EasyExcel填充数据
  44. ExcelWriter excelWriter = EasyExcel.write(outputPath)
  45. .withTemplate(templateFilePath)
  46. .inMemory(true) // 启用内存模式
  47. .build();
  48. WriteSheet writeSheet = EasyExcel.writerSheet().build();
  49. excelWriter.fill(dataMap, writeSheet); // 填充数据
  50. // 由于填充后不会自动更新公式值,所以此处需要手动更新,大文件慎用(可能会导致内存溢出)
  51. Workbook workbook = excelWriter.writeContext().writeWorkbookHolder().getWorkbook();
  52. workbook.getCreationHelper().createFormulaEvaluator().evaluateAll(); // 强制计算公式
  53. excelWriter.finish();
  54. }
  55. /**
  56. * 测试使用poi合并Excel文件
  57. * 大概就是读取模板文件的行信息(包括数据,样式等等),再写入到目标文件中
  58. */
  59. // @Test
  60. void testExcelMerge(){
  61. String sourceExcelPath = "E:\\test\\source.xlsx"; // 源Excel文件路径
  62. String targetExcelPath = "E:\\test\\target.xlsx"; // 目标Excel文件路径
  63. int targetSheetIndex = 0; // 目标sheet索引
  64. int targetRowNum = 4; // 目标行索引
  65. // 加载源Excel工作簿
  66. try (InputStream inputStream = Files.newInputStream(Paths.get(sourceExcelPath))) {
  67. Workbook sourceWorkbook = new XSSFWorkbook(inputStream);
  68. Sheet sourceSheet = sourceWorkbook.getSheetAt(0); // 读取第一个sheet
  69. // 打开已存在的Excel工作簿
  70. Workbook targetWorkbook = WorkbookFactory.create(Files.newInputStream(Paths.get(targetExcelPath)));
  71. Sheet targetSheet = targetWorkbook.getSheetAt(targetSheetIndex); // 获取目标sheet
  72. // 复制样式映射
  73. Map<Short, Short> styleMap = copyCellStyle(sourceWorkbook, targetWorkbook);
  74. // 复制行和单元格
  75. for (Row sourceRow : sourceSheet) {
  76. Row targetRow = targetSheet.createRow(targetRowNum + sourceRow.getRowNum());
  77. for (Cell sourceCell : sourceRow) {
  78. Cell targetCell = targetRow.createCell(sourceCell.getColumnIndex());
  79. copyCell(sourceCell, targetCell, targetWorkbook, styleMap);
  80. }
  81. }
  82. // 复制数据验证
  83. copyDataValidations(sourceSheet, targetSheet);
  84. // 保存目标Excel工作簿
  85. try (FileOutputStream outputStream = new FileOutputStream(targetExcelPath)) {
  86. targetWorkbook.write(outputStream);
  87. }
  88. } catch (Exception e) {
  89. e.printStackTrace();
  90. }
  91. }
  92. /**
  93. * 复制单元格样式
  94. * @param srcBook
  95. * @param desBook
  96. * @return
  97. */
  98. private static Map<Short, Short> copyCellStyle(Workbook srcBook, Workbook desBook) {
  99. Map<Short, Short> styleMap = new HashMap<>();
  100. for (short i = 0; i < srcBook.getNumCellStyles(); i++) {
  101. CellStyle srcStyle = srcBook.getCellStyleAt(i);
  102. CellStyle desStyle = desBook.createCellStyle();
  103. desStyle.cloneStyleFrom(srcStyle);
  104. styleMap.put(srcStyle.getIndex(), desStyle.getIndex());
  105. }
  106. return styleMap;
  107. }
  108. /**
  109. * 复制单元格
  110. * @param srcCell
  111. * @param desCell
  112. * @param targetWorkbook
  113. * @param styleMap
  114. */
  115. private static void copyCell(Cell srcCell, Cell desCell, Workbook targetWorkbook, Map<Short, Short> styleMap) {
  116. // 判断单元格值类型
  117. if (srcCell.getCellType() == CellType.NUMERIC) {
  118. desCell.setCellValue(srcCell.getNumericCellValue());
  119. } else if (srcCell.getCellType() == CellType.STRING) {
  120. desCell.setCellValue(srcCell.getStringCellValue());
  121. } else if (srcCell.getCellType() == CellType.BOOLEAN) {
  122. desCell.setCellValue(srcCell.getBooleanCellValue());
  123. } else if (srcCell.getCellType() == CellType.BLANK) {
  124. desCell.setBlank();
  125. } else if (srcCell.getCellType() == CellType.FORMULA) {
  126. desCell.setCellFormula(srcCell.getCellFormula());
  127. }
  128. // 复制样式
  129. desCell.setCellStyle(targetWorkbook.getCellStyleAt(styleMap.get(srcCell.getCellStyle().getIndex())));
  130. }
  131. /**
  132. * 复制数据验证
  133. * @param sourceSheet
  134. * @param targetSheet
  135. */
  136. private static void copyDataValidations(Sheet sourceSheet, Sheet targetSheet) {
  137. for (DataValidation validation : sourceSheet.getDataValidations()) {
  138. // 设置要设置数据验证的单元格坐标
  139. CellRangeAddressList regions = new CellRangeAddressList(validation.getRegions().getCellRangeAddress(0).getFirstRow() + 4,
  140. validation.getRegions().getCellRangeAddress(0).getLastRow() + 4,
  141. validation.getRegions().getCellRangeAddress(0).getFirstColumn(),
  142. validation.getRegions().getCellRangeAddress(0).getLastColumn());
  143. // 获取数据源的数据验证信息
  144. DataValidationHelper validationHelper = targetSheet.getDataValidationHelper();
  145. DataValidationConstraint constraint = validation.getValidationConstraint();
  146. // 新建数据验证
  147. DataValidation newValidation = validationHelper.createValidation(constraint, regions);
  148. // 在目标excel中设置数据验证
  149. targetSheet.addValidationData(newValidation);
  150. }
  151. }
  152. }