读取word目录标题及分段内容并强条显示

原版word数据
在这里插入图片描述

做完大概就是这样的
demo

逻辑其实比较简单,就是网上没有例子需要一点一点的去尝试,因为目前做的项目只有二级,如果需要多级可以自己修改一下

<!--word读取-->
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
 <dependency>
     <groupId>org.apache.poi</groupId>
     <artifactId>poi-ooxml</artifactId>
     <version>5.0.0</version>
 </dependency>
 <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad -->
 <dependency>
     <groupId>org.apache.poi</groupId>
     <artifactId>poi-scratchpad</artifactId>
     <version>5.0.0</version>
 </dependency>

实体用的mybatis-plus+lombok生成的,看懂扣1,看不懂扣眼珠子
里面的标准是另一张表,也就是word相关联的一个主表,就不贴了,这两个都是需要建表的,自行修改


代码

分类实体

package com.lzy.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

@Data
@TableName(value = "hc_technical_criterion_word_class")
public class HcTechnicalCriterionWordClass {
    /**
     * 主键
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    /**
     * uuid
     */
    @TableField(value = "uuid",fill = FieldFill.INSERT)
    private String uuid;

    /**
     * 上级id
     */
    @TableField(value = "super_id")
    private Integer superId;

    /**
     * 标准uuid
     */
    @TableField(value = "criterion_uuid")
    private String criterionUuid;

    /**
     * 分类名称
     */
    @TableField(value = "name")
    private String name;

    /**
     * 创建人uuid
     */
    @TableField(value = "create_user",fill = FieldFill.INSERT)
    private String createUser;

    /**
     * 创建时间
     */
    @TableField(value = "create_time",fill = FieldFill.INSERT)
    @JsonFormat(
            pattern = "yyyy-MM-dd HH:mm:ss",
            timezone = "GMT+8"
    )
    @DateTimeFormat(
            pattern="yyyy-MM-dd HH:mm:ss"
    )
    private Date createTime;

    /**
     * 修改人uuid
     */
    @TableField(value = "update_user",fill = FieldFill.UPDATE)
    private String updateUser;

    /**
     * 修改时间
     */
    @TableField(value = "update_time",fill = FieldFill.UPDATE)
    @JsonFormat(
            pattern = "yyyy-MM-dd HH:mm:ss",
            timezone = "GMT+8"
    )
    @DateTimeFormat(
            pattern="yyyy-MM-dd HH:mm:ss"
    )
    private Date updateTime;

    /**
     * 删除人uuid
     */
    @TableField(value = "delete_user")
    private String deleteUser;

    /**
     * 删除时间
     */
    @TableField(value = "delete_time")
    @JsonFormat(
            pattern = "yyyy-MM-dd HH:mm:ss",
            timezone = "GMT+8"
    )
    @DateTimeFormat(
            pattern="yyyy-MM-dd HH:mm:ss"
    )
    private Date deleteTime;

    /**
     * 0.未删除 1.已删除(默认0)
     */
    @TableField(value = "is_delete")
    @TableLogic
    private Integer isDelete;

    public static final String COL_ID = "id";

    public static final String COL_UUID = "uuid";

    public static final String COL_CRITERION_UUID = "criterion_uuid";

    public static final String COL_NAME = "name";

    public static final String COL_CREATE_USER = "create_user";

    public static final String COL_CREATE_TIME = "create_time";

    public static final String COL_UPDATE_USER = "update_user";

    public static final String COL_UPDATE_TIME = "update_time";

    public static final String COL_DELETE_USER = "delete_user";

    public static final String COL_DELETE_TIME = "delete_time";

    public static final String COL_IS_DELETE = "is_delete";
}

内容实体

package com.lzy.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

@Data
@TableName(value = "hc_technical_criterion_word_content")
public class HcTechnicalCriterionWordContent {
    /**
     * 主键
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    /**
     * uuid
     */
    @TableField(value = "uuid",fill = FieldFill.INSERT)
    private String uuid;

    /**
     * 技术标准uuid
     */
    @TableField(value = "criterion_uuid")
    private String criterionUuid;

    /**
     * 目录uuid
     */
    @TableField(value = "class_uuid")
    private String classUuid;

    /**
     * 内容
     */
    @TableField(value = "content")
    private String content;

    /**
     * 是否强条:0否 1是
     */
    @TableField(value = "is_bold")
    private Integer isBold;

    /**
     * 强条内容
     */
    @TableField(value = "bold_content")
    private String boldContent;

