spire.doc操作word文档工具类(自用)
如果有相关问题,可以一起研究下
引入依赖
<dependencies> <dependency><groupId>e-iceblue</groupId><artifactId>spire.doc.free</artifactId><version>5.2.0</version></dependency>
</dependencies>
<repositories><repository><id>com.e-iceblue</id><name>e-iceblue</name><url>https://repo.e-iceblue.cn/repository/maven-public/</url></repository>
</repositories>
代码片段
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.DocumentObjectType;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.interfaces.ICompositeObject;
import com.spire.doc.interfaces.IDocumentObject;import javax.imageio.ImageIO;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;/*** @Author: Climbing-pit* @Date: 2022/4/15 21:25* @Description: Doc工具类*/
public class DocUtil {/*** 本地Doc文件提取Txt* @param filePath 文件路径* @param savePath 提取文件保存路径* @param tempFileName 文件名* @throws IOException*/public static void extractedText(String filePath, String savePath, long tempFileName) throws IOException {//加载Word文档Document doc = new Document();doc.loadFromFile(filePath);//获取文档中的文本保存为StringString text=doc.getText();//将String写入Txt文件writeStringToTxt(text,savePath+tempFileName+SystemConstant.DOC_TXT_SUFFIX);}/*** 本地Doc文件转Html* @param filePath 文件路径* @param savePath 提取文件保存路径* @param tempFileName 文件名* @throws IOException*/public static void toHtml(String filePath, String savePath, long tempFileName) throws IOException {//加载Word文档Document doc = new Document();doc.loadFromFile(filePath);//保存为HTML格式doc.saveToFile(savePath+tempFileName+ SystemConstant.DOC_HTML_SUFFIX,FileFormat.Html);extractImages(filePath, savePath);doc.dispose();}public static void writeStringToTxt(String content, String txtFileName) throws IOException {FileWriter fWriter= new FileWriter(txtFileName,true);try {fWriter.write(content);}catch(IOException ex){ex.printStackTrace();}finally{try{fWriter.flush();fWriter.close();} catch (IOException ex) {ex.printStackTrace();}}}/*** 本地Doc文件提取图片* @param filePath 文件路径* @param savePath 提取文件保存路径* @throws IOException*/public static void extractImages(String filePath, String savePath) throws IOException {//加载Word文档Document document = new Document();document.loadFromFile(filePath);//创建Queue对象Queue nodes = new LinkedList();nodes.add(document);//创建List对象List images = new ArrayList();//遍历文档中的子对象while (nodes.size() > 0) {ICompositeObject node = (ICompositeObject) nodes.poll();for (int i = 0; i < node.getChildObjects().getCount(); i++) {IDocumentObject child = node.getChildObjects().get(i);if (child instanceof ICompositeObject) {nodes.add(child);//获取图片并添加到Listif (child.getDocumentObjectType() == DocumentObjectType.Picture) {DocPicture picture = (DocPicture) child;images.add(picture.getImage());}}}}Integer size = images.size();//将图片保存为PNG格式文件for (int i = 0; i < size; i++) {File file = new File(String.format(savePath+"图片-%d.png", i));ImageIO.write((RenderedImage) images.get(i), "PNG", file);}}public static void main(String[] args) {long l = System.currentTimeMillis();String filePath = "D:\\files\\practice\\明日提升练习.docx";//创建保存路径String parentFilePath = filePath.substring(0, filePath.lastIndexOf(File.separatorChar)+1);String savePath = parentFilePath+SystemConstant.TEST_PAPER_SAVE_PATH+l+File.separator;File parent = new File(savePath);parent.mkdirs();try {DocUtil.extractedText(filePath, savePath, l);DocUtil.toHtml(filePath, savePath, l);} catch (IOException e) {e.printStackTrace();}}
}@Component
public class SystemConstant {public static final String DOC_HTML_SUFFIX= ".html";public static final String DOC_TXT_SUFFIX= "_ExtractedText.txt";public static final String TEST_PAPER_SAVE_PATH= "testPaper"+ File.separator;public static final String DOC_SAVE_PATH= "practice"+ File.separator;}
其他接口请看官网:帮助文档 | 全面丰富的在线文档,助您快速了解如何使用产品
相关问题:
1、创建word只支持500行问题
用free spire.Doc for Java这个是免费版,有一定限制, 导出的时候可生成的段落不能超过固定的数量,超过数量之后会报错,错误信息如下:
com.spire.doc.packages.sprcdz: Spire.Doc free version is limited to 500 paragraphs. This limitation is enforced during reading or writing files.
Upgrade to Commercial Edition of Spire.Doc <https://www.e-iceblue.com/Introduce/doc-for-java.html>at com.spire.doc.collections.DocumentObjectCollection.insert(Unknown Source)at com.spire.doc.packages.sprfww.spr︻∯—(Unknown Source)at com.spire.doc.packages.sprwcx.spr┰℠(Unknown Source)at com.spire.doc.fields.TextRange.spr┐——(Unknown Source)at com.spire.doc.DocumentObject.spr⌾——(Unknown Source)at com.spire.doc.DocumentObject.spr┰——(Unknown Source)at com.spire.doc.documents.Paragraph.spr┐——(Unknown Source)at com.spire.doc.DocumentObject.spr⌾——(Unknown Source)at com.spire.doc.DocumentObject.spr┰——(Unknown Source)at com.spire.doc.Body.spr┐——(Unknown Source)at com.spire.doc.packages.sprwcx.spr┄∯—(Unknown Source)at com.spire.doc.packages.sprajx.spr▄۩—(Unknown Source)at com.spire.doc.packages.sprajx.spr╽∮—(Unknown Source)at com.spire.doc.packages.sprwcx.spr∫┃(Unknown Source)at com.spire.doc.packages.spreyw.spr▄۩—(Unknown Source)at com.spire.doc.packages.spreyw.spr◐…(Unknown Source)at com.spire.doc.packages.spreyw.spr╻∮—(Unknown Source)at com.spire.doc.packages.spreyw.spr⌨∯—(Unknown Source)at com.spire.doc.Document.spr╼┈—(Unknown Source)at com.spire.doc.Document.saveToFile(Unknown Source)at com.cloud.common.util.DocUtil.writeStringToDoc(DocUtil.java:106)at com.cloud.spider.service.impl.BiqvgeServiceImplTest.spidedrChapter(BiqvgeServiceImplTest.java:138)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:675)at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:125)at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:132)at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:124)at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:74)at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:104)at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:62)at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:43)at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:35)at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:202)at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:198)at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135)at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:69)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)at java.util.ArrayList.forEach(ArrayList.java:1249)at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)at java.util.ArrayList.forEach(ArrayList.java:1249)at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:229)at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:197)at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:211)at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:191)at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
目前我自己的解决方式是,分为多次写入,测试用例通过!
spire.doc操作word文档工具类(自用)相关推荐
- spire.doc操作word文档
/*** 导入申报方案* 1.准备基础数据,* 2.将基本书签数据插入到word文档* 3.导入实施方案表格数据* 4.导入项目预算表格数据* 5.导入执行机构表格数据* @param bean* @ ...
- POI操作Word文档工具
POI操作Word文档工具 1.POI简单介绍 2.POI操作Word文档基本方法 3.POI操作Word文档基本方法应用 4.POI操作Word文档工具类 1.POI简单介绍 POIFS(可疑混淆执 ...
- 【ChatGPT】Spire.Doc 给 Word 文档设置背景颜色和背景图片
使用 Spire.Doc 给 Word 文档设置背景颜色和背景图片 1.首先需要引用 Spire.Doc 的命名空间: using Spire.Doc; using Spire.Doc.Documen ...
- Java使用Spire.Doc实现Word文档添加图片水印
通过本文你将学到: Spire.Doc是什么? 如何在项目中引入Spire.Doc依赖? 项目中基于Spire.Doc封装工具类实现Word文档添加图片水印? 一.Spire.Doc是什么? 1.Sp ...
- java 使用Spire.Doc实现Word文档插入图片
目录 使用步骤 1.引入依赖 2.关键代码 Spire.Doc for Java 是一款专业的 Java Word 组件,开发人员使用它可以轻松地将 Word 文档创建.读取.编辑.转换和打印等功能集 ...
- 国产文档处理控件Spire.Doc WPS Word文档功能上线!快速创建Word指南
Spire.Doc for Java 是一款专业的Java Word组件,开发人员使用它可以轻松地将Word文档创建.读取.编辑.转换和打印等功能集成到自己的Java应用程序中. 本文将介绍如何使用S ...
- Word控件Spire.Doc 【页面设置】教程(10) ;通过 Spire.Doc 在 Word 文档的不同部分添加页码
有时,开发人员需要在一个 Word 文档中为不同的部分添加页码,例如封面.目录和内容位于不同的部分.本文讨论如何通过Spire.Doc为不同部分添加页码. Spire.Doc for.NET 最新下载 ...
- 使用spire.doc实现word文档合并
下载jar包: http://repo.e-iceblue.com/nexus/content/groups/public/e-iceblue/spire.doc/ maven配置: 在这里插入代码片 ...
- Java操作Word文档
Java 生成 Word 的几种方案 参考 主要有这么一些工具可以使用 Jacob.Apache POI.Java2word.iText,还有一种方法是使用XML作为模板. 使用 XML 的思 ...
最新文章
- 遮掩java_关于java中的覆写、重载、隐藏、遮掩、遮蔽
- 四则运算第三次冲刺(项目完成)
- 大数据学习之路(七)——学习小结
- (学习c++primer5th的重要)c++ primer5th类指针版本hasptr (网上源代码错误) 定义错误
- Microsoft SharePoint Server 2010 的新增功能
- 增大mysql修改表空间_innodb系统表空间维护方法
- Spring Boot:Exception parsing document: template=index, line 7 - column 3
- 40 道数据挖掘面试真题大放送!
- Unity的状态机设计
- JAVASCIPT实现WEB SERVER的问题
- 计算5的阶乘 5!的结果是?
- 电感电容阻抗和公式记录
- 兼容安卓和苹果的滚动
- 家庭局域网_分析 | 如何查询局域网内在用的IP地址?
- Lotka–Volterra equation Competitive Lotka–Volterra equations
- 监控摄像机安装的正确位置是哪里
- android studio拨打电话代码,AndroidStudio实现拨打电话和发短信(kotlin)
- [2022]李宏毅深度学习与机器学习课程内容总结
- CVPR读书笔记[5]:Gabor特征提取之Gabor核的实现
- isArray 函数,转自 笨笨狗 blog
热门文章
- 基于python中jieba包的中文分词中详细使用(一)
- 艰难坎坷终到此,从此星光不问赶路人
- DGV中复选框第一列可编辑 ,第一列固定显示
- Pikachu靶场全关详细教学(一)
- 解决报错Cannot connect to the Maven process. Try again later. If the problem persists, check the Maven
- miui11可用的位置模拟器_miui11支持机型汇总_miui11支持哪些机型_软吧下载
- android平板性价比,性价比超高的安卓娱乐平板推荐
- Ty p e O R M框架
- 微信公众号(注册申请)
- SpringBoot下载文件的正确方式~