Java 将html转换word
2015-11-10 14:58
423 查看
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 Tester {
public static boolean writeWordFile() {
boolean w = false;
String path = "E:/";
try {
if (!"".equals(path)) {
// 检查目录是否存在
File fileDir = new File(path);
if (fileDir.exists()) {
// 生成临时文件名称
String fileName = "a.doc";
String content = "<html><meta http-equiv='Content-Type' content='text/html; charset=utf-8' />" +
"<head>你好</head>" +
"<body>" +
"<table>" +
"<tr>" +
"<td>信息1</td>" +
"<td>信息2</td>" +
"<td>t3</td>" +
"<tr>" +
"</table>" +
"</body>" +
"</html>";
byte b[] = content.getBytes();
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);
bais.close();
ostream.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
return w;
}
public static void main(String[] args){
writeWordFile();
}
}
所需jar包 poi.jar http://download.csdn.net/detail/hackxiaof/9257831
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 Tester {
public static boolean writeWordFile() {
boolean w = false;
String path = "E:/";
try {
if (!"".equals(path)) {
// 检查目录是否存在
File fileDir = new File(path);
if (fileDir.exists()) {
// 生成临时文件名称
String fileName = "a.doc";
String content = "<html><meta http-equiv='Content-Type' content='text/html; charset=utf-8' />" +
"<head>你好</head>" +
"<body>" +
"<table>" +
"<tr>" +
"<td>信息1</td>" +
"<td>信息2</td>" +
"<td>t3</td>" +
"<tr>" +
"</table>" +
"</body>" +
"</html>";
byte b[] = content.getBytes();
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);
bais.close();
ostream.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
return w;
}
public static void main(String[] args){
writeWordFile();
}
}
所需jar包 poi.jar http://download.csdn.net/detail/hackxiaof/9257831
相关文章推荐
- eclipse使用svn开发Android项目
- maven安装以及eclipse配置maven 和maven Integration for eclipse
- java 排序
- 【Java并发编程】:使用synchronized获取互斥锁的几点说明
- 【Java并发编程】:图文讲述同步的另一个重要功能:内存可见性
- JAVA
- spring mvc实现修改+删除
- Struts2拦截器
- 【Java并发编程】:volatile变量修饰符—意料之外的问题(含代码)
- Ubuntu 安装 Sun JDK
- 【Java并发编程】:第五篇中volatile意外问题的正确分析解答(含代码)
- Java中交换两个数的值 三个方法
- Java中函数参数的值传递与引用传递问题
- 【Java并发编程】:图文讲述同步的另一个重要功能:内存可见性
- 异常类信息查询地址
- 【Java并发编程】:并发编程中实现内存可见的两种方法比较:加锁和volatile变量
- java 图片获取宽高
- java implement
- 关于JDK1.7中LinkedList索引方法的思考
- java.sql.Types,数据库字段类型,java数据类型的对应关系