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