• 关于Aspose对于Word操作的一些扩展及思考


         

    Aspose.word

    Aspose.Words是一款先进的类库,通过它可以直接在各个应用程序中执行各种文档处理任务。Aspose.Words支持DOC,OOXML,RTF,HTML,OpenDocument, PDF, XPS, EPUB和其他格式。使用Aspose.Words,您可以生成,更改,转换,渲染和打印文档而不使用Microsoft Word。
    上面一句话换而言之是他解决了IIS针对Microsoft Word Application的各种权限不足问题。当然,这里主要不是和他家哈牛B说Aspose怎么用,在这里主要和大家讨论的是Aspose我们怎样对他进行一些扩展让他更适合你的项目。
          1、将一些对象的动态方法进行静态化
              这里说的动态方法是什么呢,比方说打开构建Document对象,DocumentBuilder对象等,在Aspose集成时可能他们的编程习惯与我们团队里的不一样,那么可以通过一些方式让整个编码风格统一起来。 
        比如:        
            /// <summary>
            /// 使用操作者
            /// </summary>
            /// <param name="oWordApplic"></param>
            /// <param name="oDoc"></param>
        public static DocumentBuilder Builder(this  Aspose.Words.Document oDoc)  
            {  
                return new DocumentBuilder(oDoc);  
    }

      这样程序中调用写法就改为: DocumentBuilder builder=Doc.Builder();

         2、将一些经常出现或者反应习惯进行集成,静态化处理。

            比如说我们这里面插入文字,如果按照Aspose提供的,那么我们可能需要做的是:     

                oWordApplic.Bold = conBold;
                oWordApplic.Font.Size = conSize;            
                oWordApplic.ParagraphFormat.Alignment = ParagraphAlignment.Left;            
                oWordApplic.Write(strText);  
    

      但是我们可以看到,一般操作不需要黑体,字体大小也比较固定,基本上都是靠左对齐,那么我们可以进行一些集成,然后我们按照变动优先级进行默认参数排序(通常认为改动字体可能行最大、其次是是否黑体,对齐方式变动可能性最小)  

            /// <summary>  
            /// 添加内容  
            /// </summary>  
            /// <param name="strText"></param>  
            public static void WriteText(this DocumentBuilder oWordApplic, string strText, double conSize = 24, bool conBold = false
                     , ParagraphAlignment conAlign = ParagraphAlignment.Left)  
            {  
                oWordApplic.Bold = conBold;  
                oWordApplic.Font.Size = conSize;  
                switch (conAlign)  
                {
                    case ParagraphAlignment.Left:  
                        oWordApplic.ParagraphFormat.Alignment = ParagraphAlignment.Left;  
                        break;
                    case ParagraphAlignment.Center:  
                        oWordApplic.ParagraphFormat.Alignment = ParagraphAlignment.Center;  
                        break;
                    case ParagraphAlignment.Right:  
                        oWordApplic.ParagraphFormat.Alignment = ParagraphAlignment.Right;  
                        break;  
                    default:  
                        oWordApplic.ParagraphFormat.Alignment = ParagraphAlignment.Left;  
                        break;  
                }  
                oWordApplic.Write(strText); 
            }  
     
    

      那么我们在插入文字时oWordApplic.WriteText(strText);即可~

          3、善于将其他的类库与操作类库集成

               这里有一个例子,我们经常要将个DataSet里面的数据插入到word的行里面去,我们可以根据DataSet的数据结构特点与Word的表格结构特点类似

                我们第一步做的是做一个DataRow,Dictionary,Aspose.Words.Node相互映射并且进行区域行的文字替换。     

            // 根据字典替换
            public static void ReplaceText(this Aspose.Words.Node node, Dictionary<string,string> _ReplaceDic,System.Data.DataRow dr)
            {
                //System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(oleText);  
                foreach (System.Data.DataColumn dc in dr.Table.Columns)
                {
                    if (_ReplaceDic.Keys.Contains(dc.ColumnName))
                    {
                        node.Range.Replace(_ReplaceDic[dc.ColumnName], dr[dc].ToString(), false, false);
                    }
                    else
                    {
                        node.Range.Replace(dc.ColumnName, dr[dc].ToString(), false, false);
                    }
                } 
            }
    

        然后再根据DataTable对word表格进行行控制:   

            //表格增加新行
            public static Row InsertTableRow(this Table table)
            {
    
                // Retrieve the first table in the document. 
                // Clone the last row in the table.
                Row clonedRow = (Row)table.LastRow.Clone(true);
              
                // Remove all content from the cloned row's cells. This makes the row ready for
                // new content to be inserted into.
                foreach (Cell cell in clonedRow.Cells)
                    cell.RemoveAllChildren(); 
                // Add the row to the end of the table.
                table.AppendChild(clonedRow);
                return clonedRow; 
            }
    

      这样就会完全遵循了我们所谓的Word替换规则,并且还不会影响word行的样式甚至一些自定义的文字。

           4、尝试将代码、类库进行模型化与具现化。

            这里讲述的不是代码的一些写法了,而是对待类库的一种理解方式。

            Aspose对象首先我们要看他的数据用例

             1、Aspose对象关系与Word对象关系肯定是一致的。

             2、理解word的文档节点与Aspose中的Node节点的对应关系(文字、图形、书签、表格等)

             3、基本上所有的Aspose操作都能找到对应的Word操作(操作一致性,从操作word方向来理解Aspose的)

             言而总之、总而言之,反正就这样了

            

       

         

  • 相关阅读:
    Python3.7.1学习(三)求两个list的差集、并集与交集
    Python3.7.1学习(二)使用schedule模块定时执行任务
    Python3.7.1学习(一):redis的连接和简单使用
    requests保存图片
    requests模拟登陆的三种方式
    requests模块使用代理
    requests模块发送带headers的Get请求和带参数的请求
    python3.7.1安装Scrapy爬虫框架
    python-生成器
    python-迭代器
  • 原文地址:https://www.cnblogs.com/webda/p/4544126.html
Copyright © 2020-2023  润新知