java使用aspose操作word文档

继之前的spire,再来一篇。废话少说,就是想白嫖。上干货!

一、引入依赖

1、以下代码写入pom中

       <dependency>
            <groupId>aspose.words</groupId>
            <artifactId>aspose-words</artifactId>
            <version>15.8.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/libs/aspose-words-15.8.0-jdk16.jar</systemPath>
        </dependency>

2、项目里建个libs文件夹,然后把你的包或者从我这里下载的包粘贴进去

3、公共核心类:

import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.License;
import org.springframework.stereotype.Component;
import java.io.InputStream;
import java.util.Map;
@Component
public class AsPoseWordUtil {
    private static License aposeLic = new License();
    private static boolean getlic(){
        if(!aposeLic.getIsLicensed()){
            InputStream is = AsPoseWordUtil.class.getClassLoader()
                    .getResourceAsStream("asp-license/config.xml");
            try {
                aposeLic.setLicense(is);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }

    /**
     * 定位书签替换内容
     * @param docUrl 文件路径
     * @param savePath 保存路径
     * @param wordMap
     * @throws Exception
     */
    public static void BookMarkReplace(String docUrl,String savePath,Map<String,String> wordMap) throws Exception
    {
        if(!getlic()){
            System.out.print("获取证书失败");
        }
        Document doc = new Document(docUrl);
        DocumentBuilder builder = new DocumentBuilder(doc);
        //将数据插入到书签中
        for(Map.Entry<String,String> item:wordMap.entrySet()) {
            String keyVal = item.getKey();
            String val = item.getValue();
            builder.moveToBookmark(keyVal);
            builder.write(val);
        }
        doc.save(savePath);
    }


    public static void CreateTable(String docUrl,String saveUrl, String bookmark,String[][] data) throws Exception {
        if(!getlic()){
            System.out.print("获取证书失败");
        }
        Document doc = new Document(docUrl);
        DocumentBuilder builder = new DocumentBuilder(doc);
        //移动到该书签位置
        builder.moveToBookmark(bookmark);
        //创建table
        builder.startTable();
        for(int i = 0;i<data.length;i++){
            for(int j=0;j<data[i].length;j++){
                //创建单元格
                builder.insertCell();
                //创建单元格
                builder.write(data[i][j]);
            }
            //结束当前行
            builder.endRow();
        }
        //结束当前表格
        builder.endTable();
        doc.save(saveUrl);
    }
}

4、配置类,放到resources下,我的目录是见上面的图2,此处不再赘述

<License>
  <Data>
    <Products>
      <Product>Aspose.Total for Java</Product>
      <Product>Aspose.Words for Java</Product>
    </Products>
    <EditionType>Enterprise</EditionType>
    <SubscriptionExpiry>20991231</SubscriptionExpiry>
    <LicenseExpiry>20991231</LicenseExpiry>
    <SerialNumber>23dcc79f-44ec-4a23-be3a-03c1632404e9</SerialNumber>
  </Data>
  <Signature>0nRuwNEddXwLfXB7pw66G71MS93gW8mNzJ7vuh3Sf4VAEOBfpxtHLCotymv1PoeukxYe31K441Ivq0Pkvx1yZZG4O1KCv3Omdbs7uqzUB4xXHlOub4VsTODzDJ5MWHqlRCB1HHcGjlyT2sVGiovLt0Grvqw5+QXBuinoBY0suX0=</Signature>
</License>

5、测试类

    public static void main(String args[]) throws Exception {
        String docUrl = "D:/ideaworkspace/files/template/test.docx";
        String saveUrl = "D:/ideaworkspace/files/template/test1.docx";
        Map<String,String> dataMap = new HashMap<String, String>();
        dataMap.put("zxm_option","草泥马a");
        dataMap.put("zxm_idea","拉到了裤子里");
        AsPoseWordUtil.BookMarkReplace(docUrl,saveUrl,dataMap);
        String[][] data = {
                new String[]{"序号","姓名","性别","联系方式"},
                new String[]{"1","朱晓明","男","123"},
                new String[]{"2","潘周丹","女","321"}
        };
        AsPoseWordUtil.CreateTable(docUrl,saveUrl,"zxm_table",data);
        System.out.print("success");
    }

6、图片的有空再整,目前干货到这,大家采集用吧。

-----------------------------------------------------------参考文献----------------------------------------------------------
1、aspose插入word:https://blog.csdn.net/a3562323/article/details/123527216?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~aggregatepage~first_rank_ecpm_v1~rank_v31_ecpm-11-123527216-null-null.pc_agg_new_rank&utm_term=asposewords%E5%BE%AA%E7%8E%AF%E6%8F%92%E5%85%A5&spm=1000.2123.3001.4430

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值