使用docx4j生成数据库字典文档

使用docx4j生成数据库字典文档

文本参考文章

https://www.cnblogs.com/qlqwjy/p/9866484.html

https://my.oschina.net/haiyangyiba/blog/2246089

https://github.com/plutext/docx4j/

1.引入docx4j

本案例使用maven仓库引入jar包

<dependency>
    <groupId>org.docx4j</groupId>
    <artifactId>docx4j</artifactId>
    <version>6.1.2</version>
</dependency>

<!-- lombok -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.18</version>
</dependency>

<!-- JSON插件 -->
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.47</version>
</dependency>

2.docx4j配置

可以不添加配置文件,但debug日志会提示找不到docx4j配置文件

在src下创建docx4j.properties配置文件,maven项目请放在resources文件夹下

# Page size: use a value from org.docx4j.model.structure.PageSizePaper enum
# eg A4, LETTER
docx4j.PageSize=LETTER
# Page size: use a value from org.docx4j.model.structure.MarginsWellKnown enum
docx4j.PageMargins=NORMAL
docx4j.PageOrientationLandscape=false
# Page size: use a value from org.pptx4j.model.SlideSizesWellKnown enum
# eg A4, LETTER
pptx4j.PageSize=LETTER
pptx4j.PageOrientationLandscape=false
# These will be injected into docProps/app.xml
# if App.Write=true
docx4j.App.write=true
docx4j.Application=docx4j
docx4j.AppVersion=6.1.2
# of the form XX.YYYY where X and Y represent numerical values
# These will be injected into docProps/core.xml
docx4j.dc.write=true
docx4j.dc.creator.value=docx4j
docx4j.dc.lastModifiedBy.value=docx4j
#
#docx4j.McPreprocessor=true
# If you haven't configured log4j yourself
# docx4j will autoconfigure it. Set this to true to disable that
docx4j.Log4j.Configurator.disabled=false

