Java编程琐事(8)——java将html导出到word
2010-04-14 15:44
375 查看
一、第三方jar包下载:
在java中将html文件导出到word需要应用到第三方的jar包:采用poi-bin-3.0-FINAL-20070503.zip。可以到http://poi.apache.org/官方网站下载最新版本。
二、开发思路:
采用Java IO将html文件读入到一个临时的String对象中,然后采用poi提供的API生成word文档。
三、开发源代码:
package com.solid.util;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
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;
/**
* 将html文档转为doc
* @author soildwang
*
*/
public class HtmlToDoc {
/**
* 读取html文件到word
* @param filepath html文件的路径
* @return
* @throws Exception
*/
public boolean writeWordFile(String filepath) throws Exception {
boolean flag = false;
ByteArrayInputStream bais = null;
FileOutputStream fos = null;
String path = "C:/"; //根据实际情况写路径
try {
if (!"".equals(path)) {
File fileDir = new File(path);
if (fileDir.exists()) {
String content = readFile(filepath);
byte b[] = content.getBytes();
bais = new ByteArrayInputStream(b);
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
fos = new FileOutputStream(path + "temp.doc");
poifs.writeFilesystem(fos);
bais.close();
fos.close();
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fos != null) fos.close();
if(bais != null) bais.close();
}
return flag;
}
/**
* 读取html文件到字符串
* @param filename
* @return
* @throws Exception
*/
public String readFile(String filename) throws Exception {
StringBuffer buffer = new StringBuffer("");
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(filename));
buffer = new StringBuffer();
while (br.ready())
buffer.append((char) br.read());
} catch (Exception e) {
e.printStackTrace();
} finally {
if(br!=null) br.close();
}
return buffer.toString();
}
public static void main(String[] args) throws Exception {
new HtmlToDoc().writeWordFile("C:/preview4510.html");
}
}
在java中将html文件导出到word需要应用到第三方的jar包:采用poi-bin-3.0-FINAL-20070503.zip。可以到http://poi.apache.org/官方网站下载最新版本。
二、开发思路:
采用Java IO将html文件读入到一个临时的String对象中,然后采用poi提供的API生成word文档。
三、开发源代码:
package com.solid.util;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
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;
/**
* 将html文档转为doc
* @author soildwang
*
*/
public class HtmlToDoc {
/**
* 读取html文件到word
* @param filepath html文件的路径
* @return
* @throws Exception
*/
public boolean writeWordFile(String filepath) throws Exception {
boolean flag = false;
ByteArrayInputStream bais = null;
FileOutputStream fos = null;
String path = "C:/"; //根据实际情况写路径
try {
if (!"".equals(path)) {
File fileDir = new File(path);
if (fileDir.exists()) {
String content = readFile(filepath);
byte b[] = content.getBytes();
bais = new ByteArrayInputStream(b);
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
fos = new FileOutputStream(path + "temp.doc");
poifs.writeFilesystem(fos);
bais.close();
fos.close();
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fos != null) fos.close();
if(bais != null) bais.close();
}
return flag;
}
/**
* 读取html文件到字符串
* @param filename
* @return
* @throws Exception
*/
public String readFile(String filename) throws Exception {
StringBuffer buffer = new StringBuffer("");
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(filename));
buffer = new StringBuffer();
while (br.ready())
buffer.append((char) br.read());
} catch (Exception e) {
e.printStackTrace();
} finally {
if(br!=null) br.close();
}
return buffer.toString();
}
public static void main(String[] args) throws Exception {
new HtmlToDoc().writeWordFile("C:/preview4510.html");
}
}
相关文章推荐
- java使用poi将html导出word,默认打开页面视图
- Java 将html变成word导出
- java将html导出到word
- Java 将html导出word格式
- java将html导出成word
- java docx4j导出HTML为word(.docx)报错:org.docx4j.org.xhtmlrenderer.load INFO:: SAX XMLReader in use (parse
- java html导出word默认打开视图设置
- java将html导出成word(利用的poi包导出)
- java将html导出成word(利用的poi包导出)
- java后台html导出为word文件
- java 调用jacob和java2word导出word,并实现将带有格式的html文本和图片插入word
- java word 导出(FreeMarker,HTML模板,包含图片)
- java使用poi将html导出word,默认打开页面视图
- Java 实现HTML富文本导出至word完美解决方案
- web项目java后台将html表格导出为word表格文件
- java使用freemarker完成试卷的Word导出
- Java在线预览(word转html)--强势推荐
- 用java将Word文档转Html
- java导出word直接下载
- JAVA:借用OpenOffice将上传的Word文档转换成Html格式