实现方式:主要是通过aspose.word将html转为word(有轻微的格式问题),再使用jacob调用word宏进行格式调整。
实现步骤
1.导入aspose、jacob的jar以及license.xml
<!-- aspose-words 文档操作工具包 -->
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>18.6</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/aspose-words-18.6-jdk16.jar</systemPath>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>8.5.2</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/aspose-cells-8.5.2.jar</systemPath>
</dependency>
<!-- jacob打开word的工具包 -->
<dependency>
<groupId>net.sf.jacob-project</groupId>
<artifactId>jacob</artifactId>
<version>1.14.3</version>
</dependency>
<!-- FileUtil工具包 -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool-all.version}</version>
</dependency>
license.xml直接放在resources包下即可
2.html转换word代码
package com.J2011;
import static org.junit.Assert.assertTrue;
import cn.hutool.core.io.FileUtil;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import java.io.File;
import java.io.InputStream;
/**
* Unit test for simple App.
*/
public class AppTest
{
/**
* Rigorous Test :-)
*/
@Test
public void shouldAnswerWithTrue()
{
assertTrue( true );
}
@Test
public void html2Word() throws Exception {
String htmlFilePath = "E:\\html\\1正数与负数.html";
String wordFilePath = "E:\\html\\99999.docx";
//读取html文件返回btye数组
byte[] readBytes = FileUtil.readBytes(htmlFilePath);
//初始化
init();
//调用aspose,创建document对象
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertHtml(new String(readBytes));
//保存为docx格式或者是doc格式
doc.save(wordFilePath, SaveFormat.DOCX);
}
//初始化,去除转成word后的水印
public static void init() {
try {
// log.info("实现`aspose-words`授权 -> 去掉头部水印");
System.out.println("实现`aspose-words`授权 -> 去掉头部水印");
/*
实现匹配文件授权 -> 去掉头部水印 `Evaluation Only. Created with Aspose.Words. Copyright 2003-2018 Aspose Pty Ltd.` |
`Evaluation Only. Created with Aspose.Cells for Java. Copyright 2003 - 2020 Aspose Pty Ltd.`
*/
InputStream is = new ClassPathResource("license.xml").getInputStream();
License license = new License();
license.setLicense(is);
} catch (Exception e) {
// log.error("《`aspose-words`授权》 失败: {}", e.getMessage());
// System.out.println("《`aspose-words`授权》 失败: {}", e.getMessage());
}
}
}
转换出的word会有点格式问题,我这里出现的是文本框与字符不在同一水平线上,通过录制宏使用jacob调用宏。
使用jacob需要先在本地C:\Windows\System32路径下粘贴一个jacob.dll文件即可使用。
//启动本地的office word
ActiveXComponent word = new ActiveXComponent("Word.Application");
Dispatch documents = word.getProperty("Documents").toDispatch();
//指定要打开的文档并且打开它
Dispatch document = Dispatch.call(documents, "Open", wordFilePath).toDispatch();
//在这个文档上运行宏
Dispatch.call(word, "Run", new Variant("macro1"));
System.out.println("word转换成功!!!!!");
网页效果:
转成word后效果:
这样就转换成功啦,如果对你有用的话记得点赞哦!!!