Java把html转成word
2016-06-03 11:04
363 查看
一、常用的html转word
1、工具:word2html、pdf转成word转换器等
2、Java代码:jacob和poi
三、poi把html转换成word
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、工具:word2html、pdf转成word转换器等
2、Java代码:jacob和poi
</pre><p></p><p></p><p>二、jacob把html转换成word</p><p><span style="margin:0px; padding:0px; color:rgb(57,57,57); font-family:verdana,'ms song',Arial,Helvetica,sans-serif; font-size:14px; line-height:21px"><span style="margin:0px; padding:0px"><span style="margin:0px; padding:0px">Jacob只能用于windows系统,如果你的系统不是windows,建议使用Openoffice.org,这个是跨平台的</span></span></span></p><p></p><p><pre name="code" class="java">/************* * JACOB方式 * notes:需要将jacob.dll拷贝到windows/system32和classpath路径下 * @param html html静态页面路径 * @param wordFile 要生成的word文档路径 */ public static void htmlToWord(String html, String wordFile) { ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word try { app.setProperty("Visible", new Variant(false)); Dispatch wordDoc = app.getProperty("Documents").toDispatch(); wordDoc = Dispatch.invoke(wordDoc, "Add", Dispatch.Method, new Object[0], new int[1]).toDispatch(); Dispatch.invoke(app.getProperty("Selection").toDispatch(), "InsertFile", Dispatch.Method, new Object[] { html, "", new Variant(false), new Variant(false), new Variant(false) }, new int[3]); Dispatch.invoke(wordDoc, "SaveAs", Dispatch.Method, new Object[] {wordFile, new Variant(1)}, new int[1]); Dispatch.call(wordDoc, "Close", new Variant(false)); } catch (Exception e) { e.printStackTrace(); } finally { app.invoke("Quit", new Variant[] {}); } }
三、poi把html转换成word
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;
}
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树