3.生成数据字典.docx

    public static void main(String[] args) throws Exception{
        String templateFilePath = "template.docx";
        String wordFilePath = "数据字典.docx";
        HashMap<String, String> params = new HashMap<>();
        params.put("system_name", "xxx系統");
        params.put("title", "數據字典");
        params.put("author", "test");
        params.put("date", "2021-03-21");

        // 数据字典
        DataDict dataDict = mockData();
        // 创建word
        WordprocessingMLPackage wordMLPackage = Docx4jUtils.load(templateFilePath);
        // 写入数据清单
        writeTableDict(wordMLPackage, dataDict);
        // 写入数据明细
        writeTableDetail(wordMLPackage, dataDict);
        // 参数替换
        Docx4jUtils.variableReplace(wordMLPackage, params);
        // 写入目录
        writeToc(wordMLPackage);
        // save
        Docx4jUtils.save(wordMLPackage, wordFilePath);
    }

    /**
     * 写入数据清单
     */
    private static void writeTableDict(WordprocessingMLPackage wordMLPackage, DataDict dataDict) throws Exception{
        // 查找table
        ClassFinder find = new ClassFinder(Tbl.class);
        new TraversalUtil(wordMLPackage.getMainDocumentPart().getContent(), find);
        //获取到第三个表格元素=数据清单
        Tbl table = (Tbl) find.results.get(2);
        // 第二行约定为模板,获取到第二行内容
        Tr dynamicTr = (Tr) table.getContent().get(1);
        // 获取模板行的xml数据
        String dynamicTrXml = XmlUtils.marshaltoString(dynamicTr);

        // 替换数据
        LinkedHashMap<String, String> map;
        for(UserTable userTable : dataDict.getUserTables()){
            map = new LinkedHashMap<>();
            map.put("table_name", userTable.getTableName());
            map.put("table_comment", userTable.getComments());
            // 填充模板行数据
            Tr newTr = (Tr) XmlUtils.unmarshallFromTemplate(dynamicTrXml, map);
            table.getContent().add(newTr);
        }

        // 删除模板行的占位行
        table.getContent().remove(1);
    }

    /**
     * 写入数据表明细
     */
    private final static String TITLE = "2.%s %s %s";
    private static void writeTableDetail(WordprocessingMLPackage wordMLPackage, DataDict dataDict){
        // 提取正文
        MainDocumentPart main = wordMLPackage.getMainDocumentPart();
        ObjectFactory factory = Context.getWmlObjectFactory();

        // 数据表
        List<UserTable> userTables = dataDict.getUserTables();
        List<String> primaryKeys = new ArrayList<>();
        for(int i=0; i<userTables.size(); i++){
            UserTable userTable = userTables.get(i);
            // 设置标题
            // styleId=3为标题2
            main.addStyledParagraphOfText("3", String.format(TITLE, (i+1), userTable.getTableName(), userTable.getComments()));

            // 创建表格
            Tbl table = factory.createTbl();
            // 必须设置一个TblPr,否则最后会报空指针异常
            table.setTblPr(new TblPr());

            // 设置表格边框样式
            // 设置表格边框样式
            TableBorderStyle borderStyle = new TableBorderStyle();
            borderStyle.setColor("auto");
            borderStyle.setSize(new BigInteger("4"));
            borderStyle.setSpace(new BigInteger("0"));
            borderStyle.setStyle(STBorder.SINGLE);
            CTBorder ctBorder = Docx4jUtils.buildTableBorder(borderStyle);
            TblBorders tblBorders = Docx4jUtils.setTableBorder(ctBorder,ctBorder,ctBorder,ctBorder);
            table.getTblPr().setTblBorders(tblBorders);
            table.getTblPr().setTblW(Docx4jUtils.buildTblWidth(BigInteger.valueOf(5000)));

            // 写入标题
            addTableTitle(factory, table);

            // 字段信息
            List<UserTabColumn> userTabColumns = dataDict.getUserTabColumns().get(userTable.getTableName());
            for(UserTabColumn userTabColumn : userTabColumns){
                // 创建行
                Tr tr = factory.createTr();
                String columnName = userTabColumn.getColumnName();
                List<UserColComment> userColComments = dataDict.getUserColComment().get(userTable.getTableName() + columnName);
                String columnComment = userColComments.get(0).getComments();

                String dataType = userTabColumn.getDataType();
                switch (dataType){
                    case "NCLOB":
                        dataType = "CLOB";
                        break;
                    case "NBLOB":
                        dataType = "BLOB";
                        break;
                    case "NVARCHAR2":
                        dataType = "VARCHAR2";
                        break;
                }
                if (!("CLOB".equals(dataType) || "BLOB".equals(dataType) || "NUMBER".equals(dataType))) {
                    dataType = String.format("%s(%s)", dataType, userTabColumn.getDataLength());
                } else if ("NUMBER".equals(dataType)) {
                    dataType = String.format("%s(%s,%s)",dataType,userTabColumn.getDataPrecision(),userTabColumn.getDataScale());
                }
                String notNull = "TRUE";
                if ("NO".equals(userTabColumn.getDefaultOnNull())) {
                    notNull = "FALSE";
                }

                tr.getContent().add(buildTableRow(factory, columnName,1000));
                tr.getContent().add(buildTableRow(factory, dataType,1000));
                tr.getContent().add(buildTableRow(factory, notNull,1000));
                tr.getContent().add(buildTableRow(factory, columnComment,2000));
                table.getContent().add(tr);
            }

            // 索引信息
            List<String> idxData = new ArrayList<>();

            // 主键索引信息
            primaryKeys.clear();
            List<UserConstraint> userConstraints = dataDict.getUserConstraints().get(userTable.getTableName());
            if(null != userConstraints && userConstraints.size() > 0){
                for(UserConstraint userConstraint : userConstraints){
                    primaryKeys.add(userConstraint.getIndexName());
                    List<UserIndColumn> userIndColumns = dataDict.getUserIndColumns().get(userConstraint.getTableName() + userConstraint.getIndexName());
                    StringBuilder pkCols = new StringBuilder();
                    for(UserIndColumn userIndColumn : userIndColumns){
                        pkCols.append("\"").append(userIndColumn.getColumnName()).append("\",");
                    }
                    idxData.add("主键: " + userConstraint.getIndexName() + "(" + pkCols.substring(0, pkCols.length()-1) + ")");
                }
            }

            // 非主键索引
            List<UserIndex> userIndices = dataDict.getUserIndexes().get(userTable.getTableName());
            if(null != userIndices && userIndices.size() > 0){
                for(UserIndex userIndex : userIndices){
                    if (primaryKeys.contains(userIndex.getIndexName())) {
                        continue;
                    }

                    List<UserIndColumn> userIndColumns = dataDict.getUserIndColumns().get(userIndex.getTableName() + userIndex.getIndexName());
                    if(null == userIndColumns || userIndColumns.size() == 0){
                        continue;
                    }

                    StringBuilder stb = new StringBuilder();
                    boolean flag = false;
                    for (UserIndColumn userIndColumn : userIndColumns) {
                        if (userIndex.getIndexName().equals(userIndColumn.getIndexName())) {
                            stb.append("\"").append(userIndColumn.getColumnName()).append("\",");
                            flag = true;
                        }
                    }
                    if (flag) {
                        String uniqueness = "NONUNIQUE".equals(userIndex.getUniqueness()) ? "索引" : "唯一索引";
                        idxData.add(String.format("%s: %s(%s)", uniqueness, userIndex.getIndexName(), stb.substring(0, stb.length() -1)));
                    }
                }
            }

            // 新增索引信息
            addTableIndex(factory, table, idxData, BigInteger.valueOf(5000));

            // 加表格加到主要内容中
            main.addObject(table);
        }
    }

    /**
     * 新增表格标题
     */
    private static void addTableTitle(ObjectFactory factory, Tbl table){
        Tr tr = factory.createTr();
        tr.getContent().add(buildTableTitle(factory, "字段名稱", 1000));
        tr.getContent().add(buildTableTitle(factory, "字段類型", 1000));
        tr.getContent().add(buildTableTitle(factory, "是否強制", 1000));
        tr.getContent().add(buildTableTitle(factory, "字段描述", 2000));
        table.getContent().add(tr);
    }

    /**
     * 构建表格标题
     */
    private static Tc buildTableTitle(ObjectFactory factory, String title, int width){
        Tc tc = factory.createTc();
        P p = factory.createP();
        R r = factory.createR();

        // 设置表格标题字体样式
        FontStyle tableTitleStyle = new FontStyle();
        tableTitleStyle.setSize(BigInteger.valueOf(24));
        tableTitleStyle.setBold(true);
        RPr rPr = Docx4jUtils.buildFontStyle(factory, tableTitleStyle);
        r.setRPr(rPr);

        // 内容
        Text text = factory.createText();
        text.setValue(title);

        TcPr tcPr = Docx4jUtils.getTcPr(tc);
        // 设置宽度
        tcPr.setTcW(Docx4jUtils.buildTblWidth(BigInteger.valueOf(width)));

        // 设置背景颜色
        CTShd shd = new CTShd();
        shd.setVal(STShd.CLEAR);
        shd.setColor("auto");
        shd.setFill("eeeeee");
        tcPr.setShd(shd);

        r.getContent().add(text);
        p.getContent().add(r);
        tc.getContent().add(p);
        return tc;
    }

    /**
     * 构建行信息
     */
    private static Tc buildTableRow(ObjectFactory factory, String content, int width){
        Tc tc = factory.createTc();
        P p = factory.createP();
        R r = factory.createR();

        // 设置字体样式
        FontStyle tableContentStyle = new FontStyle();
        tableContentStyle.setSize(BigInteger.valueOf(22));
        RPr rPr = Docx4jUtils.buildFontStyle(factory, tableContentStyle);
        r.setRPr(rPr);

        // 内容
        Text text = factory.createText();
        text.setValue(content);

        // 设置宽度
        Docx4jUtils.getTcPr(tc).setTcW(Docx4jUtils.buildTblWidth(BigInteger.valueOf(width)));

        r.getContent().add(text);
        p.getContent().add(r);
        tc.getContent().add(p);
        return tc;
    }

    /**
     * 新增索引信息
     */
    private static void addTableIndex(ObjectFactory factory, Tbl table, List<String> data, BigInteger width){
        Tr tr = factory.createTr();
        Tc tc = factory.createTc();
        // 设置宽度
        Docx4jUtils.getTcPr(tc).setTcW(Docx4jUtils.buildTblWidth(width));

        for(String item : data){
            P p = factory.createP();
            R r = factory.createR();

            // 设置字体样式
            FontStyle tableContentStyle = new FontStyle();
            tableContentStyle.setSize(BigInteger.valueOf(22));
            RPr rPr = Docx4jUtils.buildFontStyle(factory, tableContentStyle);
            r.setRPr(rPr);

            // 内容
            Text text = factory.createText();
            text.setValue(item);

            r.getContent().add(text);
            p.getContent().add(r);
            tc.getContent().add(p);
        }
        tr.getContent().add(tc);
        table.getContent().add(tr);
    }

    /**
     * 写入目录
     */
    private static void writeToc(WordprocessingMLPackage wordMLPackage) throws Exception{
        // 提取正文
        MainDocumentPart main = wordMLPackage.getMainDocumentPart();

        List<Object> list = main.getContent();
        for(int i=0; i<list.size(); i++){
            Object item = list.get(i);
            if("目錄".equals(item.toString())){
                Docx4jUtils.tocGenerator(wordMLPackage, i, "目錄");
                P p = (P)item;
                p.getContent().removeAll(p.getContent());
                break;
            }
        }
    }

