pom.xml配置:
修改POI的版本使其保持一致,不然会出现版本不匹配的问题
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>5.2.3</version>
</dependency>
添加xmlbeans (在生成PPT时出现报错发现的问题)
这是因为POI 在处理 Office 文档(如 PPTX、XLSX 等)时,会使用 xmlbeans 来解析和生成 XML 数据。现代 Office 文件格式(如 PPTX 和 XLSX)实际上是以 ZIP 压缩包形式存储的 XML 文件集合。
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>5.1.1</version>
</dependency>
后端word和pdf文字提取功能,路径写死了,无法跟前端文件上传按钮进行交互
我们是这样解决的:直接在前端实现文件的文字提取,再将前端提取的文字返回给后端进行下一个功能(调用api接口,根据上传的大纲生成具体内容)。
前端实现文字提取代码如下:
handleFileUpload(event) {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = async (e) => {
const arrayBuffer = e.target.result;
const fileExtension = file.name.split(".").pop().toLowerCase();
if (fileExtension === "doc" || fileExtension === "docx") {
this.fileContent = await this.extractWordText(arrayBuffer);
}
// Navigate to the outline page with the file content
this.$router.push({
path: '/outline',
query: { outline: this.fileContent }
});
};
reader.readAsArrayBuffer(file);
}
},
async extractWordText(arrayBuffer) {
try {
const result = await mammoth.extractRawText({ arrayBuffer: arrayBuffer });
return result.value;
} catch (error) {
console.error("Error extracting Word text:", error);
return "Error extracting Word text";
}
},