ExcelUtil.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package cn.kdan;
  2. import org.apache.poi.ss.usermodel.*;
  3. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  4. import org.springframework.util.StringUtils;
  5. import java.io.*;
  6. import java.util.HashMap;
  7. import java.util.HashSet;
  8. import java.util.Map;
  9. import java.util.Set;
  10. import java.util.regex.Matcher;
  11. import java.util.regex.Pattern;
  12. public class ExcelUtil {
  13. public static void main(String[] args) {
  14. ExcelUtil excelUtil = new ExcelUtil();
  15. excelUtil.getSheet();
  16. excelUtil.insert();
  17. excelUtil.statistics();
  18. }
  19. /**
  20. *根据设备资产表生成各个设备的sheet表单
  21. */
  22. private void getSheet() {
  23. String inputFilePath = "src/main/resources/长沙 - 资产管理(2024.9).xlsx";
  24. String outputFilePath = "src/main/resources/output.xlsx";;
  25. try (FileInputStream fis = new FileInputStream(new File(inputFilePath));
  26. Workbook workbook = new XSSFWorkbook(fis)) {
  27. Sheet sheet = workbook.getSheetAt(0); // 读取第一个sheet
  28. Workbook newWorkbook = new XSSFWorkbook(); // 创建新的工作簿
  29. for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
  30. Row row = sheet.getRow(i);
  31. Cell cell1 = row.getCell(15); // 假设第1列为索引0
  32. Cell cell2 = row.getCell(2); // 假设第2列为索引1
  33. if(StringUtils.isEmpty(cell2.getStringCellValue())&&StringUtils.isEmpty(cell1.getStringCellValue())){
  34. continue;
  35. }
  36. String sheetName = cell1.getStringCellValue();
  37. if(!StringUtils.isEmpty(cell2.getStringCellValue())){
  38. sheetName = sheetName+ "(" + cell2.getStringCellValue() +")";
  39. }
  40. Sheet newSheet = newWorkbook.createSheet(sheetName); // 创建新的Sheet
  41. // 创建一个CellStyle来设置样式
  42. CellStyle blueStyle = newWorkbook.createCellStyle();
  43. // 设置背景色为蓝色
  44. blueStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
  45. blueStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  46. // 创建一个白色字体的Font
  47. Font whiteFont = newWorkbook.createFont();
  48. whiteFont.setColor(IndexedColors.WHITE.getIndex());
  49. newSheet.setColumnWidth(0, 60 * 256);
  50. newSheet.setColumnWidth(1, 60 * 256);
  51. newSheet.setColumnWidth(2, 40 * 256);
  52. // 将字体应用到CellStyle
  53. blueStyle.setFont(whiteFont);
  54. Row oneRow = newSheet.createRow(0); // 第一行
  55. // 应用样式到第一行的前三列
  56. for (int col = 0; col < 3; col++) {
  57. Cell cell = oneRow.createCell(col); // 对应的列
  58. if(col== 0){
  59. cell.setCellValue(cell1.getStringCellValue());
  60. }
  61. cell.setCellStyle(blueStyle);
  62. }
  63. Row twoRow = newSheet.createRow(1); // 第二行
  64. // 设置第二行前三列的值
  65. // 注意:我们再次遍历列,但这次是在第二行上创建和设置单元格
  66. for (int col = 0; col < 3; col++) {
  67. Cell cell = twoRow.createCell(col); // 对应的列
  68. // 假设我们为每列设置不同的值
  69. switch (col) {
  70. case 0:
  71. cell.setCellValue("软件名称");
  72. break;
  73. case 1:
  74. cell.setCellValue("软件来源");
  75. break;
  76. case 2:
  77. cell.setCellValue("软件版本号");
  78. break;
  79. }
  80. cell.setCellStyle(blueStyle);
  81. }
  82. }
  83. // 写入到新的 Excel 文件
  84. try (FileOutputStream fos = new FileOutputStream(outputFilePath)) {
  85. newWorkbook.write(fos);
  86. }
  87. newWorkbook.close();
  88. System.out.println("数据已成功写入到新sheet中。");
  89. } catch (IOException e) {
  90. e.printStackTrace();
  91. }
  92. }
  93. /**
  94. * 将txt的数据插入到对应人员sheet页
  95. */
  96. public void insert() {
  97. String directoryPath = "src/main/resources/mac";
  98. String excelFilePath = "src/main/resources/output.xlsx";
  99. try (Workbook workbook = new XSSFWorkbook(new FileInputStream(new File(excelFilePath)))) {
  100. File dir = new File(directoryPath);
  101. File[] directoryListing = dir.listFiles((dir1, name) -> name.endsWith(".txt"));
  102. if (directoryListing != null) {
  103. for (File file : directoryListing) {
  104. String appName = extractAppNameFromFile(file);
  105. for (Sheet sheet : workbook) {
  106. if (sheet.getSheetName().contains(appName)) {
  107. int rowNum = 0;
  108. try (BufferedReader br = new BufferedReader(new FileReader(file))) {
  109. String line;
  110. while ((line = br.readLine()) != null) {
  111. if (++rowNum > 3) { // Start from the fourth line
  112. String[] columns = line.split("\\s+", 2); // Assuming space as delimiter, adjust as needed
  113. Row row = sheet.createRow(rowNum - 3 + 1) ;
  114. for (int i = 0; i < columns.length && i < 2; i++) {
  115. Cell cell = row.createCell(i);
  116. cell.setCellValue(columns[i]);
  117. }
  118. }
  119. }
  120. }
  121. break;
  122. }
  123. }
  124. }
  125. // Write the workbook in file system
  126. try (FileOutputStream outputStream = new FileOutputStream(excelFilePath)) {
  127. workbook.write(outputStream);
  128. }
  129. }
  130. } catch (IOException e) {
  131. e.printStackTrace();
  132. }
  133. }
  134. /**
  135. * 统计mac总表
  136. */
  137. public void statistics() {
  138. String directoryPath = "src/main/resources/mac";
  139. String excelFilePath = "src/main/resources/output.xlsx";
  140. try (Workbook workbook = new XSSFWorkbook(new FileInputStream(new File(excelFilePath)))) {
  141. File dir = new File(directoryPath);
  142. File[] directoryListing = dir.listFiles((dir1, name) -> name.endsWith(".txt"));
  143. if (directoryListing != null) {
  144. Map<String, Integer> countMap = new HashMap<>();
  145. Map<String, Set<String>> secondColumnMap = new HashMap<>(); // 使用Set存储不重复的第二列数据
  146. for (File file : directoryListing) {
  147. try (BufferedReader br = new BufferedReader(new FileReader(file))) {
  148. String line;
  149. while ((line = br.readLine()) != null) {
  150. String[] columns = line.split("\\s+", 2); // 按空格分隔
  151. if (columns.length > 1) {
  152. String key = columns[0];
  153. String secondValue = columns[1];
  154. if (key.equals("")){
  155. System.out.println(file.getAbsoluteFile());
  156. }
  157. // 更新计数
  158. countMap.put(key, countMap.getOrDefault(key, 0) + 1);
  159. // 存储不重复的第二列数据
  160. secondColumnMap.putIfAbsent(key, new HashSet<>());
  161. secondColumnMap.get(key).add(secondValue);
  162. }
  163. }
  164. }
  165. }
  166. // 创建sheet并写入数据
  167. Sheet sheet = workbook.createSheet("安装软件汇总_mac");
  168. workbook.setSheetOrder(sheet.getSheetName(), 0);
  169. int rowNum = 1;
  170. // 创建一个CellStyle来设置样式
  171. CellStyle blueStyle = workbook.createCellStyle();
  172. // 设置背景色为蓝色
  173. blueStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
  174. blueStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  175. // 创建一个白色字体的Font
  176. Font whiteFont = workbook.createFont();
  177. whiteFont.setColor(IndexedColors.WHITE.getIndex());
  178. sheet.setColumnWidth(0, 60 * 256);
  179. sheet.setColumnWidth(1, 60 * 256);
  180. sheet.setColumnWidth(2, 40 * 256);
  181. // 将字体应用到CellStyle
  182. blueStyle.setFont(whiteFont);
  183. Row oneRow = sheet.createRow(0); // 第一行
  184. // 应用样式到第一行的前三列
  185. for (int col = 0; col < 3; col++) {
  186. Cell cell = oneRow.createCell(col); // 对应的列
  187. if(col== 0){
  188. cell.setCellValue("mac_汇总");
  189. }
  190. cell.setCellStyle(blueStyle);
  191. }
  192. // 写入表头
  193. Row headerRow = sheet.createRow(rowNum++);
  194. for (int col = 0; col < 3; col++) {
  195. Cell cell = headerRow.createCell(col); // 对应的列
  196. // 假设我们为每列设置不同的值
  197. switch (col) {
  198. case 0:
  199. cell.setCellValue("App 名称");
  200. break;
  201. case 1:
  202. cell.setCellValue("使用设备数");
  203. break;
  204. case 2:
  205. cell.setCellValue("软件来源");
  206. break;
  207. }
  208. cell.setCellStyle(blueStyle);
  209. }
  210. // 写入数据
  211. for (Map.Entry<String, Integer> entry : countMap.entrySet()) {
  212. Row row = sheet.createRow(rowNum++);
  213. row.createCell(0).setCellValue(entry.getKey());
  214. row.createCell(1).setCellValue(entry.getValue());
  215. // 将不重复的第二列数据合并为一个字符串
  216. String secondValues = String.join("\n", secondColumnMap.get(entry.getKey()));
  217. Cell sourceCell = row.createCell(2); // 写入第二列数据
  218. sourceCell.setCellValue(secondValues);
  219. CellStyle cellStyle = workbook.createCellStyle();
  220. cellStyle.setWrapText(true);
  221. sourceCell.setCellStyle(cellStyle);
  222. }
  223. // 写入工作簿到文件系统
  224. try (FileOutputStream outputStream = new FileOutputStream(excelFilePath)) {
  225. workbook.write(outputStream);
  226. }
  227. }
  228. } catch (IOException e) {
  229. e.printStackTrace();
  230. }
  231. }
  232. /**
  233. * 获取txt里的名字用于匹配sheet进行填充
  234. * @param file file
  235. * @return String
  236. */
  237. public static String extractAppNameFromFile(File file) {
  238. // 定义正则表达式来匹配括号内的内容
  239. Pattern pattern = Pattern.compile("\\(([^)]+)\\)");
  240. // 转换文件名为字符串,以便使用正则表达式匹配
  241. String fileName = file.getName();
  242. // 创建matcher对象
  243. Matcher matcher = pattern.matcher(fileName);
  244. // 查找匹配项
  245. if (matcher.find()) {
  246. // 返回匹配到的括号内的内容
  247. return matcher.group(1);
  248. }
  249. System.out.println("txt文件名格式错误"+fileName);
  250. // 如果没有找到匹配项,返回null或空字符串(根据你的需求)
  251. return null; // 或者 ""
  252. }
  253. }