資源簡介
html轉Word與PDFjar包與代碼,轉Word使用poi,PDF使用pd4ml,包含demo,包含字體fonts的jar包
代碼片段和文件信息
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?=?“lns:v=“+“‘urn:schemas-microsoft-com:vml‘“+?
???????????????????????????????? ???“xmlns:o=“+“‘urn:schemas-microsoft-com:office:office‘“+?
???????????????????????????????? ???“xmlns:w=“+“‘urn:schemas-microsoft-com:office:word‘“+?
???????????????????????????????? ???“xmlns:m=“+“‘http://schemas.microsoft.com/office/2004/12/omml‘“+?
???????????????????????????????? ???“xmlns=“+“‘http://www.w3.org/TR/REC-html40‘>“+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.re
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????3826??2014-08-08?10:09??html轉Word與PDFjar包與代碼\代碼\Test.java
?????文件???????3492??2014-08-07?12:36??html轉Word與PDFjar包與代碼\代碼\HtmlToDoc.java
?????文件?????335536??2014-08-08?09:46??html轉Word與PDFjar包與代碼\轉wordjar包\pd4ml.jar
?????文件?????123328??2014-08-08?09:46??html轉Word與PDFjar包與代碼\轉wordjar包\ss_css2.jar
?????文件???26762300??2014-08-08?09:46??html轉Word與PDFjar包與代碼\轉wordjar包\fonts.jar
?????文件????1675036??2014-08-08?10:06??html轉Word與PDFjar包與代碼\轉PDFjar包\poi-3.7-20101029.jar
?????目錄??????????0??2014-08-08?10:08??html轉Word與PDFjar包與代碼\代碼
?????目錄??????????0??2014-08-08?12:47??html轉Word與PDFjar包與代碼\轉wordjar包
?????目錄??????????0??2014-08-08?12:48??html轉Word與PDFjar包與代碼\轉PDFjar包
?????目錄??????????0??2014-08-08?09:53??html轉Word與PDFjar包與代碼
-----------?---------??----------?-----??----
?????????????28903518????????????????????10
評論
共有 條評論