Aspose.Words.dll 插入模板表格,使用的是邮件合并MailMerge功能,数据源是DataTable或list对象,实例

发布于:2024-08-11 ⋅ 阅读:(36) ⋅ 点赞:(0)

本实例中的实例功能有: 

1、 Aspose.Words.dll 插入模板指定域替换为文字或html标签,见1

2、Aspose.Words.dll 插入模板表格,使用的是邮件合并MailMerge功能,数据源是DataTable或List对象(将list转换成DataTable),见1和2

3、word转换Pdf文件,见1

4、将多个word输出文档合并成一个文档 ,见1

5、请求API 接口查询JSON数据 ,见3

6、翻页查询,T-SQL方法见2

1、准备工作:word模板,插入域

域插入方法:

1.1>WPS添加域,插入>>文档部件>>域>>邮件合并,见图一

1.2> Office Word添加域,插入>>文档部件>>域>>MergeField,见图二

2、效果图

3、Aspose.Words封装类:

using Aspose.Words;
using Aspose.Words.MailMerging;
using Aspose.Words.Saving;
using DS_SCZX.Common;
using DS_SCZX.Entities;
using DS_SCZX.Entities.GetObj;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;

namespace DS_SCZX.BLL
{
    /// <summary>
    /// AsposeWordHelper插件的业务逻辑层
    /// </summary>
    public class AsposeWordHelper
    {
        readonly string key = "this your key";

        public AsposeWordHelper()
        {
            #region  许可证
            Aspose.Words.License license = new Aspose.Words.License();
            //签名
            license.SetLicense(new MemoryStream(Convert.FromBase64String(key)));
            #endregion

        }
        /// <summary>
        /// Word转PDF
        /// Author:Bingo
        /// Date:2024-07-31
        /// </summary>
        /// <param name="wordPath">word绝对路径</param>
        /// <param name="PDFPath">pdf要保存的绝对路径</param>
        /// <returns></returns>
        public MessageInfo WordToPDF(string wordPath, string PDFPath)
        {
            var result = new MessageInfo();
            result.success = false;
            result.msg = "";
            if (string.IsNullOrEmpty(wordPath))
            {
                result.msg = "word路径不能为空";
                return result;
            }
            File.Exists(wordPath);
            if (!File.Exists(wordPath))
            {
                result.msg = "word文件不存在";
                return result;
            }

            //不带扩展名称的文件名称
            string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(wordPath);
            新合格证保存路径
            //string filePathTem = $@"C:\Codes\DS_SCZX\DS_SCZX\WordPDFiles\{fileNameWithoutExtension}.pdf";


            try
            {
                #region  实现Word转PDF
                Aspose.Words.Document doc = new Aspose.Words.Document(wordPath);
                PdfSaveOptions options = new PdfSaveOptions();
                options.Compliance = PdfCompliance.Pdf17;
                // Convert Word to PDF
                doc.Save(PDFPath, options);
                #endregion

                result.msg = $@"/WordPDFiles/" + fileNameWithoutExtension + ".pdf";
                result.success = true;

            }
            catch (Exception ex)
            {
                result.msg = ex.Message;
            }
            return result;
        }



        /// <summary>
        /// 合并word文档
        /// </summary>
        /// <param name="wordPaths">文档路径数组</param>
        /// <param name="outputPath">合并后的文件保存路径</param>
        public bool CombineWord(List<string> ListDocPath, string outputPath)
        {
            bool b = false;
            try
            {
                Document mergedDocument = new Document(ListDocPath.FirstOrDefault());
                // 遍历要合并的文档路径
                for (int i = 1; i < ListDocPath.Count; i++)
                {
                    Document doc = new Document(ListDocPath[i]);
                    mergedDocument.AppendDocument(doc, ImportFormatMode.UseDestinationStyles);
                }
                //保存文档
                mergedDocument.Save(outputPath);

            }
            catch (Exception)
            {
                b = false;
                throw;
            }
            return b;
            测试合并文档
            //Document doc = new Document(wordItemUrl_out); 
            //Document doc2 = new Document(wordItemUrl_out2);
            合并
            //doc.AppendDocument(doc2, ImportFormatMode.UseDestinationStyles);
            //string wordItemUrl_outALL = Server.MapPath($@"~/DownFiles/试验计划模板20240704/merged_document.docx");
            保存文档
            //doc.Save(wordItemUrl_outALL);
        }
        /// <summary>
        /// 将dataTable数据插入word文档
        /// Author:Bingo
        /// Date:2024-07-31
        /// </summary>
        /// <param name="wordPath">word模板文件路径</param>
        /// <param name="wordItemUrl2">word输出路径</param>
        /// <param name="dt2">数据源</param>
        /// <returns></returns>
        public MessageInfo InsertTable(string wordPath, string wordItemUrl2, DataTable dt2)
        {
            var result = new MessageInfo();
            result.success = false;
            result.msg = "";
            if (string.IsNullOrEmpty(wordPath))
            {
                result.msg = "word模板路径不能为空";
                return result;
            }
            File.Exists(wordPath);
            if (!File.Exists(wordPath))
            {
                result.msg = "word模板文件不存在";
                return result;
            }

            try
            {
                #region  插入表数据
                Aspose.Words.Document doc = new Aspose.Words.Document(wordPath);
                Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);
                builder.MoveToMergeField("FirstTitle");
                builder.Write("01烧结-test");
                builder.MoveToMergeField("SecondTitle");
                builder.Wr