maven导入包:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.9</version>
</dependency>
java代码:
public static void htmlToWord2() throws Exception {
//拼一个标准的HTML格式文档
String body = "<html>" +
"<h1 style=\"margin: 0px; padding: 0px 0px 32px; font-size: 36px; font-weight: normal; font-family: 微软雅黑, 宋体; text-align: center;\">女子晚走一分钟被隔离在火锅店</h1>\n" +
"<p style=\"margin: 10px 0px 30px; padding: 0px; font-family: 微软雅黑, 宋体; font-size: 16px; text-indent: 2em;\">快递小哥、外卖小哥因为疫情防控突然被关在小区内的事儿大家应该听说过不少,但是见过被隔离在火锅店的吗?</p>\n" +
"<p style=\"margin: 10px 0px 30px; padding: 0px; font-family: 微软雅黑, 宋体; font-size: 16px; text-indent: 2em;\">3月18日,河南郑州,王女士下班后来到一家火锅店吃饭,有三个朋友先行离开,她和另一位许久未见的朋友多聊了会儿天,下楼结账之后正准备出门,突然被通知要暂时隔离在店内,原因是郑州新增1例新冠肺炎无症状感染者,曾在这家店就餐。</p>\n" +
"<p style=\"margin: 10px 0px 30px; padding: 0px; font-family: 微软雅黑, 宋体; font-size: 16px; text-indent: 2em;\"><strong style=\"margin: 0px; padding: 0px;\">王女士称,她就晚走了大概一分钟,就被留在了店内。</strong></p>\n" +
"<p style=\"margin: 10px 0px 30px; padding: 0px; font-family: 微软雅黑, 宋体; font-size: 16px; text-indent: 2em;\">除了她,还有三四十位就餐的,以及十几位工作人员,都被隔离在店里。</p>\n" +
"<p style=\"margin: 10px 0px 30px; padding: 0px; font-family: 微软雅黑, 宋体; font-size: 16px; text-indent: 2em;\"><strong style=\"margin: 0px; padding: 0px;\">店主大方地免费请隔离在此的客人吃火锅,而且菜品随便点,还准备了宵夜和早午餐,王女士等人已经连续吃了3天,还相当“凡尔赛”地称,没得选择,吃能吃肉,真的吃不动了,真的挺离谱的。</strong></p>\n" +
"<p><img id=\"resourceID{8012DA12-D088-4190-A7E4-B37107B4CA98}\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"http://10.0.127.238:11001/gateway/preview/SYPreview/V1.0/PreviewInfo/PreviewImage?resourceID=8012DA12-D088-4190-A7E4-B37107B4CA98\" width=\"50%\" /></p>\n" +
"<p> </p>\n" +
"<p><span style=\"font-family: 微软雅黑, 宋体; font-size: 16px; text-indent: 32px;\">女子晚走一分钟被隔离在火锅店:连吃3天 实在吃不动了!!</span></p>\n" +
"<p style=\"text-align: center;\"><video id=\"resourceID{C3E1097D-C657-4C26-B52A-B1FBCF4A9980}\" src=\"http://10.0.127.238:11001/gateway/preview/SYPreview/V1.0/PreviewInfo/PreviewPreview?resourceID=C3E1097D-C657-4C26-B52A-B1FBCF4A9980\" controls=\"controls\" width=\"50%\"></video></p>\n" +
"<p style=\"margin: 10px 0px 30px; padding: 0px; font-family: 微软雅黑, 宋体; font-size: 16px; text-indent: 2em; text-align: center;\"> </p>"
+"</html>";
String content = "<html><body>" + body + "</body></html>";
InputStream is = new ByteArrayInputStream(content.getBytes("UTF-8"));
OutputStream os = new FileOutputStream("D:\\1work\\jar\\ceshi\\2.doc");
inputStreamToWord(is, os);
}
/**
* 把is写入到对应的word输出流os中
* 不考虑异常的捕获,直接抛出
* @param is
* @param os
* @throws IOException
*/
public static void inputStreamToWord(InputStream is, OutputStream os) throws IOException {
POIFSFileSystem fs = new POIFSFileSystem();
//对应于org.apache.poi.hdf.extractor.WordDocument
fs.createDocument(is, "WordDocument");
fs.writeFilesystem(os);
os.close();
is.close();
}
关于微软office打不开问题:
摸爬滚打两小时,记住生成doc文件,不要生成docx文件