c# - Document编辑office的doc或者docx文件表格指定格子内容并保存-自定义工具

依赖使用

using Aspose.Words;
using Aspose.Words.Tables;

打开表格

 Document doc = new Document("文件路径");
//选中第一个表格
 Table table = (Table) doc.GetChild(NodeType.Table, 0, true);
调我做的工具修改第10行第1列的格子数据
 WordUtil.editCell(table, doc, 9, 0, lsm, 10, "宋体");

 

我做的工具

/// <summary>
        /// 修改表中指定格子的数据-每个元素为一行
        /// </summary>
        /// <param name="table">表对象</param>
        /// <param name="doc">文档对象</param>
        /// <param name="row">要修改行</param>
        /// <param name="cell">要修改列</param>
        /// <param name="lval">修改后的值集合</param>
        /// <param name="fontSize">字体大小</param>
        /// <param name="fontName">字体名称</param>
        public static void editCell(Table table, Document doc, int row, int cell, List<string> lval, int fontSize,string fontName)
        {
            Cell c = table.Rows[row].Cells[cell];
            c.RemoveAllChildren();
            if (null == lval ||lval.Count==0)
            {
                return;
            }
            foreach (var val in lval)
            {
                Paragraph p = new Paragraph(doc);
                p.AppendChild(new Run(doc, val));
                p.ParagraphFormat.Alignment = ParagraphAlignment.Center;
                p.ParagraphFormat.Style.Font.Size = fontSize;
                p.ParagraphFormat.Style.Font.Name = fontName;
                c.AppendChild(p);
            }
        }

修改后记得存储

 doc.Save("新文件路径", SaveFormat.Docx);

 

posted @ 2023-03-09 14:04  岑惜  阅读(177)  评论(0)    收藏  举报