aspose处理word文档

一、概述

Aspose.Words支持Doc,Docx,RTF,HTML,OpenDocument,PDF,XPS,EPUB和其他格式

<dependency>

<groupId>com.aspose</groupId>

<artifactId>aspose-words-22.8-jdk17</artifactId>

<version>22.8</version>

</dependency>

jar包付费

组件介绍:https://www.cnblogs.com/asxinyu/p/3242754.html#_labelTop

二、word文档指定位置插入表格

2.1根据内容定位

1.定位文档位置


        Document doc = new Document(modeDest);//原文件地址
        DocumentBuilder builder = new DocumentBuilder(doc);

        NodeCollection runs = doc.getChildNodes(NodeType.PARAGRAPH, true);
        for (int i = 0; i < runs.getCount(); i++) {
            Paragraph r = (Paragraph) runs.get(i);
            String text = r.getRange().getText();//获取段落文本
            if (text.contains("项目设置")&&text.length()<10) {
                //指定段落插入表格
                builder.moveTo(r);
                createTable(builder,info);
            }
        }
        doc.save(destdoc);//新文件地址
 2.创建表格

private void createTable(DocumentBuilder builder,List<LatticeResourceVO> list) {
        //创建table
        builder.startTable();
        String[] titles = new String[] { "序号", "资源名称" };
        int[] lens = new int[] { 50, 50 };
        for (int i=0;i<titles.length;i++) {
            builder.insertCell();//插入单元格
            builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(lens[i]));//列宽-百分比
            builder.getCellFormat().getShading().setBackgroundPatternColor(Color.LIGHT_GRAY);//背景色
            builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);//对齐-居中
            builder.setBold(false);//不加粗
            builder.write(titles[i]);//写入内容
        }
        builder.endRow();//结束行
        for (int i=0;i<list.size();i++) {
            LatticeResourceExportVO exportVO = new LatticeResourceExportVO(list.get(i), i + 1);

            builder.insertCell();//插入单元格
            builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(lens[0]));
            builder.getCellFormat().getShading().setBackgroundPatternColor(Color.WHITE);//背景色-白色
            builder.getParagraphFormat().setAlignment(ParagraphAlignment.JUSTIFY);//对齐-居中
            builder.write(exportVO.getNum().toString());//写入内容

            builder.insertCell();//插入单元格
            builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(lens[1]));
            builder.getCellFormat().getShading().setBackgroundPatternColor(Color.WHITE);//背景色-白色
            builder.getParagraphFormat().setAlignment(ParagraphAlignment.JUSTIFY);//对齐-自适应
            if (StrUtil.isNotEmpty(exportVO.getResourceName())) {
                builder.write(exportVO.getResourceName());
            }
            builder.endRow();//结束行
        }
        //结束当前表格
        builder.endTable();
    }
3.效果

2.2根据书签定位

1.模板设置书签

2.指定书签插入表格

3.效果

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值