4.模板信息截图

5.生成数据字典截图

 

6.docx4j相关API

docx格式文档可以理解为一个压缩包,若将其解压可看到一个类似前端的工程项目,其中document.xml用于全文的配置,详细解说请自行查阅资料

创建新的word文档

    /**
     * 创建新的word文件
     */
    public static WordprocessingMLPackage build() throws InvalidFormatException {
        return WordprocessingMLPackage.createPackage();
    }

读取存在的word文件

    /**
     * 读取存在的word文件
     * @param wordFilePath word文件路径
     */
    public static WordprocessingMLPackage load(String wordFilePath) throws Docx4JException{
        return WordprocessingMLPackage.load(new File(wordFilePath));
    }

保存word文件

    /**
     * 保存word文件
     * @param wordMLPackage word
     */
    public static void save(WordprocessingMLPackage wordMLPackage,String wordFilePath) throws Docx4JException {
        wordMLPackage.save(new File(wordFilePath));
    }

构建字体样式

    /**
     * 构建字体样式
     * @return RPr
     */
    public static RPr buildFontStyle(ObjectFactory factory, FontStyle style){
        RPr rpr = factory.createRPr();

        //设置字体
        if(StringUtils.isNotBlank(style.getCnFontFamily()) || StringUtils.isNotBlank(style.getEnFontFamily())){
            RFonts font = new RFonts();
            font.setAscii(StringUtils.isBlank(style.getCnFontFamily()) ? "宋体" : style.getCnFontFamily());
            if(StringUtils.isNotBlank(style.getEnFontFamily())){
                font.setEastAsia(style.getEnFontFamily());
            }
            rpr.setRFonts(font);
        }

        //设置颜色
        if(StringUtils.isNotBlank(style.getColor())){
            Color color = new Color();
            color.setVal(style.getColor());
            rpr.setColor(color);
        }

        //设置字体大小
        if(null != style.getSize()){
            HpsMeasure fontSize = new HpsMeasure();
            fontSize.setVal(style.getSize());
            rpr.setSzCs(fontSize);
            rpr.setSz(fontSize);
        }

        //设置粗体
        if(null != style.getBold()){
            BooleanDefaultTrue bold = factory.createBooleanDefaultTrue();
            bold.setVal(style.getBold());
            rpr.setB(bold);
        }

        //设置斜体
        if(null != style.getItalic()){
            BooleanDefaultTrue italic = new BooleanDefaultTrue();
            rpr.setI(italic);
        }

        //设置删除线
        if(null != style.getDeleteLine()){
            BooleanDefaultTrue deleteLine = new BooleanDefaultTrue();
            deleteLine.setVal(style.getDeleteLine());
            rpr.setStrike(deleteLine);
        }

        //设置下划线
        if(null != style.getUnderlineEnumeration()){
            U u = factory.createU();
            u.setVal(style.getUnderlineEnumeration());
            rpr.setU(u);
        }

        // 设置背景颜色
        if (null != style.getBackgroundColor()) {
            CTShd shd = new CTShd();
            shd.setVal(style.getBackgroundColor());
            rpr.setShd(shd);
        }

        // 设置边框样式
        if(StringUtils.isNotBlank(style.getBorderColor()) || null != style.getBorderSize() || null != style.getBorderSpace()){
            CTBorder border = new CTBorder();
            if(StringUtils.isNotBlank(style.getBorderColor())){
                border.setColor(style.getBorderColor());
            }

            if(null != style.getBorderSize()){
                border.setSz(style.getBorderSize());
            }

            if(null != style.getBorderSpace()){
                border.setSpace(style.getBorderSpace());
            }

            if(null != style.getBorderShadow()){
                border.setShadow(style.getBorderShadow());
            }

        }

        // 着重号
        if(null != style.getEmphasis()){
            CTEm em = new CTEm();
            em.setVal(style.getEmphasis());
            rpr.setEm(em);
        }

        // 空心
        if(null != style.getOutline()){
            BooleanDefaultTrue outline = new BooleanDefaultTrue();
            outline.setVal(style.getOutline());
            rpr.setOutline(outline);
        }

        // 设置上标下标
        if(null != style.getVerticalAlign()){
            CTVerticalAlignRun value = new CTVerticalAlignRun();
            value.setVal(style.getVerticalAlign());
            rpr.setVertAlign(value);
        }

        // 设置字符间距缩进
        if(null != style.getIndent()){
            CTTextScale value = new CTTextScale();
            value.setVal(style.getIndent());
            rpr.setW(value);
        }

        // 设置字符间距信息
        if(null != style.getSpacing()){
            CTSignedTwipsMeasure value = new CTSignedTwipsMeasure();
            value.setVal(style.getSpacing());
            rpr.setSpacing(value);
        }

        // 设置文本位置
        if(null != style.getPosition()){
            CTSignedHpsMeasure ctPosition = new CTSignedHpsMeasure();
            ctPosition.setVal(style.getPosition());
            rpr.setPosition(ctPosition);
        }

        // 阴文
        if(null != style.getImprint()){
            BooleanDefaultTrue imprint = new BooleanDefaultTrue();
            imprint.setVal(style.getImprint());
            rpr.setImprint(imprint);
        }

        // 阳文
        if(null != style.getEmboss()){
            BooleanDefaultTrue emboss = new BooleanDefaultTrue();
            emboss.setVal(style.getEmboss());
            rpr.setEmboss(emboss);
        }

        // 设置隐藏
        if(null != style.getVanish()){
            BooleanDefaultTrue vanish = new BooleanDefaultTrue();
            vanish.setVal(style.getVanish());
            rpr.setVanish(vanish);
        }

        // 设置阴影
        if(null != style.getShadow()){
            BooleanDefaultTrue shadow = new BooleanDefaultTrue();
            shadow.setVal(style.getShadow());
            rpr.setShadow(shadow);
        }

        // 设置底纹
        if(null != style.getShading()){
            CTShd shd = new CTShd();
            shd.setVal(style.getShading());
            rpr.setShd(shd);
        }

        // 设置突出显示文本
        if(null != style.getHightlight()){
            Highlight highlight = new Highlight();
            highlight.setVal(style.getHightlight());
            rpr.setHighlight(highlight);
        }

        // 设置删除线
        if(null != style.getStrike()){
            BooleanDefaultTrue strike = new BooleanDefaultTrue();
            strike.setVal(style.getStrike());
            rpr.setStrike(strike);
        }

        // 设置双删除线
        if(null != style.getDoubleStrike()){
            BooleanDefaultTrue strike = new BooleanDefaultTrue();
            strike.setVal(style.getDoubleStrike());
            rpr.setDstrike(strike);
        }

        // 倾斜
        if(null != style.getTilt()){
            BooleanDefaultTrue b = new BooleanDefaultTrue();
            b.setVal(style.getTilt());
            rpr.setI(b);
        }

        return rpr;
    }

