直接把Html文本写入到Word文件

  1. 获取查看页面的body内容和引用的css文件路径传入到后台。
  2. 把对应css文件的内容读取出来。
  3. 利用body内容和css文件的内容组成一个标准格式的Html文本。
  4. 根据组合后的Html文本生成对应的ByteArrayInputStream。
  5. 构建一个默认的POIFSFileSystem,并利用它和生成的ByteArrayInputStream创建一个WordDocument。
  6. 把构建的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所写)

最新文章

  1. WebSocket异常 通常每个套接字地址(协议/网络地址/端口)只允许使用一次
  2. UVa 442 (栈) Matrix Chain Multiplication
  3. Python基础:1.数据类型(元组)
  4. 遇到autoreconf: not found
  5. CSS控制表单
  6. HeapAnalyzer
  7. FastJson中@JSONField注解使用
  8. 安装uwsgi
  9. JDBC连接数据库的几种方法
  10. 51Nod 1110 距离之和最小 V3 中位数 思维
  11. 使用+Leapms查看线性规划的单纯形表,itsme命令
  12. 创建一个Windows服务程序与实现定时器效果
  13. 在没联网环境下,启动tomcat出错
  14. hostapd、/dev/random、/dev/urandom
  15. layui数据表格监听按钮问题
  16. Node核心模块
  17. C# 字符串 输出格式 指定间隔 通用性很强
  18. VueRouter 源码深度解析
  19. jmeter报错:响应数据HTTP Status 500 &amp; 后台日志Typed variable declaration : Object constructor
  20. Actions对Element的一些操作解析

热门文章

  1. C++构造函数和拷贝构造函数详解
  2. Weekly Contest 78--------&gt;811. Subdomain Visit Count (split string with space and hash map)
  3. 网络工程师HCNA认证学习笔记Day1
  4. IDEA安装actiBPM插件,亲测成功!避免直接在线安装或下载jar包硬盘都会报错问题!
  5. 构建使用SQL服务器的ASP.net Core2.0 API
  6. 前端开发 - Emmet使用手册
  7. [BZOJ3128]a+b Problem
  8. [aspnetcore]asp.net core程序部署到Ubuntu中的路径问题
  9. SNMP4J 总结
  10. 轻松把玩HttpClient之配置ssl,采用绕过证书验证实现https