POI把html写入word doc文件
2024-08-30 11:32:22
直接把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所写)
最新文章
- WebSocket异常 通常每个套接字地址(协议/网络地址/端口)只允许使用一次
- UVa 442 (栈) Matrix Chain Multiplication
- Python基础:1.数据类型(元组)
- 遇到autoreconf: not found
- CSS控制表单
- HeapAnalyzer
- FastJson中@JSONField注解使用
- 安装uwsgi
- JDBC连接数据库的几种方法
- 51Nod 1110 距离之和最小 V3 中位数 思维
- 使用+Leapms查看线性规划的单纯形表,itsme命令
- 创建一个Windows服务程序与实现定时器效果
- 在没联网环境下,启动tomcat出错
- hostapd、/dev/random、/dev/urandom
- layui数据表格监听按钮问题
- Node核心模块
- C# 字符串 输出格式 指定间隔 通用性很强
- VueRouter 源码深度解析
- jmeter报错:响应数据HTTP Status 500 &; 后台日志Typed variable declaration : Object constructor
- Actions对Element的一些操作解析
热门文章
- C++构造函数和拷贝构造函数详解
- Weekly Contest 78-------->;811. Subdomain Visit Count (split string with space and hash map)
- 网络工程师HCNA认证学习笔记Day1
- IDEA安装actiBPM插件,亲测成功!避免直接在线安装或下载jar包硬盘都会报错问题!
- 构建使用SQL服务器的ASP.net Core2.0 API
- 前端开发 - Emmet使用手册
- [BZOJ3128]a+b Problem
- [aspnetcore]asp.net core程序部署到Ubuntu中的路径问题
- SNMP4J 总结
- 轻松把玩HttpClient之配置ssl,采用绕过证书验证实现https