html转word文档

package cn.com.szhtkj.util;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem; public class testMain {
public static boolean writeWordFile() {
boolean w = false;
String path = "d:/";
try {
if (!"".equals(path)) {
// 检查目录是否存在
File fileDir = new File(path);
if (fileDir.exists()) {
// 生成临时文件名称
String fileName = "a11.doc";
// 使用传html 转word
String contentConcat = "<html>" + "<meta charset='UTF-8'>" + "<head>信息</head>" + "<body>"
+ "<table>" + "<tr>" + "<td style='color:red;border:1px solid red;'>姓名</td>"
+ "<td style='font-size:36px;border:1px solid red;'>性别</td>" + "<tr>" + "</table>"
+ "</body>" + "</html>";
byte b[] = contentConcat.getBytes("UTF-8");
ByteArrayInputStream bais = new ByteArrayInputStream(b);
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
FileOutputStream ostream = new FileOutputStream(path + fileName);
poifs.writeFilesystem(ostream);
ostream.close();
poifs.close();
bais.close();
/*
* 访问桌面上的html转word ByteArrayOutputStream bos = new ByteArrayOutputStream();
* FileInputStream fis = new
* FileInputStream("C:\\Users\\ghost\\Desktop\\1.html"); int b = -1; while ((b =
* fis.read()) != -1) { bos.write(b); } fis.close(); bos.close();
* ByteArrayInputStream bais = new ByteArrayInputStream(bos.toByteArray());
* POIFSFileSystem poifs = new POIFSFileSystem(); DirectoryEntry directory =
* poifs.getRoot(); DocumentEntry documentEntry =
* directory.createDocument("WordDocument", bais); FileOutputStream ostream =
* new FileOutputStream(path + fileName); poifs.writeFilesystem(ostream);
* bais.close(); ostream.close();
*/
}
} } catch (IOException e) {
e.printStackTrace();
} return w;
} public static void main(String[] args) {
writeWordFile();
} }

最新文章

  1. Vue.js的入门
  2. windows 64位 安装apache+php+mysql
  3. Codeforces 144D Missile Silos 最短路
  4. xamarin.android之 Android 4.4+ 获取图片真实路径
  5. bzoj 2815 灾难
  6. ODB 短板
  7. Curious Robin Hood(树状数组+线段树)
  8. PropertyChangeSupport的使用
  9. 使用OpenCL
  10. java-枚举一些字典信息的例子
  11. WordPress-基础设置之阅读设置
  12. 不怕你配置不对,就怕你看的资料不对!MIM 与 SharePoint 同步完全配置指南。
  13. Python爬虫10-页面解析数据提取思路方法与简单正则应用
  14. Https,Http,TCP,IP的一些理解
  15. js——class基础
  16. Confluence 6 在你的 LDAP 目录中优化用户和用户组数量
  17. pytest文档14-函数传参和firture传参数request
  18. Mac Terminal open app with a file opened
  19. Dockerfile文件制作自己的镜像
  20. 使用Python解析JSON详解

热门文章

  1. 在使用easyui datagrid在tab中遇到的问题
  2. 如何使用图形界面Webmin管理linux服务器
  3. Git diff 代码比较的高级技巧
  4. 一些BFC
  5. HDU 1005 Number Sequence(找规律)
  6. Android开发进度04
  7. Laravel源码解析之从入口开始
  8. django-10-中间件和上下文管理器
  9. maven 测试写入JRE参数
  10. Linux5355端口被0.0.0.0监听