java实现富文本转word并下载,部分功能

maven

xml 复制代码
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
		<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>

        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.13.1</version>
        </dependency>

代码

工具类

java 复制代码
import com.example.contant.FontSizeConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xwpf.usermodel.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Slf4j
public class RichTextParser {

    private static final List<String> tagList = Arrays.asList("p", "h1", "h2", "h3", "h4", "h5", "h6", "ol", "ul");

    private static final List<String> specialTagList = Arrays.asList("strong", "em", "u", "s", "blockquote", "pre",
            "ol", "li", "ul", "sub", "sup");

    private static final List<String> listTag = Arrays.asList("ol", "ul");

    public static void parseToDocx(String inputHtml, HttpServletResponse response) throws IOException {

        log.info("转换前:{}",inputHtml);
        inputHtml = inputHtml.replaceAll("\"", "'");
        log.info("转换后:{}",inputHtml);

        response.setContentType("application/msword");
        response.setHeader("Content-Disposition", "attachment; filename=result.docx");

        XWPFDocument document = new XWPFDocument();
        Document doc = Jsoup.parse(inputHtml);
        Elements paragraphs = getAll(new Elements(), doc);
        for (Element paragraph : paragraphs) {
            XWPFParagraph xwpfParagraph = document.createParagraph();

            if (listTag.contains(paragraph.tagName())) {
                createListTag(document, paragraph);
            } else {
                List<Element> list = separateRendering(paragraph);
                list.forEach(ele -> {
                    String text = ele.text();
                    XWPFRun run = xwpfParagraph.createRun();
                    run.setText(text);
                    Element tmp = ele;
                    if (StringUtils.equals(tmp.tagName(), "p") && tmp.children().size() != 0) {
                        tmp = tmp.child(0);
                    }
                    String style = tmp.attr("style");
                    if (style != null && !style.isEmpty()) {
                        String[] styleAttrs = style.split(";");
                        processCss(styleAttrs, run);
                    }

                    processFontsize(ele, run);
                    processSpecialLabels(ele, run);
                });
            }
        }
        OutputStream outputStream = response.getOutputStream();
        document.write(outputStream);
        outputStream.close();
    }

    private static void createListTag(XWPFDocument document, Element paragraph) {
        XWPFParagraph orderedListParagraph = document.createParagraph();
        XWPFRun orderedListRun = orderedListParagraph.createRun();
        XWPFNumbering numbering = null;
        numbering = document.createNumbering();

        Elements children = paragraph.children();
        for (Element element : children) {
            XWPFParagraph orderedListItemParagraph1 = document.createParagraph();
            XWPFRun orderedListItemRun1 = orderedListItemParagraph1.createRun();
            orderedListItemRun1.setText(element.text());
            orderedListItemParagraph1.setNumID(numbering.addNum(BigInteger.ONE));
//            orderedListItemParagraph1.setNumID(
//                    numbering.addNum(
//                            StringUtils.equals(paragraph.tagName(), "ul") ? BigInteger.ONE : BigInteger.valueOf(2)
//                    )
//            );
        }

    }

    private static List<Element> separateRendering(Element paragraph) {
        Elements children = paragraph.children();
        return new ArrayList<Element>() {{
            if (children.size() == 0) {
                add(paragraph);
            } else {
                addAll(children);
            }
        }};
    }

    private static Elements getAll(Elements elements, Document doc) {
        for (Element child : doc.children().get(0).child(1).children()) {
            if (tagList.contains(child.tagName())) {
                elements.add(child);
            }
        }


        return elements;
    }

    private static void processCss(String[] styleAttrs, XWPFRun run) {
        for (String styleAttr : styleAttrs) {
            String[] attrParts = styleAttr.split(":");
            if (attrParts.length == 2) {
                String attrName = attrParts[0].trim();
                String attrValue = attrParts[1].trim();
                switch (attrName) {
                    case "background-color":
                        run.setColor(getHexColor(attrValue));
                        break;
                    case "color":
                        run.setColor(getHexColor(attrValue));
                        break;
                }
            }
        }
    }