设置表格宽度

    /**
     * 设置表格宽度
     * @param val 宽度 5000为百分之百的宽度
     * @return TblWidth
     */
    public static TblWidth buildTblWidth(BigInteger val){
        TblWidth width = new TblWidth();
        width.setW(val);
        //此处试了几种方式均不好用,只有这个pct的合适,50分之一是一个百分点,5000为百分之百的宽度
        width.setType("pct");
        return width;
    }

模板参数替换

    /**
     * 模板参数替换
     * 格式 ${param1}
     */
    public static void variableReplace(MainDocumentPart main, HashMap<String, String> params) throws JAXBException, Docx4JException{
        main.variableReplace(params);
    }

    /**
     * 模板参数替换
     * 格式 ${param1}
     */
    public static void variableReplace(WordprocessingMLPackage wordMLPackage, HashMap<String, String> params) throws JAXBException, Docx4JException{
        // 提取正文
        MainDocumentPart main = wordMLPackage.getMainDocumentPart();
        variableReplace(main, params);
    }

生成目录

    /**
     * 生成目录
     * @param wordMLPackage WordprocessingMLPackage
     * @param index 生成目录位置
     * @param tocHeadingText 目录标题
     */
    public static void tocGenerator(WordprocessingMLPackage wordMLPackage, int index, String tocHeadingText) throws TocException {
        TocGenerator tocGenerator = new TocGenerator(wordMLPackage);
        if(StringUtils.isNotBlank(tocHeadingText)){
            Toc.setTocHeadingText(tocHeadingText);
        }
        // skipPageNumbering如果设置为TRUE 则会联网 要付费
        tocGenerator.generateToc(index, "TOC \\o \"1-3\" \\h \\z \\u", true);
    }

