C#通过书签对Word文档进行操作

一、前置准备

1、 以WPS为例插入书签

2、需要使用到的类库:Aspose.Words

官方网址:Document Processing API for .NET: C#, F#, VB.NET, etc.

二、通过书签将数据插入

/// <summary>
    /// 通过书签插入数据
    /// </summary>
    /// <param name="templateFileName">需要复制创建的模板文件名字</param>
    /// <param name="exportFileName">插入数据之后输出的文件名字</param>
    /// <param name="insertContentList">通过书签插入的数据的列表</param>
    /// <param name="bookMarkName">书签名字</param>
    void InsertDataByBookMark(string templateFileName, string exportFileName, List<string> insertContentList,
        string bookMarkName)
    {
        //创建一个新的Word文档
        string exportPath =
            Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + exportFileName; //输出文件的位置
        string docSource =
            string.Format(Application.dataPath + "/../Resources"+templateFileName, Application.dataPath); //输出模板的位置,这里的"/../Resources"指的是和Assets文件夹同级的Resources文件夹
        File.Copy(docSource, exportPath, true);
        Document doc = new Document(exportPath);
        DocumentBuilder builder = new DocumentBuilder(doc);

        //向文档中插入数据
        for (int i = 0; i < insertContentList.Count; i++)
        {
            builder.MoveToBookmark(bookMarkName + i);
            builder.Write(insertContentList[i]);
        }

        //导出新创建的Word文档
        doc.Save(exportPath);

        //打开Word文档
        System.Diagnostics.Process newProcess = new System.Diagnostics.Process();
        System.Diagnostics.ProcessStartInfo myProcessStartInfo =
            new System.Diagnostics.ProcessStartInfo(exportPath, null);
        newProcess.StartInfo = myProcessStartInfo;
        newProcess.Start();
    }

三、通过书签将图片插入

 /// <summary>
    /// 通过书签向Word文档中插入图片
    /// </summary>
    /// <param name="templateFileName">需要复制创建的模板文件名字</param>
    /// <param name="exportFileName">插入数据之后输出的文件名字</param>
    /// <param name="bookMarkName">书签名字</param>
    /// <param name="needInsertSpriteName">需要插入的图片的名字(需要带图片格式)</param>
    /// <returns></returns>
    void InsertImageByBookMark(string templateFileName, string exportFileName,string bookMarkName,string needInsertSpriteName)
    {
        //创建一个新的Word文档
        string exportPath =
            Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + exportFileName; //输出文件的位置
        string docSource =
            string.Format(Application.dataPath + "/../Resources" + templateFileName,
                Application.dataPath); //输出模板的位置,这里的"/../Resources"指的是和Assets文件夹同级的Resources文件夹
        File.Copy(docSource, exportPath, true);
        Document doc = new Document(exportPath);
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\" +
                        needInsertSpriteName)) //判断文件是否存在
        {
            builder.MoveToBookmark(bookMarkName);
            builder.InsertImage(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\" +
                                needInsertSpriteName);
        }
        
        //导出新创建的Word文档
        doc.Save(docDest);
    }

四、通过书签将表格插入

 /// <summary>
    /// 通过书签插入表格
    /// </summary>
    /// <param name="templateWordName">需要复制创建的模板文件名字</param>
    /// <param name="exportWordName">插入数据之后输出的文件名字</param>
    /// <param name="tableBookMarkList">表格书签名集合</param>
    /// <param name="tableDataList">表格集合</param>
    protected void InsertTableByBookMark(string templateWordName, string exportWordName, List<string> tableBookMarkList,
        List<List<List<string>>>
            tableDataList)
    {
        //创建Word文档
        string docTemplatePath =
            string.Format(Application.dataPath + "/../Resources" + templateWordName, Application.dataPath);
        string docExportPath = Environment
            .GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + exportWordName;
        File.Copy(docTemplatePath, docExportPath, true);
        Document doc = new Document(docExportPath);
        DocumentBuilder builder = new DocumentBuilder(doc);

        //将表格中的数据插入到指定的书签内(这里默认是有多个表格,所以使用了三层List进行嵌套)
        for (int i = 0; i < tableDataList.Count; i++)
        {
            builder.MoveToBookmark(tableBookMarkList[i]);
            builder.StartTable();
            for (int row = 0; row < tableDataList[i].Count; row++)
            {
                for (int cel = 0; cel < tableDataList[i][row].Count; cel++)
                {
                    builder.InsertCell();
                    builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;//设置插入数据的格式是水平居中
                    builder.Write(tableDataList[i][row][cel]);
                }

                builder.EndRow();
            }

            builder.EndTable();
        }

        //保存文件
        doc.Save(docExportPath);
    }

五、提示

1、在开发的时候不止需要引用Aspose.Words,同时还需要引用System.IO;

2、演示代码中的输出位置Word文档位置都是默认是在桌面上的,如果需要输出在别的路径需要修改

3、转载请标明出处!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值