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