构造表格边框样式

    /**
     * 构造表格边框样式
     * @param style 样式配置
     * @return CTBorder
     */
    public static CTBorder buildTableBorder(TableBorderStyle style){
        CTBorder border = new CTBorder();
        if(StringUtils.isNotBlank(style.getColor())){
            border.setColor(style.getColor());
        }

        if(null != style.getSize()){
            border.setSz(style.getSize());
        }

        if(null != style.getSpace()){
            border.setSpace(style.getSpace());
        }

        STBorder val = null != style.getStyle() ? style.getStyle() : STBorder.SINGLE;
        border.setVal(val);
        return border;
    }

设置表格边框样式

    /**
     * 设置表格边框样式
     * @param top 上边框
     * @param bottom 下边框
     * @param left 左边框
     * @param right 右边框
     * @return TblBorders
     */
    public static TblBorders setTableBorder(CTBorder top, CTBorder bottom, CTBorder left, CTBorder right){
        TblBorders borders = new TblBorders();
        borders.setBottom(top);
        borders.setLeft(bottom);
        borders.setRight(left);
        borders.setTop(right);
//        borders.setInsideH(border);
//        borders.setInsideV(border);
        return borders;
    }

FontStyle

import lombok.Data;
import org.docx4j.wml.STEm;
import org.docx4j.wml.STShd;
import org.docx4j.wml.STVerticalAlignRun;
import org.docx4j.wml.UnderlineEnumeration;

