Spire.Doc for .NET(1)

简介:
   Spire.Doc for .NET 是一个专门为开发人员设计的能在任意的 .NET 平台上快速和高质量的创建、读取、写入、转换和打印 word 文档文件的 .NET 组件。作为一个独立的 .NET 组件,Spire.Doc for .NET 不必在电脑上安装微软 Word 。但是,它却可以将微软 Word 文档创建功能并入任何开发人员的 .NET 应用程序中。

主要优点:

1、不需要安装 Microsoft Office

相比较于通过 Microsoft Office 自动转化格式时的不稳定、缓慢和不可扩展,Spire.Doc for .NET 在抛开 Microsoft Office 的束缚后显得相对独立,而且其远胜于 Microsoft Office 的高效性、稳定性和可扩展性也让开发人员不再束手束脚 。

2、能高质量的转化文件

通过 Spire.Doc for .NET,开发人员可以将 Word 保存到文本流、保存为 Web 响应,也可以将 Word 转化为 XML、RTF、EMF、TXT、XPS、EPUB、HTML、SVG,反过来转化也可以 。同时 Spire.Doc for .NET 也支持将 Word 转化为 PDF 、HTML 转化为图片 。

3、丰富的Word文档功能支持

Spire.Doc for .NET 的一个最常用的用法就是动态创建Word文档 。几乎所有的 Word 文档元素都得到了  Spire.Doc for .NET 的支持,包括页面,页眉、页脚、脚注部分,段落,列表,表格,文本字段,超链接,书签,评论,图片,风格,背景设置,打印功能,文档设置和保护 。此外,图形对象包括形状、文本框、图片、OLE对象和控件也得到支持 。

4、可简单的处理现有 Word 文档

Spire.Doc for .NET 可帮助开发人员简单的处理 Word 文档,它支持搜索和替换功能、对准、分页符、填补领域、文件连接、文件拷贝、打印、复杂和深层嵌套的邮件合并等等 。

使用:

通过NuGet获取 Install-Package Spire.Office

里面包含了Doc,XLS,PDF,EMail、、、等

使用场景,根据模板替换模板里面的数据

1:基类

    /// <summary>
    /// 导出基类
    /// </summary>
    public class ExportBase
    {
        /// <summary>
        /// 根据属性获取值
        /// </summary>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public string GetValue(string propertyName)
        {
            string value = "";
            try
            {
                if (!string.IsNullOrEmpty(propertyName))
                {
                    var objectValue = this.GetType().GetProperty(propertyName).GetValue(this, null);
                    if (objectValue != null)
                    {
                        value = objectValue.ToString();
                    }
                }
            }
            catch (Exception)
            {
            }
            return value;
        }
        /// <summary>
        /// 根据属性获取描述值
        /// </summary>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public string GetDescription(string propertyName)
        {
            try
            {
                PropertyInfo item = this.GetType().GetProperty(propertyName);
                string des = ((DescriptionAttribute)Attribute.GetCustomAttribute(item, typeof(DescriptionAttribute))).Description;// 属性值
                return des;
            }
            catch (Exception)
            {
                return "";
            }
        }
    }