    /**
     * 创建人uuid
     */
    @TableField(value = "create_user",fill = FieldFill.INSERT)
    private String createUser;

    /**
     * 创建时间
     */
    @TableField(value = "create_time",fill = FieldFill.INSERT)
    @JsonFormat(
            pattern = "yyyy-MM-dd HH:mm:ss",
            timezone = "GMT+8"
    )
    @DateTimeFormat(
            pattern="yyyy-MM-dd HH:mm:ss"
    )
    private Date createTime;

    /**
     * 修改人uuid
     */
    @TableField(value = "update_user",fill = FieldFill.UPDATE)
    private String updateUser;

    /**
     * 修改时间
     */
    @TableField(value = "update_time",fill = FieldFill.UPDATE)
    @JsonFormat(
            pattern = "yyyy-MM-dd HH:mm:ss",
            timezone = "GMT+8"
    )
    @DateTimeFormat(
            pattern="yyyy-MM-dd HH:mm:ss"
    )
    private Date updateTime;

    /**
     * 删除人uuid
     */
    @TableField(value = "delete_user")
    private String deleteUser;

    /**
     * 删除时间
     */
    @TableField(value = "delete_time")
    @JsonFormat(
            pattern = "yyyy-MM-dd HH:mm:ss",
            timezone = "GMT+8"
    )
    @DateTimeFormat(
            pattern="yyyy-MM-dd HH:mm:ss"
    )
    private Date deleteTime;

    /**
     * 0.未删除 1.已删除(默认0)
     */
    @TableField(value = "is_delete")
    @TableLogic
    private Integer isDelete;

    public static final String COL_ID = "id";

    public static final String COL_UUID = "uuid";

    public static final String COL_CRITERION_UUID = "criterion_uuid";

    public static final String COL_CLASS_UUID = "class_uuid";

    public static final String COL_CONTENT = "content";

    public static final String COL_IS_BOLD = "is_bold";

    public static final String COL_BOLD_CONTENT = "bold_content";

    public static final String COL_CREATE_USER = "create_user";

    public static final String COL_CREATE_TIME = "create_time";

    public static final String COL_UPDATE_USER = "update_user";

    public static final String COL_UPDATE_TIME = "update_time";

    public static final String COL_DELETE_USER = "delete_user";

    public static final String COL_DELETE_TIME = "delete_time";

    public static final String COL_IS_DELETE = "is_delete";
}