import java.math.BigInteger;

/**
 * 字体样式
 */
@Data
public class FontStyle {
    /** 中文字体 tips:宋体 **/
    private String cnFontFamily;

    /** 英文字体 **/
    private String enFontFamily;

    /** 字体大小 **/
    private BigInteger size;

    /** 字体颜色 **/
    private String color;

    /** 是否加粗 **/
    private Boolean bold;

    /** 是否斜体 **/
    private Boolean italic;

    /** 是否删除线 **/
    private Boolean deleteLine;
    /**
     * 设置下划线
     * UnderlineEnumeration.SINGLE 单下划线
     * UnderlineEnumeration.DOUBLE 双下划线
     * UnderlineEnumeration.DASH 虚线
     * UnderlineEnumeration.WAVE 波浪线
     */
    private UnderlineEnumeration underlineEnumeration;

    /** 背景颜色 **/
    private STShd backgroundColor;

    /** 字体边框颜色 **/
    private String borderColor;

    /** 字体边框大小 **/
    private BigInteger borderSize;

    /** 字体边框间距 **/
    private BigInteger borderSpace;

    /** 字体边框阴影 **/
    private Boolean borderShadow;

    /** 着重号 **/
    private STEm emphasis;

    /** 空心 **/
    private Boolean outline;

    /** 设置上标下标 **/
    private STVerticalAlignRun verticalAlign;

    /** 设置字符间距缩进 **/
    private Integer indent;

    /** 设置字符间距信息 **/
    private BigInteger spacing;

    /** 设置文本位置 **/
    private BigInteger position;

    /** 阴文 **/
    private Boolean imprint;

    /** 阳文 **/
    private Boolean emboss;

