错误原因:
利用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