如果有相关问题,可以一起研究下
引入依赖
<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)
目前我自己的解决方式是,分为多次写入,测试用例通过!