    /** 设置隐藏 **/
    private Boolean vanish;

    /** 设置阴影 **/
    private Boolean shadow;

    /** 设置底纹 **/
    private STShd shading;

    /** 设置突出显示文本 **/
    private String hightlight;

    /** 设置单删除线 **/
    private Boolean strike;

    /** 设置双删除线 **/
    private Boolean doubleStrike;

    /** 倾斜 **/
    private Boolean tilt;

}

TableBorderStyle

import lombok.Data;
import org.docx4j.wml.STBorder;

import java.math.BigInteger;

/**
 * 表格边框样式
 */
@Data
public class TableBorderStyle {

    /** 边框大小 **/
    private BigInteger size;

    /** 边框间距 **/
    private BigInteger space;

    /** 边框颜色 **/
    private String color;

    /** 边框样式 **/
    private STBorder style;
}

 

 

7.目录相关操作疑问

目前看 目录如果需要带页码就需要企业版,  还有生成和查找方法,目前找到的都不行,只有这样做法能勉强达到效果,但生成的目录点击跳转不了,求大神指导~~

TocGenerator tocGenerator = new TocGenerator(wordMLPackage);
if(StringUtils.isNotBlank(tocHeadingText)){
    Toc.setTocHeadingText(tocHeadingText);
}
// skipPageNumbering如果设置为TRUE 则会联网 要付费
tocGenerator.generateToc(index, "TOC \\o \"1-3\" \\h \\z \\u", true);