2:实体类

    public class PurchaseContract : ExportBase
    {
        /// <summary>
        /// 甲方
        /// </summary>
        public string PartyA { get; set; }
        /// <summary>
        /// 乙方
        /// </summary>
        public string PartyB { get; set; }
        /// <summary>
        /// 合同编号
        /// </summary>
        public string ContractNo { get; set; }
        /// <summary>
        /// 签订地点
        /// </summary>
        public string ContractedAddress { get; set; }
        /// <summary>
        /// 签订时间
        /// </summary>
        public string ContractedTime { get; set; }
        /// <summary>
        /// 甲方地址
        /// </summary>
        public string AddressA { get; set; }
        /// <summary>
        /// 乙方地址
        /// </summary>
        public string AddressB { get; set; }
        /// <summary>
        /// 甲方联系电话
        /// </summary>
        public string PartyAPhone { get; set; }
        /// <summary>
        /// 乙方联系电话
        /// </summary>
        public string PartyBPhone { get; set; }
        /// <summary>
        /// 甲方开户行
        /// </summary>
        public string PartyABankName { get; set; }
        /// <summary>
        /// 乙方开户行
        /// </summary>
        public string PartyBBankName { get; set; }
        /// <summary>
        /// 甲方开户行账户
        /// </summary>
        public string PartyABankAccount { get; set; }
        /// <summary>
        /// 乙方开户行账户
        /// </summary>
        public string PartyBBankAccount { get; set; }
    }

    public class PurchaseContractDetaile : ExportBase
    {
        /// <summary>
        /// 采购单号
        /// </summary>
        public string 采购单号 { get; set; }
        /// <summary>
        /// 产品名称
        /// </summary>
        public string 品名 { get; set; }
        /// <summary>
        /// SKU
        /// </summary>
        public string SKU { get; set; }
        /// <summary>
        /// 产品描述
        /// </summary>
        public string 产品描述 { get; set; }
        /// <summary>
        /// 单位
        /// </summary>
        public string 单位 { get; set; }
        /// <summary>
        /// 数量
        /// </summary>
        public int 数量 { get; set; }
        /// <summary>
        /// 单价
        /// </summary>
        public decimal? 单价 { get; set; }
        /// <summary>
        /// 采购成本
        /// </summary>
        public decimal? 采购成本 { get; set; }
        /// <summary>
        /// 金额
        /// </summary>
        public decimal? 金额 { get; set; }
        /// <summary>
        /// 税额
        /// </summary>
        public decimal? 税金 { get; set; }
        /// <summary>
        /// 价税合计
        /// </summary>
        public decimal? 价税合计 { get; set; }

        public void SetTaxValue(bool IsTax)
        {
            try
            {
                if (单价.HasValue)
                {
                    单价 = Math.Round(单价.Value, 2);
                }
                if (金额.HasValue)
                {
                    金额 = Math.Round(金额.Value, 2);
                }
            }
            catch (Exception)
            {

            }
        }
    }

3:替换模板

    public static class SpireWord
    {
        static Document document = new Document();
        /// <summary>
        /// 获取模板
        /// </summary>
        public static void GetDocument()
        {
            try
            {
                string wordTemplatePath = AppDomain.CurrentDomain.BaseDirectory + "\\TemplateExport\\ContractTemplate.docx";
                document.LoadFromFile(wordTemplatePath);
            }
            catch (Exception ex)
            {
                string errorMsg = ex.Message;
                throw;
            }
        }
        public static void CreateNewWord()
        {
            document.SaveToFile("Replace.docx", FileFormat.Docx);
        }
        /// <summary>
        /// 替换模板
        /// </summary>
        /// <param name="purchaseContract"></param>
        public static void ReplaseTemplateWord(PurchaseContract purchaseContract)
        {
            try
            {
                foreach (System.Reflection.PropertyInfo p in purchaseContract.GetType().GetProperties())
                {
                    Console.WriteLine("Name:{0} Value:{1}", p.Name, p.GetValue(purchaseContract));
                    document.Replace(p.Name, p.GetValue(purchaseContract).ToString(), false, true);
                }
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
                throw;
            }
        }
        public static void AddTable(List<PurchaseContractDetaile> purchaseOrderDetaileList)
        {
            try
            {
                Table table = document.Sections[0].Tables[0] as Table;
                int rowNum = purchaseOrderDetaileList.Count;
                DataTable dataTable = new DataTable();
                int i = 1;
                foreach (var item in purchaseOrderDetaileList)
                {
                    table.AddRow(10);
                    if (i < table.Rows.Count)
                    {
                        table[i, 0].AddParagraph().AppendText(item.采购单号);
                        table[i, 1].AddParagraph().AppendText(item.品名);
                        table[i, 2].AddParagraph().AppendText(item.SKU);
                        table[i, 3].AddParagraph().AppendText(item.产品描述);
                        table[i, 4].AddParagraph().AppendText(item.单位);
                        table[i, 5].AddParagraph().AppendText(item.数量.ToString());
                        table[i, 6].AddParagraph().AppendText(item.单价.ToString());
                        table[i, 7].AddParagraph().AppendText(item.采购成本.ToString());
                        table[i, 8].AddParagraph().AppendText(item.税金.ToString());
                        table[i, 9].AddParagraph().AppendText(item.价税合计.ToString());
                    }
                    i++;
                }
                table.AddRow(10);
                table[i, 0].AddParagraph().AppendText("合计(大写):" + Common.ConvertToChineseRMB(2386.20M));
                table.ApplyHorizontalMerge(i, 0, 8);
                table[i, 9].AddParagraph().AppendText("2386.20");
            }
            catch (Exception ex)
            {
                string errorMsg = ex.Message;
                throw;
            }
        }

    }

 4:模板文件

转载于:https://www.cnblogs.com/123qq/p/9480532.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值