From fe74eb879d3e4ca7514fee68daa3c3131fb0f5c8 Mon Sep 17 00:00:00 2001 From: luke Date: Sun, 25 May 2025 15:07:52 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4es=E4=B8=AD=E5=AD=98=E6=94=BE?= =?UTF-8?q?=E7=9A=84=E6=96=87=E4=BB=B6=E8=B7=AF=E5=BE=84=EF=BC=8C=E7=A1=AE?= =?UTF-8?q?=E4=BF=9D=E5=90=84=E7=AB=AF=E5=AF=BC=E5=85=A5=E7=9A=84=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E6=98=AF=E4=B8=80=E6=A0=B7=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../doc/parser/domain/impl/ExcelImporter.java | 72 +++++++++++++++++++ .../resources/application-dev-mac.properties | 1 + .../application-dev-windows.properties | 3 +- .../resources/application-docker.properties | 2 +- 4 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/doc/parser/domain/impl/ExcelImporter.java diff --git a/src/main/java/com/doc/parser/domain/impl/ExcelImporter.java b/src/main/java/com/doc/parser/domain/impl/ExcelImporter.java new file mode 100644 index 0000000..536cc2f --- /dev/null +++ b/src/main/java/com/doc/parser/domain/impl/ExcelImporter.java @@ -0,0 +1,72 @@ +package com.doc.parser.domain.impl; + +/** + * @author Luke.ye + * @date 2025/5/25 14:54 + */ +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import java.io.FileInputStream; +import java.io.IOException; +import java.nio.file.Path; +import java.util.Iterator; + +@Component +public class ExcelImporter extends AbstractBaseFileImporter { + + @Value("${excel.path}") + private String excelPath; + + @Override + protected String getDirectoryPath() { + return excelPath; + } + + @Override + protected String getFileSuffix() { + return ".xlsx"; + } + + @Override + protected String getDocTypeCode() { + return "excel"; + } + + @Override + protected String extractContent(Path path) throws IOException { + try (FileInputStream fis = new FileInputStream(path.toFile()); + Workbook workbook = new XSSFWorkbook(fis)) { + + Sheet sheet = workbook.getSheetAt(0); + StringBuilder sb = new StringBuilder(); + Iterator rowIterator = sheet.iterator(); + + if (rowIterator.hasNext()) rowIterator.next(); // 跳过表头 + + while (rowIterator.hasNext()) { + Row row = rowIterator.next(); + String title = getCellString(row.getCell(0)); + String content = getCellString(row.getCell(1)); + + if (!title.isBlank()) { + sb.append("【标题】").append(title).append("\n"); + } + if (!content.isBlank()) { + sb.append(content).append("\n\n"); + } + } + + return sb.toString().trim(); + } + } + + private String getCellString(Cell cell) { + if (cell == null) return ""; + cell.setCellType(CellType.STRING); + return cell.getStringCellValue().trim(); + } +} + diff --git a/src/main/resources/application-dev-mac.properties b/src/main/resources/application-dev-mac.properties index 3cbf029..9e7d173 100644 --- a/src/main/resources/application-dev-mac.properties +++ b/src/main/resources/application-dev-mac.properties @@ -26,3 +26,4 @@ exclude.file.path.prefix=/Users/admin/Desktop/ahnx-share-src-public/public markdown.path=/Users/admin/Desktop/ahnx-share-src-public/public pdf.path=/Users/admin/Desktop/ahnx-share-src-public/public word.path=/Users/admin/Desktop/ahnx-share-src-public/public +excel.path=/Users/admin/Desktop/ahnx-share-src-public/public diff --git a/src/main/resources/application-dev-windows.properties b/src/main/resources/application-dev-windows.properties index 597abe9..eaa12ff 100644 --- a/src/main/resources/application-dev-windows.properties +++ b/src/main/resources/application-dev-windows.properties @@ -25,4 +25,5 @@ arthas.tunnel-server=ws://101.132.255.39:7777/ws exclude.file.path.prefix=D:/02-documents/01-ahnx-share-src-public/public markdown.path=D:/02-documents/01-ahnx-share-src-public/public pdf.path=D:/02-documents/01-ahnx-share-src-public/public -word.path=D:/02-documents/01-ahnx-share-src-public/public \ No newline at end of file +word.path=D:/02-documents/01-ahnx-share-src-public/public +excel.path=D:/02-documents/01-ahnx-share-src-public/public \ No newline at end of file diff --git a/src/main/resources/application-docker.properties b/src/main/resources/application-docker.properties index b0481f3..c562b53 100644 --- a/src/main/resources/application-docker.properties +++ b/src/main/resources/application-docker.properties @@ -27,4 +27,4 @@ pdf.path=/app/import-data word.path=/app/import-data # 相对路径裁剪 -exclude.file.path.prefix=/app/ \ No newline at end of file +exclude.file.path.prefix=/app/import-data \ No newline at end of file