資源簡介
Itext實現(xiàn)Html轉(zhuǎn)化為Doc源碼、Doc中部分代碼為html轉(zhuǎn)化

代碼片段和文件信息
package?com.spb.SpringBootDemo.rest;
import?java.awt.Color;
import?java.io.FileInputStream;
import?java.io.FileNotFoundException;
import?java.io.FileOutputStream;??
import?java.io.IOException;??
import?java.io.InputStream;??
import?java.io.OutputStream;??
import?java.io.StringReader;
import?java.util.List;
import?org.apache.poi.poifs.filesystem.DirectoryEntry;??
import?org.apache.poi.poifs.filesystem.POIFSFileSystem;??
import?com.lowagie.text.Cell;
import?com.lowagie.text.Document;
import?com.lowagie.text.DocumentException;
import?com.lowagie.text.Font;
import?com.lowagie.text.PageSize;
import?com.lowagie.text.Paragraph;
import?com.lowagie.text.Table;
import?com.lowagie.text.html.simpleparser.HTMLWorker;
import?com.lowagie.text.html.simpleparser.styleSheet;
import?com.lowagie.text.pdf.baseFont;
import?com.lowagie.text.rtf.RtfWriter2;
??
/**?
?*?HTML?轉(zhuǎn)換?Word?
?*?@author?Lsd?2018-01-19?
?*?
?*/??
public?class?HTML2Word?{??
????public?static?void?main(String[]?args)?throws?Exception?{?
???? String?filepath?=?“C:/test/“;
????????//創(chuàng)建?POIFSFileSystem?對象??
????????POIFSFileSystem?poifs?=?new?POIFSFileSystem();????
????????//獲取DirectoryEntry??
????????DirectoryEntry?directory?=?poifs.getRoot();????
????????//創(chuàng)建輸出流??
????????OutputStream?out?=?new?FileOutputStream(filepath+“2003.doc“);??
????????try?{??
????????????//創(chuàng)建文檔1.格式2.HTML文件輸入流??
???????? InputStream?is?=?new?FileInputStream(filepath+“2003.html“);
????????????directory.createDocument(“WordDocument“is?);??
????????????//寫入??
????????????poifs.writeFilesystem(out);??
????????????//釋放資源??
????????????out.close();??
????????????System.out.println(“success“);??
????????????String?htmlContent?=?“1222 First ?Row “
???????????? +?“Second你好 Row
“;?
????????????insertHtml2Doc(htmlContent);
????????}?catch?(IOException?e)?{??
????????????e.printStackTrace();??
????????}????
????}??
??????
????/**?
?????*?獲取?class?path?中的文件流?
?????*?@param?name?名稱?
?????*?@return?InputStream??
?????*/??
????public?static?InputStream?getInputStream(String?name){??
????????return?Thread.currentThread().getContextClassLoader().getResourceAsStream(name);??
????}??
????
????public?static?void?insertHtml2Doc(String?htmlContent)?throws?IOException?DocumentException{
//??????設(shè)置中文字體??
????
???? OutputStream?out?=?new?FileOutputStream(“c:/test/a.doc“);??
????????Document?document?=?new?Document(PageSize.A4);??
????????RtfWriter2.getInstance(document?out);??
????????document.open();??
???? Paragraph?context?=?new?Paragraph();??
????????//String?htmlContent?=?“
“;???
????????styleSheet?ss?=?new?styleSheet();??
????????ss.loadTagstyle(“table“?“border“?“1000“);
????????ss.loadTagstyle(“tr“?“width“?“10“);
????????List?htmlList?=?com.lowagie.text.html.simpleparser.HTMLWorker.parseToList(new?StringRead
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????3072??2018-01-19?15:15??test\2003.doc
?????文件????????604??2018-01-19?08:58??test\2003.html
?????文件??????????0??2018-01-19?08:42??test\2007.docx
?????文件??????65788??2018-01-19?15:23??test\2007.html
?????文件????????314??2018-01-19?11:26??test\a.files\colorschememapping.xm
?????文件????????233??2018-01-19?11:26??test\a.files\filelist.xm
?????文件???????3432??2018-01-19?11:26??test\a.files\header.htm
?????文件???????3116??2018-01-19?11:26??test\a.files\themedata.thmx
?????文件???????4277??2018-01-19?15:44??test\HTML2Word.java
????..A..H.???????162??2018-01-19?15:15??test\~$a.doc
?????目錄??????????0??2018-01-19?11:26??test\a.files
?????目錄??????????0??2018-01-19?08:39??test\image
?????目錄??????????0??2018-01-19?15:50??test
?????文件??????31267??2018-01-19?15:50??test\a.doc
-----------?---------??----------?-----??----
???????????????112265????????????????????14
評論
共有 條評論