Itext实现将Html代码转换为Doc

版权声明:本文为博主原创文章,转载请标明出处: https://blog.csdn.net/u012343297/article/details/79107702

最近一个项目中需要将页面的一些字段生成doc文档,而页面中部分字段是使用kindeeditor编辑器编辑的,存到数据库中是html代码,如:

<p class="20" style="text-indent:28.0000pt;">
    公司成立了项目部负责本项目的设计工作。项目部主要人员组成如下。
</p>

就需要在导出doc的同时,把html转换成word中可以正常显示的内容。场景就是这样,废话就不多说了,直接上代码。

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 转换 Word 
 * @author Lsd 2018-01-19 
 * 
 */  
public class HTML2Word {  
    public static void main(String[] args) throws Exception { 

        String filepath = "C:/test/";
        //创建 POIFSFileSystem 对象  
        POIFSFileSystem poifs = new POIFSFileSystem();    
        //获取DirectoryEntry  
        DirectoryEntry directory = poifs.getRoot();    
        //创建输出流  
        OutputStream out = new FileOutputStream(filepath+"2003.doc");  
        try {  
            //创建文档,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 = "<table><tr><td colspan='2'>1222</td></tr><tr><td>First</td> <td>Row</td></tr>"
                    + "<tr><td>Second你好</td><td>Row</td></tr></table>"; 
            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);  
    }  
    /**
     * 将html直接追加到doc文件中
     * @param htmlContent
     * @throws IOException
     * @throws DocumentException
     */
    public static void insertHtml2Doc(String htmlContent) throws IOException, DocumentException{
//      设置中文字体  

        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 = "<img src='http://localhost:8081/project/Image/a.jpg'/>";   
        StyleSheet ss = new StyleSheet();  
        ss.loadTagStyle("table", "border", "1000");
        List htmlList = com.lowagie.text.html.simpleparser.HTMLWorker.parseToList(new StringReader(htmlContent), ss);  
        for (int i = 0 ; i < htmlList.size(); i++)
        {  
            com.lowagie.text.Element e = (com.lowagie.text.Element) htmlList.get(i);  

            context.add(e);  
        }  
        Font font = new Font(Font.NORMAL, 16, Font.NORMAL, new Color(0, 0, 0));
        context.setFont(font);

        document.add(context);
        document.close();  
        System.out.println("ok");  
    }
    /**
     * 将html代码table的单元格中,对于表格加入单元格中样式有问题,待解决
     * @param htmlContent
     * @return
     * @throws IOException
     * @throws DocumentException
     */
    public  Paragraph insertHtml2Cell(String htmlContent) throws IOException, DocumentException{

        Paragraph context = new Paragraph();  
        StyleSheet ss = new StyleSheet();  

        List htmlList = com.lowagie.text.html.simpleparser.HTMLWorker.parseToList(new StringReader(htmlContent), ss);  
        for (int i = 0 ; i < htmlList.size(); i++)
        {  
            com.lowagie.text.Element e = (com.lowagie.text.Element) htmlList.get(i);  
            context.add(e);  
        }  

         return context;
    }

}  

Maven依赖配置

<dependency>
        <groupId>fr.opensagres.xdocreport</groupId>
        <artifactId>fr.opensagres.xdocreport.document</artifactId>
        <version>1.0.5</version>
    </dependency>
    <dependency>  
        <groupId>fr.opensagres.xdocreport</groupId>  
        <artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>  
        <version>1.0.5</version>  
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.12</version>
    </dependency>
    <dependency>
    <groupId>com.lowagie</groupId>
    <artifactId>itext</artifactId>
    <version>2.1.7</version>
    </dependency>
    <dependency>
    <groupId>com.lowagie</groupId>
    <artifactId>itext-rtf</artifactId>
    <version>2.1.7</version>
</dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-scratchpad</artifactId>
        <version>3.12</version>
    </dependency>

以上是实现导出导出的demo,具体项目中,将insertHtml2Doc和insertHtml2Cell方法修改后放到相关的Util中即可。

源码:http://download.csdn.net/download/u012343297/10214329

展开阅读全文

没有更多推荐了,返回首页