java 将富文本转化为word,保留原本格式
通过java,将富文本转化为word
·
private String profile;
public String generateContractString(String afterTreatment, String projectId, String builderName) {
try {
//导出地址
int year = Calendar.getInstance().get(Calendar.YEAR);
int month = Calendar.getInstance().get(Calendar.MONTH) + 1;
int day = Calendar.getInstance().get(Calendar.DATE);
String yearStr = String.valueOf(year);
String monthStr = String.valueOf(month);
String dayStr = String.valueOf(day);
String head = profile;
String filePath = projectId + "/contract";
String dirPrefix =
filePath + "/fileupload/" + yearStr + File.separator + monthStr + File.separator + dayStr + "/";
// 文件夹地址
File path = new File(head + dirPrefix);
if (!path.exists()) {
path.mkdirs();
}
String name = IdUtil.simpleUUID() + builderName + ".html";
String docName = IdUtil.simpleUUID() + builderName + ".docx";
FileUtil.touch(head + dirPrefix + name);
boolean blg = false;
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(head + dirPrefix + name));
String a = "" + afterTreatment;
byte[] b = (a).getBytes();
bw.write(afterTreatment);
bw.close();
Document doc = new Document();
doc.loadFromFile(head + dirPrefix + name, FileFormat.Html, XHTMLValidationType.None);
//保存为Word格式到指定路径
doc.saveToFile(head + dirPrefix + docName, FileFormat.Docx_2013);
} catch (IOException e) {
e.printStackTrace();
}
return dirPrefix + docName;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
大体思路是依靠e-iceblue来实现转化,先将富文本的内容存入到html中,再通过e-iceblue将html转化为word,并保留原本的格式
所需依赖
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>5.2.0</version>
</dependency>
更多推荐
所有评论(0)