ExcelUtil.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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.insertWindows();
  18. excelUtil.statistics();
  19. excelUtil.statisticsWindows();
  20. }
  21. /**
  22. *根据设备资产表生成各个设备的sheet表单
  23. */
  24. private void getSheet() {
  25. String inputFilePath = "src/main/resources/长沙 - 资产管理(2024.9).xlsx";
  26. String outputFilePath = "src/main/resources/output.xlsx";;
  27. try (FileInputStream fis = new FileInputStream(new File(inputFilePath));
  28. Workbook workbook = new XSSFWorkbook(fis)) {
  29. Sheet sheet = workbook.getSheetAt(0); // 读取第一个sheet
  30. Workbook newWorkbook = new XSSFWorkbook(); // 创建新的工作簿
  31. for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
  32. Row row = sheet.getRow(i);
  33. Cell cell1 = row.getCell(15); // 假设第1列为索引0
  34. Cell cell2 = row.getCell(2); // 假设第2列为索引1
  35. if(StringUtils.isEmpty(cell2.getStringCellValue())&&StringUtils.isEmpty(cell1.getStringCellValue())){
  36. continue;
  37. }
  38. String sheetName = cell1.getStringCellValue();
  39. if(!StringUtils.isEmpty(cell2.getStringCellValue())){
  40. sheetName = sheetName+ "(" + cell2.getStringCellValue() +")";
  41. }
  42. Sheet newSheet = newWorkbook.createSheet(sheetName); // 创建新的Sheet
  43. // 创建一个CellStyle来设置样式
  44. CellStyle blueStyle = newWorkbook.createCellStyle();
  45. // 设置背景色为蓝色
  46. blueStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
  47. blueStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  48. // 创建一个白色字体的Font
  49. Font whiteFont = newWorkbook.createFont();
  50. whiteFont.setColor(IndexedColors.WHITE.getIndex());
  51. newSheet.setColumnWidth(0, 60 * 256);
  52. newSheet.setColumnWidth(1, 60 * 256);
  53. newSheet.setColumnWidth(2, 40 * 256);
  54. // 将字体应用到CellStyle
  55. blueStyle.setFont(whiteFont);
  56. Row oneRow = newSheet.createRow(0); // 第一行
  57. // 应用样式到第一行的前三列
  58. for (int col = 0; col < 3; col++) {
  59. Cell cell = oneRow.createCell(col); // 对应的列
  60. if(col== 0){
  61. cell.setCellValue(cell1.getStringCellValue());
  62. }
  63. cell.setCellStyle(blueStyle);
  64. }
  65. Row twoRow = newSheet.createRow(1); // 第二行
  66. // 设置第二行前三列的值
  67. // 注意:我们再次遍历列,但这次是在第二行上创建和设置单元格
  68. for (int col = 0; col < 3; col++) {
  69. Cell cell = twoRow.createCell(col); // 对应的列
  70. // 假设我们为每列设置不同的值
  71. switch (col) {
  72. case 0:
  73. cell.setCellValue("软件名称");
  74. break;
  75. case 1:
  76. cell.setCellValue("软件来源");
  77. break;
  78. case 2:
  79. cell.setCellValue("软件版本号");
  80. break;
  81. }
  82. cell.setCellStyle(blueStyle);
  83. }
  84. }
  85. // 写入到新的 Excel 文件
  86. try (FileOutputStream fos = new FileOutputStream(outputFilePath)) {
  87. newWorkbook.write(fos);
  88. }
  89. newWorkbook.close();
  90. System.out.println("数据已成功写入到新sheet中。");
  91. } catch (IOException e) {
  92. e.printStackTrace();
  93. }
  94. }
  95. /**
  96. * 将txt的数据插入到对应人员sheet页
  97. */
  98. public void insert() {
  99. String directoryPath = "src/main/resources/mac";
  100. String excelFilePath = "src/main/resources/output.xlsx";
  101. try (Workbook workbook = new XSSFWorkbook(new FileInputStream(new File(excelFilePath)))) {
  102. File dir = new File(directoryPath);
  103. File[] directoryListing = dir.listFiles((dir1, name) -> name.endsWith(".txt"));
  104. if (directoryListing != null) {
  105. for (File file : directoryListing) {
  106. String appName = extractAppNameFromFile(file);
  107. for (Sheet sheet : workbook) {
  108. if (sheet.getSheetName().contains(appName)) {
  109. int rowNum = 0;
  110. try (BufferedReader br = new BufferedReader(new FileReader(file))) {
  111. String line;
  112. while ((line = br.readLine()) != null) {
  113. if (++rowNum > 3) { // Start from the fourth line
  114. String[] columns = line.split("(?<=\\.app)\\s+", 2); // Assuming space as delimiter, adjust as needed
  115. Row row = sheet.createRow(rowNum - 3 + 1) ;
  116. for (int i = 0; i < columns.length && i < 2; i++) {
  117. Cell cell = row.createCell(i);
  118. cell.setCellValue(columns[i]);
  119. }
  120. }
  121. }
  122. }
  123. break;
  124. }
  125. }
  126. }
  127. // Write the workbook in file system
  128. try (FileOutputStream outputStream = new FileOutputStream(excelFilePath)) {
  129. workbook.write(outputStream);
  130. }
  131. }
  132. } catch (IOException e) {
  133. e.printStackTrace();
  134. }
  135. }
  136. /**
  137. * 统计mac总表
  138. */
  139. public void statistics() {
  140. String directoryPath = "src/main/resources/mac";
  141. String excelFilePath = "src/main/resources/output.xlsx";
  142. try (Workbook workbook = new XSSFWorkbook(new FileInputStream(new File(excelFilePath)))) {
  143. File dir = new File(directoryPath);
  144. File[] directoryListing = dir.listFiles((dir1, name) -> name.endsWith(".txt"));
  145. if (directoryListing != null) {
  146. Map<String, Integer> countMap = new HashMap<>();
  147. Map<String, Set<String>> secondColumnMap = new HashMap<>(); // 使用Set存储不重复的第二列数据
  148. for (File file : directoryListing) {
  149. try (BufferedReader br = new BufferedReader(new FileReader(file))) {
  150. String line;
  151. while ((line = br.readLine()) != null) {
  152. String[] columns = line.split("(?<=\\.app)\\s+", 2); // Assuming space as delimiter, adjust as needed
  153. if (columns.length > 1) {
  154. String key = columns[0];
  155. String secondValue = columns[1];
  156. if (key.equals("")){
  157. System.out.println(file.getAbsoluteFile());
  158. }
  159. // 更新计数
  160. countMap.put(key, countMap.getOrDefault(key, 0) + 1);
  161. // 存储不重复的第二列数据
  162. secondColumnMap.putIfAbsent(key, new HashSet<>());
  163. secondColumnMap.get(key).add(secondValue);
  164. }
  165. }
  166. }
  167. }
  168. // 创建sheet并写入数据
  169. Sheet sheet = workbook.createSheet("安装软件汇总_mac");
  170. workbook.setSheetOrder(sheet.getSheetName(), 0);
  171. int rowNum = 1;
  172. // 创建一个CellStyle来设置样式
  173. CellStyle blueStyle = workbook.createCellStyle();
  174. // 设置背景色为蓝色
  175. blueStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
  176. blueStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  177. // 创建一个白色字体的Font
  178. Font whiteFont = workbook.createFont();
  179. whiteFont.setColor(IndexedColors.WHITE.getIndex());
  180. sheet.setColumnWidth(0, 60 * 256);
  181. sheet.setColumnWidth(1, 60 * 256);
  182. sheet.setColumnWidth(2, 40 * 256);
  183. // 将字体应用到CellStyle
  184. blueStyle.setFont(whiteFont);
  185. Row oneRow = sheet.createRow(0); // 第一行
  186. // 应用样式到第一行的前三列
  187. for (int col = 0; col < 3; col++) {
  188. Cell cell = oneRow.createCell(col); // 对应的列
  189. if(col== 0){
  190. cell.setCellValue("mac_汇总");
  191. }
  192. cell.setCellStyle(blueStyle);
  193. }
  194. // 写入表头
  195. Row headerRow = sheet.createRow(rowNum++);
  196. for (int col = 0; col < 3; col++) {
  197. Cell cell = headerRow.createCell(col); // 对应的列
  198. // 假设我们为每列设置不同的值
  199. switch (col) {
  200. case 0:
  201. cell.setCellValue("App 名称");
  202. break;
  203. case 1:
  204. cell.setCellValue("使用设备数");
  205. break;
  206. case 2:
  207. cell.setCellValue("软件来源");
  208. break;
  209. }
  210. cell.setCellStyle(blueStyle);
  211. }
  212. // 写入数据
  213. for (Map.Entry<String, Integer> entry : countMap.entrySet()) {
  214. Row row = sheet.createRow(rowNum++);
  215. row.createCell(0).setCellValue(entry.getKey());
  216. row.createCell(1).setCellValue(entry.getValue());
  217. // 将不重复的第二列数据合并为一个字符串
  218. String secondValues = String.join("\n", secondColumnMap.get(entry.getKey()));
  219. Cell sourceCell = row.createCell(2); // 写入第二列数据
  220. sourceCell.setCellValue(secondValues);
  221. CellStyle cellStyle = workbook.createCellStyle();
  222. cellStyle.setWrapText(true);
  223. sourceCell.setCellStyle(cellStyle);
  224. }
  225. // 写入工作簿到文件系统
  226. try (FileOutputStream outputStream = new FileOutputStream(excelFilePath)) {
  227. workbook.write(outputStream);
  228. }
  229. }
  230. } catch (IOException e) {
  231. e.printStackTrace();
  232. }
  233. }
  234. /**
  235. * 将txt的数据插入到对应人员sheet页
  236. */
  237. public void insertWindows() {
  238. String directoryPath = "src/main/resources/windows";
  239. String excelFilePath = "src/main/resources/output.xlsx";
  240. try (Workbook workbook = new XSSFWorkbook(new FileInputStream(new File(excelFilePath)))) {
  241. File dir = new File(directoryPath);
  242. File[] directoryListing = dir.listFiles((dir1, name) -> name.endsWith(".txt"));
  243. if (directoryListing != null) {
  244. for (File file : directoryListing) {
  245. String appName = extractAppNameFromFile(file);
  246. for (Sheet sheet : workbook) {
  247. if (sheet.getSheetName().contains(appName + ")")) {
  248. int rowNum = 0;
  249. try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-16LE"))) { // 指定编码为 UTF-16LE
  250. String line;
  251. while ((line = br.readLine()) != null) {
  252. if (++rowNum > 1) { // Start from the fourth line
  253. // 使用正则表达式找到任意字符后面跟着两个空格的位置
  254. Pattern pattern = Pattern.compile("(.).\\s\\s");
  255. Matcher matcher = pattern.matcher(line);
  256. if (matcher.find()) {
  257. int index = matcher.start() + 2; // 匹配的起始位置加上三个字符长度
  258. Row row = sheet.createRow(rowNum) ;
  259. // 在找到的位置进行分割
  260. String part1 = line.substring(0, index).trim();
  261. Cell cell = row.createCell(0);
  262. cell.setCellValue(part1);
  263. String part2 = line.substring(index).trim(); // 去除前导空格
  264. Cell cell1 = row.createCell(2);
  265. cell1.setCellValue(part2);
  266. // 输出结果
  267. System.out.println("Part 1: " + part1);
  268. System.out.println("Part 2: " + part2);
  269. } else {
  270. System.out.println("No match found.");
  271. }
  272. /*String[] columns = line.split(".\\s\\s", 2); // Assuming space as delimiter, adjust as needed
  273. Row row = sheet.createRow(rowNum - 3 + 1) ;
  274. for (int i = 0; i < columns.length && i < 2; i++) {
  275. if (i == 1) {
  276. Cell cell = row.createCell(i + 1);
  277. cell.setCellValue(columns[i].trim());
  278. } else {
  279. Cell cell = row.createCell(i);
  280. cell.setCellValue(columns[i]);
  281. }
  282. }*/
  283. }
  284. }
  285. }
  286. break;
  287. }
  288. }
  289. }
  290. // Write the workbook in file system
  291. try (FileOutputStream outputStream = new FileOutputStream(excelFilePath)) {
  292. workbook.write(outputStream);
  293. }
  294. }
  295. } catch (IOException e) {
  296. e.printStackTrace();
  297. }
  298. }
  299. /**
  300. * 统计Windows总表
  301. */
  302. public void statisticsWindows() {
  303. String directoryPath = "src/main/resources/windows";
  304. String excelFilePath = "src/main/resources/output.xlsx";
  305. try (Workbook workbook = new XSSFWorkbook(new FileInputStream(new File(excelFilePath)))) {
  306. File dir = new File(directoryPath);
  307. File[] directoryListing = dir.listFiles((dir1, name) -> name.endsWith(".txt"));
  308. if (directoryListing != null) {
  309. Map<String, Integer> countMap = new HashMap<>();
  310. Map<String, Set<String>> secondColumnMap = new HashMap<>(); // 使用Set存储不重复的第二列数据
  311. for (File file : directoryListing) {
  312. try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-16LE"))) {
  313. String line;
  314. while ((line = br.readLine()) != null) {
  315. // 使用正则表达式找到任意字符后面跟着两个空格的位置
  316. Pattern pattern = Pattern.compile("(.).\\s\\s");
  317. Matcher matcher = pattern.matcher(line);
  318. if (matcher.find()) {
  319. int index = matcher.start() + 3; // 匹配的起始位置加上三个字符长度
  320. // 在找到的位置进行分割
  321. String key = line.substring(0, index);
  322. String secondValue = line.substring(index).trim(); // 去除前导空格
  323. if (key.equals("")) {
  324. System.out.println(file.getAbsoluteFile());
  325. }
  326. // 更新计数
  327. countMap.put(key, countMap.getOrDefault(key, 0) + 1);
  328. // 存储不重复的第二列数据
  329. secondColumnMap.putIfAbsent(key, new HashSet<>());
  330. secondColumnMap.get(key).add(secondValue);
  331. } else {
  332. System.out.println("No match found.");
  333. }
  334. /*String[] columns = line.split(".\\s\\s", 2); // 按空格分隔
  335. if (columns.length > 1) {
  336. String key = columns[0];
  337. String secondValue = columns[1].trim();
  338. if (key.equals("")) {
  339. System.out.println(file.getAbsoluteFile());
  340. }
  341. // 更新计数
  342. countMap.put(key, countMap.getOrDefault(key, 0) + 1);
  343. // 存储不重复的第二列数据
  344. secondColumnMap.putIfAbsent(key, new HashSet<>());
  345. secondColumnMap.get(key).add(secondValue);
  346. }*/
  347. }
  348. }
  349. }
  350. // 创建sheet并写入数据
  351. Sheet sheet = workbook.createSheet("安装软件汇总_windows");
  352. workbook.setSheetOrder(sheet.getSheetName(), 0);
  353. int rowNum = 1;
  354. // 创建一个CellStyle来设置样式
  355. CellStyle blueStyle = workbook.createCellStyle();
  356. // 设置背景色为蓝色
  357. blueStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
  358. blueStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  359. // 创建一个白色字体的Font
  360. Font whiteFont = workbook.createFont();
  361. whiteFont.setColor(IndexedColors.WHITE.getIndex());
  362. sheet.setColumnWidth(0, 60 * 256);
  363. sheet.setColumnWidth(1, 60 * 256);
  364. sheet.setColumnWidth(2, 40 * 256);
  365. // 将字体应用到CellStyle
  366. blueStyle.setFont(whiteFont);
  367. Row oneRow = sheet.createRow(0); // 第一行
  368. // 应用样式到第一行的前三列
  369. for (int col = 0; col < 3; col++) {
  370. Cell cell = oneRow.createCell(col); // 对应的列
  371. if (col == 0) {
  372. cell.setCellValue("windows_汇总");
  373. }
  374. cell.setCellStyle(blueStyle);
  375. }
  376. // 写入表头
  377. Row headerRow = sheet.createRow(rowNum++);
  378. for (int col = 0; col < 3; col++) {
  379. Cell cell = headerRow.createCell(col); // 对应的列
  380. // 假设我们为每列设置不同的值
  381. switch (col) {
  382. case 0:
  383. cell.setCellValue("App 名称");
  384. break;
  385. case 1:
  386. cell.setCellValue("使用设备数");
  387. break;
  388. case 2:
  389. cell.setCellValue("版本号");
  390. break;
  391. }
  392. cell.setCellStyle(blueStyle);
  393. }
  394. // 写入数据
  395. for (Map.Entry<String, Integer> entry : countMap.entrySet()) {
  396. Row row = sheet.createRow(rowNum++);
  397. row.createCell(0).setCellValue(entry.getKey());
  398. row.createCell(1).setCellValue(entry.getValue());
  399. // 将不重复的第二列数据合并为一个字符串
  400. String secondValues = String.join("\n", secondColumnMap.get(entry.getKey()));
  401. Cell sourceCell = row.createCell(2); // 写入第二列数据
  402. sourceCell.setCellValue(secondValues);
  403. CellStyle cellStyle = workbook.createCellStyle();
  404. cellStyle.setWrapText(true);
  405. sourceCell.setCellStyle(cellStyle);
  406. }
  407. // 写入工作簿到文件系统
  408. try (FileOutputStream outputStream = new FileOutputStream(excelFilePath)) {
  409. workbook.write(outputStream);
  410. }
  411. }
  412. } catch (IOException e) {
  413. e.printStackTrace();
  414. }
  415. }
  416. /**
  417. * 获取txt里的名字用于匹配sheet进行填充
  418. * @param file file
  419. * @return String
  420. */
  421. public static String extractAppNameFromFile(File file) {
  422. // 定义正则表达式来匹配括号内的内容
  423. Pattern pattern = Pattern.compile("\\(([^)]+)\\)");
  424. // 转换文件名为字符串,以便使用正则表达式匹配
  425. String fileName = file.getName();
  426. // 创建matcher对象
  427. Matcher matcher = pattern.matcher(fileName);
  428. // 查找匹配项
  429. if (matcher.find()) {
  430. // 返回匹配到的括号内的内容
  431. return matcher.group(1);
  432. }
  433. System.out.println("txt文件名格式错误"+fileName);
  434. // 如果没有找到匹配项,返回null或空字符串(根据你的需求)
  435. return null; // 或者 ""
  436. }
  437. }