• aspose-words导出word方法


    一、引用依赖

                <dependency>
                    <groupId>com.asposegroupId>
                    <artifactId>aspose-wordsartifactId>
                    <version>19.5version>
                    <classifier>jdk17classifier>
                dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    二、导出类

    public class WordTable {
        //定义文档接口
        public Document doc;
        public DocumentBuilder builder;
     public WordTable() {
        }
    	/**
         * 初始化构造方法
         * @param doc 模板地址
         */
        public WordTable(String doc) {
            try {
                // 建立DocumentBuilder物件
                this.doc = new Document(doc);
                this.builder = new DocumentBuilder(this.doc);
            } catch (Exception e) {
                System.out.println("初始化失败" + e.toString());
            }
    
        }
    	/**
         * 新建文档
         * @param doc 新建对象
         */
        public WordTable(Document doc) {
            try {
                // 建立DocumentBuilder物件
                this.doc = doc;
                this.builder = new DocumentBuilder(this.doc);
            } catch (Exception e) {
                System.out.println("初始化失败" + e.toString());
            }
        }
        /**
         * 横向纸张
         */
        public void Portarit(){
            builder.getPageSetup().setOrientation(Orientation.LANDSCAPE);
        }
     	/**
         * 新增表格
         */
        public void createTale(List<OrgWordTarget> orgWordTargets,String orgName) throws Exception {
            //水平居中
            builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
            StyleCollection styles= doc.getStyles();
            builder.getFont().setName("微软雅黑");
            builder.getFont().setBold(true);
            builder.getFont().setSize(18);
            builder.writeln("标题");
            builder.getFont().setName("宋体");
            builder.getFont().setSize(12);
            builder.getFont().setBold(false);
            builder.getParagraphFormat().setLineSpacingRule(LineSpacingRule.MULTIPLE);
            //12为一倍行距
            builder.getParagraphFormat().setLineSpacing(18);
    
            builder.write(orgName+"(盖章):                 ");
            builder.writeln("                                年     月       日   ");
            //创建表格
            Table table = builder.startTable();
            String[] titles = new String[]{"标题1", "标题2", "标题3", "标题4", "标题5", "标题6", "标题7"};
            double[] lens = new double[]{8.8, 18, 14, 15.8, 15.8, 14.6, 12.4};
            builder.getFont().setSize(11);
            builder.getFont().setName("微软雅黑");
            for (int i = 0; i < 7; i++) {
                builder.insertCell();//插入单元格
                //设置宽度注意要在插入单元格后在设置
                builder.getCellFormat().setWidth(lens[i]);
    //            builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(lens[i]));//列宽-百分比
                builder.getCellFormat().getShading().setBackgroundPatternColor(Color.LIGHT_GRAY);//背景色-灰色
                builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);//垂直居中对齐
                builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);//水平居中
    
                builder.write(titles[i]);//写入内容
            }
            builder.endRow();//结束行
            int i=1;
            builder.getFont().setBold(false);
            for (OrgWordTarget o:orgWordTargets) {
                insertCell(i+"");
                builder.getCellFormat().setWidth(lens[0]);
                insertCell(o.getTargetName());
                builder.getCellFormat().setWidth(lens[1]);
                insertCell(o.getDataSource());
                builder.getCellFormat().setWidth(lens[2]);
                insertCell(o.getMethod());
                builder.getCellFormat().setWidth(lens[3]);
                insertCell(o.getExplain());
                builder.getCellFormat().setWidth(lens[4]);
                insertCell(o.getDataUpdateInterval());
                builder.getCellFormat().setWidth(lens[5]);
                insertCell("□ 是  □ 否");
                builder.getCellFormat().setWidth(lens[6]);
                builder.endRow();//结束行
                i++;
            }
            builder.endTable();//结束表格
    
            builder.getFont().setSize(11);
            builder.getFont().setBold(false);
            builder.getFont().setName("宋体");
            builder.writeln();
            builder.writeln("备注:无");
    
            builder.getFont().setSize(12);
            builder.writeln("                                                                              联系人:");
            builder.write("                                                                                联系方式:");
    
            //设置页码
            addHeaderFooter(doc);
    }
        /**
         * 插入方法
         * @param value
         */
        public void insertCell(String value){
            builder.insertCell();//插入单元格
            builder.getCellFormat().getShading().setBackgroundPatternColor(Color.WHITE);//背景色-白色
    //        builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);//对齐-居中
            builder.getParagraphFormat().setAlignment(CellVerticalAlignment.CENTER);//垂直-居中
            if (value==null){
                return;
            }
            builder.write(value);//写入内容
        }
        /**
         * 设置页码
         * @param doc
         * @throws Exception
         */
        public void addHeaderFooter(Document doc) throws Exception {
            //创建页脚 页码
            HeaderFooter footer = new HeaderFooter(doc, HeaderFooterType.FOOTER_PRIMARY);
            doc.getFirstSection().getHeadersFooters().add(footer);
    
            //页脚段落
            Paragraph footerpara = new Paragraph(doc);
            footerpara.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
            Run footerparaRun = new Run(doc);
            footerparaRun.getFont().setName("宋体");
            footerparaRun.getFont().setSize(9.0);//小5号字体
            footerpara.appendChild(footerparaRun);
            footerpara.appendField(FieldType.FIELD_PAGE, true);//当前页码
            footerpara.appendChild(footerparaRun);
            footer.appendChild(footerpara);
        }
        /**
         * 替换模板中的站位符
         *生成站位符方法 选择插入-->文档部件-->域-->类别选择邮箱合并--->MergeField-->自定义域名
         * @throws Exception
         */
        public void setMap() throws Exception {
             String[] landSupplyA = new String[1];
             String[] landSupplyB = new String[1];
            landSupplyA[0] = "test";
            landSupplyB[0] = "测试一下";
    
            doc.getMailMerge().execute(landSupplyA, landSupplyB);
        }
    }
    
    • 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

    占位符位置截图:
    替换符位置

  • 相关阅读:
    0-1规划的MATLAB求解
    RegNet架构复现--CVPR2020
    语音分离---学习笔记(1)
    Android聚合SDK母包反编译出包教程
    问题 D: 是否为有效的拓扑序列
    Gin中间件
    智能时代的高效协作工具-TeamLinker,让团队像局域网一样工作
    Vue_组件间通信
    mysql数据库 - 统诉
    Vue2高级-axios和vue-resource、配置代理服务器(解决跨域问题)
  • 原文地址:https://blog.csdn.net/qq_42051115/article/details/122208463