700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > freemarker生成word文档无法用office打开问题

freemarker生成word文档无法用office打开问题

时间:2018-04-21 18:57:22

相关推荐

freemarker生成word文档无法用office打开问题

错误原因:

利用freemarker生成的word文档利用notepad打开是xml格式。而正常的文档格式利用notepad打开是乱码,需要转换。

代码案例:

import com.aspose.words.Document;import com.aspose.words.License;import com.aspose.words.SaveFormat;import java.io.*;public class xmlToDocx{private static boolean getLicense() {boolean result = false;try {InputStream inputSream= xmlToDocx.class.getClassLoader().getResourceAsStream("license.xml");License license= new License();license.setLicense(inputSream);result = true;} catch (Exception e) {e.printStackTrace();}return result;}//xml转成word.docxpublic static byte[] xmlToWord(byte[] input) {if (!getLicense()) { //解决水印问题return null;}ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();try {Document doc = new Document(new ByteArrayInputStream(input));doc.save(byteArrayOutputStream, SaveFormat.DOCX);return byteArrayOutputStream.toByteArray();} catch (Exception e) {e.printStackTrace();return null;}finally {try {byteArrayOutputStream.close();} catch (IOException e) {e.printStackTrace();}}}}

Aspose-word以及license.xml下载地址:

/download/qq_43185415/10669384

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。