兼容doc/xls格式的文件
This commit is contained in:
parent
9ec83eec49
commit
9670477f51
10
pom.xml
10
pom.xml
@ -39,6 +39,7 @@
|
||||
<pdfbox.version>2.0.29</pdfbox.version>
|
||||
<poi.version>5.2.3</poi.version>
|
||||
<poi-ooxml.version>5.2.3</poi-ooxml.version>
|
||||
<poi-scratchpad.version>5.2.3</poi-scratchpad.version>
|
||||
|
||||
<mysql-connector-java.version>8.0.33</mysql-connector-java.version>
|
||||
<mybatis-plus-boot-starter.version>3.5.5</mybatis-plus-boot-starter.version>
|
||||
@ -190,6 +191,11 @@
|
||||
<artifactId>poi-ooxml</artifactId>
|
||||
<version>${poi-ooxml.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>poi-scratchpad</artifactId>
|
||||
<version>${poi-scratchpad.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- MySQL JDBC 驱动 -->
|
||||
<dependency>
|
||||
@ -346,6 +352,10 @@
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>poi-ooxml</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>poi-scratchpad</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
|
||||
@ -1,9 +1,7 @@
|
||||
package com.knowledge.base.domain.doc.service.impl;
|
||||
|
||||
/**
|
||||
* @author Luke.ye
|
||||
* @date 2025/5/25 14:54
|
||||
*/
|
||||
|
||||
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
@ -15,6 +13,10 @@ import java.nio.file.Path;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author Luke.ye
|
||||
* @date 2025/5/25 14:54
|
||||
*/
|
||||
@Component
|
||||
public class ExcelImporter extends AbstractBaseFileImporter {
|
||||
|
||||
@ -43,8 +45,11 @@ public class ExcelImporter extends AbstractBaseFileImporter {
|
||||
|
||||
@Override
|
||||
protected String extractContent(Path path) throws IOException {
|
||||
String fileName = path.getFileName().toString().toLowerCase();
|
||||
boolean isXlsx = fileName.endsWith(".xlsx");
|
||||
|
||||
try (FileInputStream fis = new FileInputStream(path.toFile());
|
||||
Workbook workbook = new XSSFWorkbook(fis)) {
|
||||
Workbook workbook = isXlsx ? new XSSFWorkbook(fis) : new HSSFWorkbook(fis)) {
|
||||
|
||||
Sheet sheet = workbook.getSheetAt(0);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package com.knowledge.base.domain.doc.service.impl;
|
||||
|
||||
import com.knowledge.base.domain.common.enums.DocTypeEnum;
|
||||
import org.apache.poi.hwpf.HWPFDocument;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
|
||||
@ -44,12 +45,28 @@ public class WordImporter extends AbstractBaseFileImporter {
|
||||
|
||||
@Override
|
||||
protected String extractContent(Path path) throws IOException {
|
||||
try (XWPFDocument document = new XWPFDocument(new FileInputStream(path.toFile()))) {
|
||||
StringBuilder content = new StringBuilder();
|
||||
for (XWPFParagraph para : document.getParagraphs()) {
|
||||
content.append(para.getText()).append("\n");
|
||||
String fileName = path.getFileName().toString().toLowerCase();
|
||||
|
||||
if (fileName.endsWith(".docx")) {
|
||||
// 处理 .docx
|
||||
try (FileInputStream fis = new FileInputStream(path.toFile());
|
||||
XWPFDocument document = new XWPFDocument(fis)) {
|
||||
|
||||
StringBuilder content = new StringBuilder();
|
||||
for (XWPFParagraph para : document.getParagraphs()) {
|
||||
content.append(para.getText()).append("\n");
|
||||
}
|
||||
return content.toString().trim();
|
||||
}
|
||||
return content.toString().trim();
|
||||
} else if (fileName.endsWith(".doc")) {
|
||||
// 处理 .doc
|
||||
try (FileInputStream fis = new FileInputStream(path.toFile());
|
||||
HWPFDocument doc = new HWPFDocument(fis)) {
|
||||
|
||||
return doc.getDocumentText().trim();
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("不支持的 Word 文件类型: " + fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user