POI把html写入word doc文件
直接把Html文本写入到Word文件
- 获取查看页面的body内容和引用的css文件路径传入到后台。
- 把对应css文件的内容读取出来。
- 利用body内容和css文件的内容组成一个标准格式的Html文本。
- 根据组合后的Html文本生成对应的ByteArrayInputStream。
- 构建一个默认的POIFSFileSystem,并利用它和生成的ByteArrayInputStream创建一个WordDocument。
- 把构建的POIFSFileSystem写入到对应的输出流。
经过上面这几步之后我们就可以把Html格式的文本写入到Word文件中,同时使生成的Word文件呈现出对应的Web样式。需要注意的是原本Html文件中引用到的css文件的内容需要放到生成的Word文件中,生成后的Word文件才会呈现出对应的Web样式。下面是一个针对于该方式的一个简单例子:
public void htmlToWord2() throws Exception {
InputStream bodyIs = new FileInputStream("f:\\1.html");
InputStream cssIs = new FileInputStream("f:\\1.css");
String body = this.getContent(bodyIs);
String css = this.getContent(cssIs);
//拼一个标准的HTML格式文档
String content = "<html><head><style>" + css + "</style></head><body>" + body + "</body></html>";
InputStream is = new ByteArrayInputStream(content.getBytes("GBK"));
OutputStream os = new FileOutputStream("f:\\1.doc");
this.inputStreamToWord(is, os);
}
/**
* 把is写入到对应的word输出流os中
* 不考虑异常的捕获,直接抛出
* @param is
* @param os
* @throws IOException
*/
private 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();
}
/**
* 把输入流里面的内容以UTF-8编码当文本取出。
* 不考虑异常,直接抛出
* @param ises
* @return
* @throws IOException
*/
private String getContent(InputStream... ises) throws IOException {
if (ises != null) {
StringBuilder result = new StringBuilder();
BufferedReader br;
String line;
for (InputStream is : ises) {
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
while ((line=br.readLine()) != null) {
result.append(line);
}
}
return result.toString();
}
return null;
}
1.css代码如下
table {
border: 1px solid blue;
width: 800px;
height: 500px;
text-align: center;
}
td {
width: 200px;
border: 1px solid blue;
}
1.html对应的内容如下:
<table cellpadding="" style="border-collapse: collapse;">
<tr>
<td>中文</td>
<td>中文</td>
<td>中文</td>
<td>中文</td>
</tr>
<tr>
<td>中文</td>
<td>中文</td>
<td>中文</td>
<td>中文</td>
</tr>
</table>
效果图

(注:本文是基于poi3.9所写)
POI把html写入word doc文件的更多相关文章
- 使用POI读写Word doc文件
使用POI读写word doc文件 目录 1 读word doc文件 1.1 通过WordExtractor读文件 1.2 通过HWPFDocument读文件 2 写w ...
- android使用POI读写word doc文件
目录 1 读word doc文件 1.1 通过WordExtractor读文件 1.2 通过HWPFDocument读文件 2 写word doc文件 Apache p ...
- POI写入word doc 03 模板的实例
在使用POI写word doc文件的时候我们必须要先有一个doc文件才行,因为我们在写doc文件的时候是通过HWPFDocument来写的,而HWPFDocument是要依附于一个doc文件的.所以通 ...
- 使用POI转换word doc文件
目录 1 转换为Html文件 2 转换为Xml文件 3 转换为Text文件 在POI中还存在有针对于word doc文件进行格式转换的功能.我们可以将word的内容 ...
- POI转换word doc文件为(html,xml,txt)
在POI中还存在有针对于word doc文件进行格式转换的功能.我们可以将word的内容转换为对应的Html文件,也可以把它转换为底层用来描述doc文档的xml文件,还可以把它转换为底层用来描述doc ...
- VBA/VBScript提取Word(*.doc)文件中包含的图片(照片)
VBA/VBScript提取Word(*.doc)文件中包含的图片(照片) 要处理的人事简历表是典型的Word文档,其中一人一份doc,里面包含有个人的照片,如果要把里面的照片复制出来就比较麻烦了 ...
- POI读word doc 03 文件的两种方法
Apache poi的hwpf模块是专门用来对word doc文件进行读写操作的.在hwpf里面我们使用HWPFDocument来表示一个word doc文档.在HWPFDocument里面有这么几个 ...
- POI读写Word docx文件
使用POI读写word docx文件 目录 1 读docx文件 1.1 通过XWPFWordExtractor读 1.2 通过XWPFDocument读 2 写docx ...
- Java将数据写入word文档(.doc)
Java可用org.apache.poi包来操作word文档.org.apache.poi包可于官网上下载,解压后各jar作用如下图所示: 可根据需求导入对应的jar. 一.HWPFDocument类 ...
随机推荐
- 走进VR游戏开发的世界
http://geek.csdn.net/news/detail/76504 我们组在2014年下半年尝试开发了一款Xbox One平台的体感游戏,2015年上半年进行收尾工作的同时,结合之前积累的体 ...
- [HNOI2010] 弹飞绵羊 bounce
标签:分块.题解: 200000,而且标号从0开始,很符合分块的条件啊.看看怎么实现. 首先分成√n个区间,然后如果我们对于每一个位置i,求出一个Next[i]和step[i],分别表示跳到的后一个位 ...
- SpringBoot | Thymeleaf | 局部更新
建立一个实体类: public class Fruit { int id; String name; public Fruit() { } public Fruit(int id, String na ...
- JDBC | 查询表数据行数
两种方法: 1. "select * from userinfo" 利用ResultSet的last和getRow方法来获得ResultSet的总行数,适用于在查询数据的同时统 ...
- idea | gitee 码云
https://blog.csdn.net/qq_32340877/article/details/81205547
- 【bzoj1123】BLO
1123: [POI2008]BLO Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2222 Solved: 1090[Submit][Status ...
- Technocup 2017 - Elimination Round 1 (Unofficially Open for Everyone, Rated for Div. 2) D
The organizers of a programming contest have decided to present t-shirts to participants. There are ...
- 【aspnetcore】异常捕捉可用知识点
1.使用过滤器ExceptionFilter:补充:常用过滤器:AuthorizationFilter.ActionFilter.ResultFilter.ResourceFilter.Excepti ...
- 持续集成~Jenkins里的NuGet和MSBuild插件
Jenkins是一个持续集成的环境,它是java开发的,大叔认为它的工作流程是 从源代码拉一个项目下来到它本地(可以配置定时机制) 恢复相关程序包nuget 编译程序 发布程序 现在说一下在配置jen ...
- vue http请求 vue-resource使用方法
1.安装vue-resource扩展: npm install vue-resource 2.在main.js中引入 import http from 'vue-resource' 3.使用方法 // ...