public static void main(String[] args) throws IOException {
StringBuffer sb = new StringBuffer();
sb.append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:w=\"urn:schemas-microsoft-com:office:word\" xmlns:m=\"http://schemas.microsoft.com/office/2004/12/omml\" xmlns=\"http://www.w3.org/TR/REC-html40\" >");
sb.append("<head></head>");
sb.append("<body>");
sb.append(text);
sb.append("</body>");
sb.append("</html>");
//中文编码
ByteArrayInputStream bais = null;
FileOutputStream targetWord = null;
try {
byte[] bArr = sb.toString().getBytes("UTF-8");
bais = new ByteArrayInputStream(bArr);
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
//文件暂时放在本机下载文件夹
targetWord = new FileOutputStream(new File(path + "测试word.docx"));
poifs.writeFilesystem(targetWord);
targetWord.flush();
return;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != bais) {
bais.close();
}
if (null != targetWord) {
targetWord.close();
}
}
}
最终导出的docx文件在wps打开没问题,在office word打开就报错