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


    maven

    		<dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
            
    		<dependency>
                <groupId>org.apache.poigroupId>
                <artifactId>poiartifactId>
                <version>4.1.2version>
            dependency>
    
            <dependency>
                <groupId>org.apache.poigroupId>
                <artifactId>poi-ooxmlartifactId>
                <version>4.1.2version>
            dependency>
    
            <dependency>
                <groupId>org.jsoupgroupId>
                <artifactId>jsoupartifactId>
                <version>1.13.1version>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    代码
    工具类

    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 指数
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230

    常量类

    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;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  • 相关阅读:
    jsoncpp库的使用及用httplib库搭建HTTP服务器
    Android FFmpeg系列——C多线程使用
    JavaScript中的实例化研究
    LeetCode 每日一题 2023/10/9-2023/10/15
    OpenSSL 生成 RootCA (根证书)并自签署证书(支持 IP 地址)
    [C++黑马程序员笔记]P113-P117类和对象-对象特性(2)
    kubernetes部署jenkins
    微软首款AI芯片代号“雅典娜”;马斯克四年内将让“星舰”上火星丨 RTE 开发者日报 Vol.61
    什么是验厂&什么是认证
    怎样恢复误删和损坏磁盘上的文件
  • 原文地址:https://blog.csdn.net/weixin_44808225/article/details/134028061