C# Aspose.Words 用法

Aspose.Words.dll 

百度云盘下载地址(免费):

链接:https://pan.baidu.com/s/148L2M51wwT9q5YxOvip_jQ 
提取码:iwy7 

一:新建模板

‘《》’ 并不是自己手动打出来的,要通过文本域构建。

二:插入文本域。(插入--->文档部件--->域---->选择MergeFileID--->填写域名---->点击保存)

三:代码实现

AsposeWordHelper helper = new AsposeWordHelper();
string templatePath = "F://模板.docx";  //模板路径
helper.OpenTempelte(templatePath); //打开模板文件
string FileName = "F://a.pdf";//保存路径
string[] fieldNames = new string[] { "SupplierName", "ContractNo", "Name", "Time" };
object[] fieldValues = new object[] { "供应商名称01", "123456", "采购方", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") };
helper.Executefield(fieldNames, fieldValues);//域赋值
helper.SavePdf(FileName); //文件保存,保存为pdf

四:实现效果

这只是普通的占位赋值。

下面介绍一下集合遍历。

一:新建模板

也是通过添加文本域来实现模板构建。

TableStart、TableEnd 是固定的。

Table就是你的集合名;

代码实现:

AsposeWordHelper helper = new AsposeWordHelper();
string templatePath = "F://模板.docx";  //模板路径
helper.OpenTempelte(templatePath); //打开模板文件
string FileName = "F://a.pdf";//保存路径

string[] fieldNames = new string[] { "SupplierName", "ContractNo", "Name", "Time" };
object[] fieldValues = new object[] { "供应商名称01", "123456", "采购方", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") };
helper.Executefield(fieldNames, fieldValues);//域赋值
//构建测试集合
DataTable table = new DataTable("Table");
table.Columns.Add("Number");
table.Columns.Add("OrderNo");
table.Columns.Add("OrderTime");
table.Columns.Add("SendNo");
table.Columns.Add("Money");
table.Rows.Add(new object[] { 1, "123456789", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "666", "8888" });
table.Rows.Add(new object[] { 2, "1234567892", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "6662", "88882" });
table.Rows.Add(new object[] { 3, "1234567893", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "6663", "88883" });
helper.WriteTable(table); //集合赋值遍历
helper.SavePdf(FileName); //文件保存,保存为pdf

实现效果:

全部代码实现(简单封装,具体可以看官方文档):

官网文档: https://docs.aspose.com/display/wordsjava/Home
官网代码示例: https://github.com/aspose-words/Aspose.Words-for-.NET

    /// <summary>
    /// word文档操作辅助类
    /// </summary>
    public class AsposeWordHelper
    {
        /// <summary>
        /// Word
        /// </summary>
        private Document wordDoc;

        /// <summary>
        /// 基于模版新建Word文件
        /// </summary>
        /// <param name="path">模板路径</param>
        public void OpenTempelte(string path)
        {
            wordDoc = new Document(path);
        }

        /// <summary>
        /// 书签赋值用法
        /// </summary>
        /// <param name="LabelId">书签名</param>
        /// <param name="Content">内容</param>
        public void WriteBookMark(string LabelId, string Content)
        {
            if (wordDoc.Range.Bookmarks[LabelId] != null)
            {
                wordDoc.Range.Bookmarks[LabelId].Text = Content;
            }
        }

        /// <summary>
        /// 列表赋值用法
        /// </summary>
        /// <param name="dt"></param>
        public void WriteTable(DataTable dt)
        {
            wordDoc.MailMerge.ExecuteWithRegions(dt);
        }

        /// <summary>
        /// 文本域赋值用法
        /// </summary>
        /// <param name="fieldNames">key</param>
        /// <param name="fieldValues">value</param>
        public void Executefield(string[] fieldNames, object[] fieldValues)
        {
            wordDoc.MailMerge.Execute(fieldNames, fieldValues);
        }

        /// <summary>
        /// Pdf文件保存
        /// </summary>
        /// <param name="filename">文件路径+文件名</param>
        public void SavePdf(string filename)
        {
            wordDoc.Save(filename, SaveFormat.Pdf);
        }

        /// <summary>
        /// Doc文件保存
        /// </summary>
        /// <param name="filename">文件路径+文件名</param>
        public void SaveDoc(string filename)
        {
            wordDoc.Save(filename, SaveFormat.Doc);
        }

        /// <summary>
        /// 不可编辑受保护,需输入密码
        /// </summary>
        /// <param name="pwd">密码</param>
        public void NoEdit(string pwd)
        {
            wordDoc.Protect(ProtectionType.ReadOnly, pwd);
        }

        /// <summary>
        /// 只读
        /// </summary>
        public void ReadOnly()
        {
            wordDoc.Protect(ProtectionType.ReadOnly);
        }

        /// <summary>
        /// 通过流导出word文件
        /// </summary>
        /// <param name="stream">流</param>
        /// <param name="fileName">文件名</param>
        public static HttpResponseMessage ExportWord(Stream stream, string fileName)
        {
            var file = stream;
            fileName += DateTime.Now.ToString("yyyyMMddHHmmss");
            HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
            result.Content = new StreamContent(file);
            result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/msword");
            result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
            result.Content.Headers.ContentDisposition.FileName = fileName + ".doc";
            return result;
        }

        /// <summary>
        /// 通过流导出pdf文件
        /// </summary>
        /// <param name="stream">流</param>
        /// <param name="fileName">文件名</param>
        public static HttpResponseMessage ExportPdf(Stream stream, string fileName)
        {
            var file = stream;
            fileName += DateTime.Now.ToString("yyyyMMddHHmmss");
            HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
            result.Content = new StreamContent(file);
            result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
            result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
            result.Content.Headers.ContentDisposition.FileName = fileName + ".pdf";
            return result;
        }
    }

 

posted @ 2019-12-10 18:36  仅此而已。  阅读(9934)  评论(3编辑  收藏  举报