读取word写入分类和内容

    public boolean writeWord(String uuid) {
        System.err.println("----------读取word----------");
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("criterion_uuid",uuid);
        wordClassService.remove(updateWrapper);
        wordContentService.remove(updateWrapper);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("uuid",uuid);
        HcTechnicalCriterion criterion = this.getOne(queryWrapper);
        String fileName = criterion.getFile().substring(criterion.getFile().lastIndexOf('/')+1);
        HcTechnicalCriterionWordClass wordClass = null;
        HcTechnicalCriterionWordContent wordContent = null;
        String content = "";
        String boldContent = "";
        int isBold = 0;
        int superId = 0;
        try {
            String url = filePath+fileName;
            InputStream is = new FileInputStream(url);
            if(url.endsWith(".DOCX") || url.endsWith(".docx")){
                XWPFDocument document = new XWPFDocument(is);
                List<XWPFParagraph> paragraphList = document.getParagraphs();
                for (XWPFParagraph para : paragraphList) {
                    if("2".equals(para.getStyle())){
                        if(StrUtil.isNotBlank(content)){
                            wordContent = new HcTechnicalCriterionWordContent();
                            wordContent.setClassUuid(wordClass.getUuid());
                            wordContent.setContent(content);
                            wordContent.setCriterionUuid(uuid);
                            if(isBold == 1){
                                wordContent.setIsBold(1);
                                wordContent.setBoldContent(boldContent);
                            }
                            wordContentService.save(wordContent);
                            content = "";
                            boldContent = "";
                            isBold = 0;
                        }
                        wordClass = new HcTechnicalCriterionWordClass();
                        wordClass.setCriterionUuid(uuid);
                        wordClass.setName(para.getText());
                        wordClass.setSuperId(0);
                        wordClassService.save(wordClass);
                        superId = wordClass.getId();
                    }else if("3".equals(para.getStyle())){
                        if(StrUtil.isNotBlank(content)){
                            wordContent = new HcTechnicalCriterionWordContent();
                            wordContent.setClassUuid(wordClass.getUuid());
                            wordContent.setContent(content);
                            wordContent.setCriterionUuid(uuid);
                            if(isBold == 1){
                                wordContent.setIsBold(1);
                                wordContent.setBoldContent(boldContent);
                            }
                            wordContentService.save(wordContent);
                            content = "";
                            boldContent = "";
                            isBold = 0;
                        }
                        wordClass = new HcTechnicalCriterionWordClass();
                        wordClass.setCriterionUuid(uuid);
                        wordClass.setName(para.getText());
                        wordClass.setSuperId(superId);
                        wordClassService.save(wordClass);
                    }else{
                        List<XWPFRun> runList = para.getRuns();
                        XWPFRun run = runList.get(0);
                        if(run.isBold()){
                            content += "<b>"+run.text()+"</b>"+"</br>";
                            boldContent = "<b>"+run.text()+"</b>";
                            isBold = 1;
                        }else{
                            content += run.text()+"</br>";
                        }
                    }
                }
            }else if(url.endsWith(".DOC") || url.endsWith(".doc")){
                POIFSFileSystem fs = new POIFSFileSystem(is);
                HWPFDocument document = new HWPFDocument(fs);
                Range range = document.getRange();
                for (int i = 0; i < range.numParagraphs(); i++) {
                    Paragraph paragraph = range.getParagraph(i);
                    if(paragraph.getStyleIndex() == 1){
                        if(StrUtil.isNotBlank(content)){
                            wordContent = new HcTechnicalCriterionWordContent();
                            wordContent.setClassUuid(wordClass.getUuid());
                            wordContent.setContent(content);
                            wordContent.setCriterionUuid(uuid);
                            if(isBold == 1){
                                wordContent.setIsBold(1);
                                wordContent.setBoldContent(boldContent);
                            }
                            wordContentService.save(wordContent);
                            content = "";
                            boldContent = "";
                            isBold = 0;
                        }
                        wordClass = new HcTechnicalCriterionWordClass();
                        wordClass.setCriterionUuid(uuid);
                        wordClass.setName(paragraph.text());
                        wordClass.setSuperId(0);
                        wordClassService.save(wordClass);
                        superId = wordClass.getId();
                    }else if(paragraph.getStyleIndex() == 2){
                        if(StrUtil.isNotBlank(content)){
                            wordContent = new HcTechnicalCriterionWordContent();
                            wordContent.setClassUuid(wordClass.getUuid());
                            wordContent.setContent(content);
                            wordContent.setCriterionUuid(uuid);
                            if(isBold == 1){
                                wordContent.setIsBold(1);
                                wordContent.setBoldContent(boldContent);
                            }
                            wordContentService.save(wordContent);
                            content = "";
                            boldContent = "";
                            isBold = 0;
                        }
                        wordClass = new HcTechnicalCriterionWordClass();
                        wordClass.setCriterionUuid(uuid);
                        wordClass.setName(paragraph.text());
                        wordClass.setSuperId(superId);
                        wordClassService.save(wordClass);
                    }else{
                        CharacterRun run = paragraph.getCharacterRun(0);
                        if(run.isBold()){
                            content += "<b>"+run.text()+"</b>"+"</br>";
                            boldContent = "<b>"+run.text()+"</b>";
                            isBold = 1;
                        }else{
                            content += run.text()+"</br>";
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

树形目录跟强条显示就不用贴了吧,树形目录很多工具类都可以做出来,有需要的话留言我再贴吧,我自己用的hutool工具包生成的,强条显示就是判断is_bold=1然后拿到bold_content字段就行了


输出样式

树形目录

{
	"code": 200,
	"msg": "成功",
	"data": [
		{
			"id": 104,
			"parentId": 0,
			"name": "1 总则\r",
			"uuid": "1a51c4ea-2200-48de-a998-e8ad9cf49cab"
		},
		{
			"id": 105,
			"parentId": 0,
			"name": "2 术语\r",
			"uuid": "302cb678-5817-4017-86cf-01bf72d22382"
		},
		{
			"id": 106,
			"parentId": 0,
			"name": "3 基本规定\r",
			"uuid": "218beb4d-6300-48eb-a347-5da85a0c71a6",
			"children": [
				{
					"id": 107,
					"parentId": 106,
					"name": "3.1 施工技术管理\r",
					"uuid": "656555c5-4813-402c-82e9-271f6b083e57"
				},
				{
					"id": 108,
					"parentId": 106,
					"name": "3.2 施工质量管理\r",
					"uuid": "cd23f613-7963-42a8-86d5-8007eff4c616"
				},
				{
					"id": 109,
					"parentId": 106,
					"name": "3.3 材料与设备质量管理\r",
					"uuid": "3a55d6f4-95b3-4afd-8956-2df836ba7ec6"
				},
				{
					"id": 110,
					"parentId": 106,
					"name": "3.4 安全与环境保护\r",
					"uuid": "a83d3209-3f10-4a1d-ab6f-439b577c4118"
				}
			]
		},
		{
			"id": 111,
			"parentId": 0,
			"name": "4 金属风管与配件制作\r",
			"uuid": "d77fa82d-da7e-44c5-860a-63a14e9a377b",
			"children": [
				{
					"id": 112,
					"parentId": 111,
					"name": "4.1 一般规定\r",
					"uuid": "cc66eedb-d498-4804-93e1-92c7430695be"
				},
				{
					"id": 113,
					"parentId": 111,
					"name": "4.2 金属风管制作\r",
					"uuid": "b740a448-6c6c-4960-9ffe-aaafcf5b012b"
				},
				{
					"id": 114,
					"parentId": 111,
					"name": "4.3 配件制作\r",
					"uuid": "827e8c14-6cf2-4883-87d0-fbccd6df74db"
				},
				{
					"id": 115,
					"parentId": 111,
					"name": "4.4 质量检查\r",
					"uuid": "cf5b09ef-0cb9-49fb-9f41-e11ab322a6e4"
				}
			]
		},
		{
			"id": 116,
			"parentId": 0,
			"name": "5 非金属与复合风管及配件制作\r",
			"uuid": "1f00aa8f-afc2-44f3-92ae-3b56bc97f94f",
			"children": [
				{
					"id": 117,
					"parentId": 116,
					"name": "5.1 一般规定\r",
					"uuid": "a549fb2b-896e-4687-a9a0-ed7132cad97e"
				},
				{
					"id": 118,
					"parentId": 116,
					"name": "5.2 聚氨酯铝箔与酚醛铝箔复合风管及配件制作\r",
					"uuid": "9e519f73-67f0-4ae6-b9dd-6e9e29f68e3f"
				}
			]
		},
		{
			"id": 119,
			"parentId": 0,
			"name": "本规范用词说明\r",
			"uuid": "1b16009d-39ea-490d-9226-decee7b41cda"
		},
		{
			"id": 120,
			"parentId": 0,
			"name": "引用标准名录\r",
			"uuid": "ed6e20cd-f6b4-4ba7-b744-b0b67b30507c"
		}
	]
}

强条显示

{
	"code": 200,
	"msg": "成功",
	"data": [
		{
			"classUuid": "1a51c4ea-2200-48de-a998-e8ad9cf49cab",
			"className": "1 总则\r",
			"boldContent": "<b>1.0.1为加强通风与空调工程施工安装技术的管理,规范施工工艺,强化施工安装过程控制,确保工程质量,制定本规范。\r</b>"
		},
		{
			"classUuid": "656555c5-4813-402c-82e9-271f6b083e57",
			"className": "3.1 施工技术管理\r",
			"boldContent": "<b>3.1.5施工图变更需经原设计单位认可,当施工图变更涉及通风与空调工程的使用效果和节能效果时,该项变更应经原施工图设计文件审查机构审查,在实施前应办理变更手续,并应获得监理和建设单位的确认。</b>"
		}
	]
}

拿到目录内容

{
	"code": 200,
	"msg": "成功",
	"data": {
		"id": 66,
		"uuid": "3ed0ae31-ae0c-4a7d-91f8-f8736ebe5b4a",
		"criterionUuid": "00335304-963a-406b-98f9-59d8f4a37866",
		"classUuid": "1a51c4ea-2200-48de-a998-e8ad9cf49cab",
		"content": "<b>1.0.1为加强通风与空调工程施工安装技术的管理,规范施工工艺,强化施工安装过程控制,确保工程质量,制定本规范。\r</b></br>1.0.2本规范适用于建筑工程中通风与空调工程的施工安装。\r</br>1.0.3通风与空调工程施工安装中采用的工程技术文件、承包合同文件对工程质量的要求不应低于本规范的规定。\r</br>1.0.4通风与空调工程施工除应符合本规范外,尚应符合国家现行有关标准的规定。\r</br>",
		"isBold": 1,
		"boldContent": "<b>1.0.1为加强通风与空调工程施工安装技术的管理,规范施工工艺,强化施工安装过程控制,确保工程质量,制定本规范。\r</b>",
		"createUser": null,
		"createTime": "2021-09-11 14:53:40",
		"updateUser": null,
		"updateTime": null,
		"deleteUser": null,
		"deleteTime": null,
		"isDelete": 0
	}
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值