    private static String getHexColor(String rgbString) {
        String[] rgbValues = rgbString.replaceAll("[^\\d,]", "").split(",");

        int red = Integer.parseInt(rgbValues[0].trim());
        int green = Integer.parseInt(rgbValues[1].trim());
        int blue = Integer.parseInt(rgbValues[2].trim());

        String hexRed = Integer.toHexString(red);
        String hexGreen = Integer.toHexString(green);
        String hexBlue = Integer.toHexString(blue);

        return padZero(hexRed) + padZero(hexGreen) + padZero(hexBlue);
    }

    private static String padZero(String hexValue) {
        return hexValue.length() == 1 ? "0" + hexValue : hexValue;
    }

    private static void processSpecialLabels(Element paragraph, XWPFRun run) {
        Elements children = paragraph.children();
        if (children.size() == 0) {
            doRenderingSpecialLabels(paragraph, run);
            return;
        }

        for (Element e : children) {
            doRenderingSpecialLabels(e, run);
            processSpecialLabels(e, run);
        }
    }

    private static void processFontsize(Element paragraph, XWPFRun run) {

        switch (paragraph.tagName()) {
            case "h1":
                run.setFontSize(FontSizeConstant.H1_SIZE);
                break;
            case "h2":
                run.setFontSize(FontSizeConstant.H2_SIZE);
                break;
            case "h3":
                run.setFontSize(FontSizeConstant.H3_SIZE);
                break;
            case "h4":
                run.setFontSize(FontSizeConstant.H4_SIZE);
                break;
            case "h5":
                run.setFontSize(FontSizeConstant.H5_SIZE);
                break;
            case "h6":
                run.setFontSize(FontSizeConstant.H6_SIZE);
                break;
        }
    }

    private static void doRenderingSpecialLabels(Element tag, XWPFRun run) {

        if (specialTagList.contains(tag.tagName())) {
            switch (tag.tagName()) {
                case "strong":
                    run.setBold(true);
                    break;
                case "em":
                    run.setItalic(true);
                    break;
                case "u":
                    run.setUnderline(UnderlinePatterns.SINGLE);
                    break;
                case "s":
                    run.setStrikeThrough(true);
                    break;
                case "blockquote":
                    run.setImprinted(true);
                    break;
                case "pre":

                    break;
                case "sub":
                    doGenerateSub();
                    break;
                case "sup":
                    doGenerateSup();
                    break;
            }
        }
    }

    private static void doGenerateSub() {
        // todo 底数
    }

    private static void doGenerateSup() {
        // todo 指数
    }

常量类

java 复制代码
public interface FontSizeConstant {

    int H1_SIZE = 14;
    int H2_SIZE = 12;
    int H3_SIZE = 11;
    int H4_SIZE = 10;
    int H5_SIZE = 9;
    int H6_SIZE = 8;
}
相关推荐
.生产的驴12 分钟前
SpringBoot 对接第三方登录 手机号登录 手机号验证 微信小程序登录 结合Redis SaToken
java·spring boot·redis·后端·缓存·微信小程序·maven
爱上语文14 分钟前
宠物管理系统:Dao层
java·开发语言·宠物
王ASC1 小时前
SpringMVC的URL组成,以及URI中对/斜杠的处理,解决IllegalStateException: Ambiguous mapping
java·mvc·springboot·web
是小崔啊1 小时前
开源轮子 - Apache Common
java·开源·apache
因我你好久不见1 小时前
springboot java ffmpeg 视频压缩、提取视频帧图片、获取视频分辨率
java·spring boot·ffmpeg
程序员shen1616111 小时前
抖音短视频saas矩阵源码系统开发所需掌握的技术
java·前端·数据库·python·算法
Ling_suu1 小时前
SpringBoot3——Web开发
java·服务器·前端
天使day2 小时前
SpringMVC
java·spring·java-ee
CodeClimb2 小时前
【华为OD-E卷-简单的自动曝光 100分(python、java、c++、js、c)】
java·python·华为od