将html内容写入word文档

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
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;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Html2Doc {

    /**
     * 将html内容写入word文档
     * 
     * @param htmlStr
     * @param path
     * @param filename
     * @param examPaperName
     * @return
     * @throws IOException
     */
    public static boolean writeWordFileByHtmlStr(String htmlStr, String path, String filename, String examPaperName)
            throws IOException {

        String head = "<html><div style=\"text-align: center\"><span style=\"font-size: 28px\"><span style=\"font-family: 黑体\">"
                + examPaperName + "<br /> <br /> </span></span></div>";
        String tail = "</html>";
        htmlStr = head + htmlStr + tail;
        ByteArrayInputStream bais = null;
        FileOutputStream ostream = null;
        try {
            if (!"".equals(path)) {
                File fileDir = new File(path);
                if (!fileDir.exists()) {
                    fileDir.mkdirs();
                }
                if (fileDir.exists()) {

                    String fileName = filename;
                    byte b[] = htmlStr.getBytes("GBK");
                    bais = new ByteArrayInputStream(b);
                    POIFSFileSystem poifs = new POIFSFileSystem();
                    DirectoryEntry directory = poifs.getRoot();
                    DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
                    ostream = new FileOutputStream(path + fileName);
                    poifs.writeFilesystem(ostream);
                    bais.close();
                    ostream.close();
                    poifs.close();
                    documentEntry.delete();
                }
            }
        } catch (IOException e) {
            bais.close();
            ostream.close();
            e.printStackTrace();
            throw e;
        }
        return true;
    }

    public static boolean writeWordFile(String content, String filename, String path, String examPaperName)
            throws IOException {

        String head = "<html><div style=\"text-align: center\"><span style=\"font-size: 28px\"><span style=\"font-family: 黑体\">"
                + examPaperName + "<br /> <br /> </span></span></div>";
        String tail = "</html>";
        content = head + content + tail;
        content = exampaper_formater(content);
        ByteArrayInputStream bais = null;
        FileOutputStream ostream = null;
        try {
            if (!"".equals(path)) {
                File fileDir = new File(path);
                if (!fileDir.exists())
                    fileDir.mkdirs();
                if (fileDir.exists()) {

                    String fileName = filename;
                    byte b[] = content.getBytes("GBK");
                    bais = new ByteArrayInputStream(b);
                    POIFSFileSystem poifs = new POIFSFileSystem();
                    DirectoryEntry directory = poifs.getRoot();
                    // DocumentEntry documentEntry =
                    directory.createDocument("WordDocument", bais);
                    ostream = new FileOutputStream(path + fileName);
                    poifs.writeFilesystem(ostream);
                    bais.close();
                    ostream.close();
                    poifs.close();
                }
            }
        } catch (IOException e) {
            bais.close();
            ostream.close();
            e.printStackTrace();
            throw e;
        }
        return true;
    }

    private static String exampaper_formater(String content) {
        Document doc = Jsoup.parse(content, "UTF-8");

        Elements lis = doc.select("li");
        Elements textareas = doc.select("textarea");
        Elements inputs = doc.select("input");

        for (Element e : lis) {
            e.after("<br/>");
        }

        for (Element e : inputs) {
            String tmp_text = e.text();
            e.after("<p>" + tmp_text + "</p>");
            e.remove();
        }

        for (Element e : textareas) {
            e.after("<br /><br /><br /><br /><br />");
            e.remove();
        }

        return doc.toString();
    }
    public static void main(String[] args) {
    String content = "1、两头渐变透明:"
                + "<HR style='FILTER: alpha(opacity=100,finishopacity=0,style=3)' width='80%' color=#987cb9 SIZE=3>"
                + "2、纺锤形:"
                + "<HR style='FILTER: alpha(opacity=100,finishopacity=0,style=2)' width='80%' color=#987cb9 SIZE=10>"
                + "3、右边渐变透明:"
                + "<HR style='FILTER: alpha(opacity=100,finishopacity=0,style=1)' width='80%' color=#987cb9 SIZE=3>"
                + "4、左边渐变透明:"
                + "<HR style='FILTER: alpha(opacity=0,finishopacity=100,style=1)' width='80%' color=#987cb9 SIZE=3>"
                + "5、虚线:<HR style='border:1 dashed #987cb9' width='80%' color=#987cb9 SIZE=1>"
                + "6、双线:<HR style=\"border:3 double #987cb9\" width=\"80%\" color=#987cb9 SIZE=3>"
                + "7、立体效果:<HR style=\"FILTER: progid:DXImageTransform.Microsoft.Shadow(color:#987cb9,direction:145,strength:15)\" width=\"80%\" color=#987cb9 SIZE=1>"
                + "8、钢针效果:<HR style=\"FILTER: progid:DXImageTransform.Microsoft.Glow(color=#987cb9,strength=10)\" width=\"80%\" color=#987cb9 SIZE=1>"
                + "9、长线效果:<HR align=center width=300 color=#987cb9 SIZE=1>";
        String filename = "Paper.doc";
        String path = "d:\\";
        String examPaperName = "模拟试卷";
        try {
            writeWordFile(content, filename, path, examPaperName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值