富文本转word

富文本转word保存

重要的事放前面
引入poi时出现了很多包版本不兼容的事故
以下是我的一套pom配置

<!--word操作工具类-->
        <dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <version>1.6.0-beta1</version>
            <exclusions>
                <exclusion>
                    <artifactId>poi-ooxml-schemas</artifactId>
                    <groupId>org.apache.poi</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>4.5.11</version>
        </dependency>
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.11.3</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.2.3</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.2.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>5.2.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-full</artifactId>
            <version>5.2.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.2.3</version>
        </dependency>

注:我的可能不是最好的,但是凑活着可以正常运行
poi版本已经出到1.12了这里为什么要用1.6呢!
因为如果自己写富文本标签转换很费时费劲
在网上找到了一个poi版本1.6的富文本转word的开源代码
代码地址:https://gitee.com/xuwangcheng/html-to-word.git
所以咱们就站巨人的肩膀上了,为这个位作者点赞。

由于poi的版本过低,若依原生态的excel转换工具中有很多jar包是跟poi冲突的!
所以上面的pom文件有一些exclusion排除一些冲突jar包不引入!

在开发中注意doc.close会报错,writeToFile方法也不可以使用了,需要自己调用最原始字符流写入文件的方法。

主要实现:
1.H(1,2,3,4,5,6,7)标题标签识别
2.br 阴影标签识别
3.s|del|strike 删除划线标签识别
4.i 网址标签识别
5.li 队列标签识别
6.p 字段标签识别
7.l|ul 队列标签识别
8.span 字段标签识别
9.b|strong 粗体标签识别
10.sup 样式标签识别
11.table 别个标签识别
12.th 表头单元格标签识别
13.u 下划线标签识别
14.img 图片标签识别

  • img可以识别到png,jpg,jpeg后缀
  • 图片可以自定义大小
  • 图片来源可以是二进制和网络图片链接

代码如下:

/**
     * 新增会议纪要信息
     */
//    @PreAuthorize("@ss.hasPermi('system:mettingRecord:add')")
    @Log(title = "会议纪要信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YyMettingRecord yyMettingRecord) throws IOException {

        //生成docx文件
        Configure config = Configure.newBuilder().build();
        AbstractRenderPolicy htmlRenderPolicy = null;
        // 使用自己写的策略处理
        htmlRenderPolicy = HtmlToWordUtil.createHtmlRenderPolicy(null);
        config.customPolicy("mettingContent", htmlRenderPolicy);
        //表格配置
        config.customPolicy("detail_table", new DetailTablePolicy());

        headTextStyle.setFontFamily("Hei");
        headTextStyle.setFontSize(9);
        headTextStyle.setColor("7F7F7F");

        headStyle.setBackgroundColor("F2F2F2");
        headStyle.setAlign(STJc.CENTER);

        rowStyle = new TableStyle();
        rowStyle.setAlign(STJc.CENTER);

        meetingEndStyle.setColor("00ff7f");
        meetingEndStyle.setFontSize(12);

        DetailData detailTable = new DetailData();
        List<RowRenderData> mettingEnds = new ArrayList<>();

        Map<String, Object> map = new HashMap<String, Object>();

        ArrayList<RowRenderData> rowRenderDatas = new ArrayList<>();
        map.put("mettingName",yyMettingRecord.getMettingName());
        map.put("mettingSummary",yyMettingRecord.getMettingSummary());
        map.put("mettingType",yyMettingRecord.getMettingType());
        map.put("mettingTheme",yyMettingRecord.getMettingTheme());
        map.put("mettingContent",yyMettingRecord.getMettingContent());
        map.put("detail_table",detailTable);
        InputStream resourceAsStream = YyMettingRecordController.class.getClassLoader().getResourceAsStream("docx/mettingTemplate.docx");
        XWPFTemplate template = XWPFTemplate.compile(resourceAsStream, config);
        template.render(map);
        String exportFilePath = profile + "/file/upload/" + System.currentTimeMillis()+ "会议纪要" + ".docx";
        yyMettingRecord.setFilePath(exportFilePath);
        FileOutputStream out = new FileOutputStream(exportFilePath);
        template.write(out);
        out.flush();
        out.close();

        //文件上传到minion中
        String url = minioUrl + "/file/upload";
        File file = new File(exportFilePath);
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        // 创建一个多部分实体构建器
        //文件名称中文乱码问题在create()后面加上:.setMode(HttpMultipartMode.RFC6532);
        MultipartEntityBuilder builder = MultipartEntityBuilder.create().setMode(HttpMultipartMode.RFC6532);
        builder.addBinaryBody("file", file);
        // 将构建的实体设置为HttpPost请求的实体
        HttpEntity multipartEntity = builder.build();
        httpPost.setEntity(multipartEntity);
        // 执行HTTP请求
        HttpResponse responsePost = httpClient.execute(httpPost);
        // 处理响应
        String responseBody = EntityUtils.toString(responsePost.getEntity());
        JSONObject jsonFileUrl =  JSON.parseObject(responseBody);
        JSONArray jsonFileUrls = new JSONArray();
        jsonFileUrls.add(jsonFileUrl);
        yyMettingRecord.setFileUrl(jsonFileUrls);
        file.delete();//删除临时文件
        yyMettingRecord.setSeeUsers(new String(seeUsers));
        return toAjax(yyMettingRecordService.insertYyMettingRecord(yyMettingRecord));
    }

上面代码有些改动,还请见解!
最后生成word临时文件然后再存进minio,其实应该也可以直接二进制存进去!这个没有细看。

具体工具类如下:https://gitee.com/crcrwork/html2word.git

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值