Apache POI将HTML转换成Word
结果图如下:
package com.poi.word; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.apache.poi.poifs.filesystem.DirectoryEntry; import org.apache.poi.poifs.filesystem.POIFSFileSystem; /** * HTML 转换 Word * @author LGF 2015-01-04 * */ public class HTML2Word { public static void main(String[] args) throws Exception { //创建 POIFSFileSystem 对象 POIFSFileSystem poifs = new POIFSFileSystem(); //获取DirectoryEntry DirectoryEntry directory = poifs.getRoot(); //创建输出流 OutputStream out = new FileOutputStream("src/html_to_word.doc"); try { //创建文档,1.格式,2.HTML文件输入流 directory.createDocument("WordDocument", getInputStream("word.html")); //写入 poifs.writeFilesystem(out); //释放资源 out.close(); System.out.println("success"); } catch (IOException e) { e.printStackTrace(); } } /** * 获取 class path 中的文件流 * @param name 名称 * @return InputStream */ public static InputStream getInputStream(String name){ return Thread.currentThread().getContextClassLoader().getResourceAsStream(name); } }
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>HTML to PDF</title> <style type="text/css"> h1 { color:#ccc; } table tr td{ text-align:center; border:1px solid red; padding:4px; color:red; } table tr th{ background-color:#84C7FD; color:#fff; width: 100px; } .itext{ color:#84C7FD; font-weight:bold; } .description{ color:gray; } </style> </head> <body> <h1>HTML to Word</h1> <p> <span class="poi">poi</span> <span class="description">converting HTML to Word</span> </p> <table> <tr> <th class="label">Title</th> <td>iText - Java HTML to PDF</td> <td>iText - Java HTML to PDF</td> <td>iText - Java HTML to PDF</td> </tr> <tr> <th>URL</th> <td>www.apache.org</td> <td>www.apache.org</td> <td>www.apache.org</td> </tr> </table> <img src="http://dl2.iteye.com/upload/attachment/0083/2873/9d920e8c-8f09-395a-986a-e7a49911676c.png"/> </body> </html>