其他方法如下:

    public static void main(String[] args) throws Exception{
        TocGenerator tocGenerator = new TocGenerator(wordMLPackage);
        Toc.setTocHeadingText("目录");
        tocGenerator.generateToc( 3, "TOC \\o \"1-3\"", true);

        Document wmlDocumentEl = main.getJaxbElement();
        Body body =  wmlDocumentEl.getBody();
        TocFinder finder = new TocFinder();
        new TraversalUtil(body.getContent(), finder);
        System.out.println(finder.getTocSDT().getSdtContent().getContent().size());

        wordMLPackage.getMainDocumentPart()
                .getDocumentSettingsPart().getJaxbElement()
                .setUpdateFields(new BooleanDefaultTrue());

        TocGenerator tocGenerator = new TocGenerator(wordMLPackage);
        tocGenerator.updateToc( true);

        VariablePrepare.prepare(wordMLPackage);

        ObjectFactory factory = Context.getWmlObjectFactory();
        P paragraph = factory.createP();

        // addFieldBegin
        addFieldBegin(factory, paragraph);
        addTableOfCententField(factory, paragraph);
        // addFieldEnd
        addFieldEnd(factory, paragraph);

    }

    private static void addFieldBegin(ObjectFactory factory, P paragraph){
        R run = factory.createR();
        FldChar fldChar = factory.createFldChar();
        fldChar.setFldCharType(STFldCharType.BEGIN);
        fldChar.setDirty(true);
        run.getContent().add(getWrappendFldChar(fldChar));
        paragraph.getContent().add(run);
    }

    private static void addTableOfCententField(ObjectFactory factory, P paragraph){
        R run = factory.createR();
        Text text = new Text();
        text.setSpace("preserve");
        text.setValue("TOC \\o \"1-3\" \\h \\z \\u");
        run.getContent().add(factory.createRInstrText(text));
        paragraph.getContent().add(run);
    }

    private static void addFieldEnd(ObjectFactory factory, P paragraph){
        R run = factory.createR();
        FldChar fldChar = factory.createFldChar();
        fldChar.setFldCharType(STFldCharType.END);
        fldChar.setDirty(true);
        run.getContent().add(getWrappendFldChar(fldChar));
        paragraph.getContent().add(run);
    }

    private static JAXBElement getWrappendFldChar(FldChar fldChar){
        return new JAXBElement(new QName(Namespaces.NS_WORD12, "fldChar"), FldChar.class, fldChar);
    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
电商项目数据库设计文档全文共7页,当前为第1页。电商项目数据库设计文档全文共7页,当前为第1页。 电商项目数据库设计文档全文共7页,当前为第1页。 电商项目数据库设计文档全文共7页,当前为第1页。 第六小组 潮易购电商管理系统 数据库设计文档 .段海旭,孙振华,王海伦等 2018-11-30 电商项目数据库设计文档全文共7页,当前为第2页。电商项目数据库设计文档全文共7页,当前为第2页。 电商项目数据库设计文档全文共7页,当前为第2页。 电商项目数据库设计文档全文共7页,当前为第2页。 目录 1 文档介绍 1 1.1 编写目的 1 1.2 适用范围 1 1.3 读者对象 1 2 数据库环境说明 1 3 数据库的命名规则 2 4 逻辑设计 2 5 物理设计 2 5.1表汇总 2 5.2各表信息 3 1.用户基本信息表(T_userInfo) 3 2.角色信息表(T_Root) 3 3.用户订单基本信息表(T_Userirdercon) 3 4.用户订单详细信息表(T_Userorderdetail) 4 5.商品类别的基本信息表(T_Goodstype) 4 6.商品基本信息表(T_Goodscon) 4 7.特价/主题商品信息表(T_Specialgoods) 5 8.购物车信息表(T_Shopping_Cart) 5 6 安全性设计 6 6.1防止用户直接操作数据库的方法 6 7 数据库管理与维护说明 6 文档介绍 编写目的 作为软件设计文档的重要组成部分,本文档主要对该系统后台数据库的概念模型设计和物理模型设计作出了统一的规定,同时确定了每个表的数据字典结构。它是开发人员,测试人员编码及测试的重要参考依据。 1.2 适用范围 本概要设计文档提供给系统设计开发人员,包括详细设计人员和项目组成员,不得提供给组外人员 电商项目数据库设计文档全文共7页,当前为第3页。电商项目数据库设计文档全文共7页,当前为第3页。1.3 读者对象 电商项目数据库设计文档全文共7页,当前为第3页。 电商项目数据库设计文档全文共7页,当前为第3页。 本文档的主要读者包括: 本系统的设计人员:包括模块设计人员 本系统的系统设计人员:包括数据库开发,编码人员 本系统的测试人员 数据库环境说明 数据库采用Micrsoft SQL Server数据库管理系统建立并维护。数据库设计过程中采用Micrsoft公司的Visio创建潮易购数据库的ER图,并生成数据库脚本文件"数据库设计.DLL"。其中SQL Server的登录模式为混合身份验证,超级用户的用户名用户名均为sa,密码为123456,SQL Server服务器的端口号:1433。 数据库的命名规则 符合3个范式: 主键关系,表间关系、表中字段是不可再分的属性。 表的表示:描述单一信息,功能简单实用、命名规范合理。 字段的类型,程度。 数据库的命名:采用首字母大写模式。 如:潮易购电商管理,数据库名称为ChaoYiBuys(潮流商店)。 数据库表命名:所有表以T_开头,后面跟英文解释,采用首字母大写形式。 如:用户基本信息数据库名称为T_UserInfo 电商项目数据库设计文档全文共7页,当前为第4页。电商项目数据库设计文档全文共7页,当前为第4页。逻辑设计 电商项目数据库设计文档全文共7页,当前为第4页。 电商项目数据库设计文档全文共7页,当前为第4页。 本系统的数据库按照面向对象的思想,设计对应实体类,由实体类生成对应的数据库表,数据库中的关系,反应了对象间的关系。 物理设计 5.1表汇总 序号 数据库数据库表存储内容 1 T_UserInfo 用户基本信息 2 T_Userordercon 用户订单的基本信息,订单发送地址等 3 T_Userorderdetail 用户订单的商品内容 4 T_Goodstype 商城内物品的类别信息 5 T_Goodscon 商城内物品的基本信息 6 T_Adminuserinfor 管理员的基本信息 7 T_Bulletioncon 公告栏的基本信息 8 T_Specialgoods 特价/主题商品信息 5.2各表信息 1.用户基本信息表(T_userInfo) 序号 字段名 字段类型 说明 备注 1 Usr_id Int 用户编号 Primary key 2 Usr_name Varchar(50) 用户名 Not Null 3 Usr_pwd Varchar(50) 密码 Not Null 4 Usr_realname Varchar(50) 真实姓名 Not Null 5 Usr_sex Varchar(2) 性别 6 Usr_tel Varchar(11) 电话 7 Usr_email Varchar(100) E_mail 8 Usr_addres

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值