栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

【Aspose for .NET】Aspose.word操作表格:在表格中插入行

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Aspose for .NET】Aspose.word操作表格:在表格中插入行

Aspose.word 在表格中插入行

参数说明:table :当前 document中的table对象 获取方式 如下:

Table labTable = (Aspose.Words.Tables.Table)doc.GetChild(NodeType.Table, 0, true);
        /// 
        /// 在表格中插入行
        /// 
        /// 表格对象
        /// 插入表格的单元格对齐方式
        /// 字体名称
        /// 从当前获取表格的哪一行开始插入
        /// 要插入的list
        /// 当前文档的document
        /// 
        public static bool InsertRowToTable(Table table, ParagraphAlignment cellAlignment, string FontName, int startRow, IList list, Document doc)
        {
            try
            {
                int positionRow = startRow + 1;
                DataTable dataTable = ListToDataTable(list);
                for (int i = 0; i < dataTable.Rows.Count; i++)
                {
                    Row insertRow = (Row)table.Rows[startRow].Clone(true);
                    foreach (Cell cell in insertRow.Cells)
                    {
                        cell.RemoveAllChildren();
                    }
                    for (int j = 0; j < dataTable.Columns.Count; j++)
                    {

                        var cl = insertRow.Cells[j];
                        Paragraph p = new Paragraph(doc);
                        p.ParagraphFormat.Alignment = cellAlignment;
                        p.ParagraphFormat.Style.Font.Name = FontName;
                        p.AppendChild(new Run(doc, dataTable.Rows[i][j].ToString()));
                        cl.AppendChild(p);
                    }
                    table.Rows.Insert(positionRow, insertRow);
                    positionRow++;
                }
                return true;
            }
            catch (Exception e)
            {
                throw e;
            }
        }


        /// 
        /// C# List转换成DataTable 列顺序根据创建Model顺序
        /// 
        /// 
        /// 
        public static DataTable ListToDataTable(IList list)
        {
            DataTable result = new DataTable();
            if (list.Count > 0)
            {
                PropertyInfo[] propertys = list[0].GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    //获取类型
                    Type colType = pi.PropertyType;
                    //当类型为Nullable<>时
                    if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
                    {
                        colType = colType.GetGenericArguments()[0];
                    }
                    result.Columns.Add(pi.Name, colType);
                }
                for (int i = 0; i < list.Count; i++)
                {
                    ArrayList tempList = new ArrayList();
                    foreach (PropertyInfo pi in propertys)
                    {
                        object obj = pi.GetValue(list[i], null);
                        tempList.Add(obj);
                    }
                    object[] array = tempList.ToArray();
                    result.LoadDataRow(array, true);
                }
            }
            return result;
        }

 

 

Aspose.word根据datatable创建表格在表格末尾添加行
        /// 
        /// 根据datatable创建表格(末尾添加)
        /// 
        /// 当前doc中的table
        /// 数据list
        /// 文档doc
        /// 
        public static bool CreateTable(Table table, IList list, Document doc)
        {
            try
            {
                DataTable dataTable = ListToDataTable(list);
                for (int i = 0; i < dataTable.Rows.Count; i++)
                {
                    //Row LastRow = (Row)table.Rows[12].Clone(true);
                    Row LastRow = (Row)table.LastRow.Clone(true);
                    foreach (Cell cell in LastRow.Cells)
                    {
                        cell.RemoveAllChildren();
                    }
                    for (int j = 0; j < dataTable.Columns.Count; j++)
                    {

                        var cl = LastRow.Cells[j];
                        Paragraph p = new Paragraph(doc);
                        p.AppendChild(new Run(doc, dataTable.Rows[i][j].ToString()));
                        cl.AppendChild(p);
                    }
                    table.AppendChild(LastRow);
                }
                return true;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/909015.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号