Aspose.Words对于Word的操作

对于word操作一般是对已有word模板的操作,直接新建的不考虑,网上教程很多,自己看吧
一般有以下几种办法(忘了具体几种了,一般情况下以下就够了)
1、通过书签替换
顾名思义,就是先定义一个书签,然后在书签的位置填进去相应的数据或图片,具体操作
1 >先在word上插入->书签->定义书签名
2 >在程序中使用

Document doc = new Document(tmppath); //载入模板
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);
builder.MoveToBookmark("zy");//跳转到书签名是zy的位置
builder.Write("书签位置要替换的文字");

图片还有一个根据位置定位到某个位置然后粘贴,实例如下

builder.InsertImage(img, RelativeHorizontalPosition.Page, left, RelativeVerticalPosition.TopMargin, 0, width, height, WrapType.None);

参数具体含义有很明了,就不说了。
2、通过表格单元格替换
一般word模板里边基本就是表格,所以对表格的操作很重要
先找到相应的表格

Document doc = new Document(tmppath); //载入模板
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);
NodeCollection allTables = doc.GetChildNodes(NodeType.Table, true);
Table table = allTables[0] as Aspose.Words.Tables.Table;//拿到第一个表格


然后对表格进行操作,但是默认的表格的单元格是只可读的,于是我想出一个办法,就是先取到单元格A存储到一个新建的单元格对象单元格B里面,然后修改单元格B,最后删除单元格A并且把单元格B添加到单元格A中,这样就完美的替换了单元格了,而且原来单元格的宽高都不用在调整了,于是我把它封装成了一个方法示例如下
                

                /// <summary>
                /// 修改表中的数据
                /// </summary>
                /// <param name="table">表名</param>
                /// <param name="doc">文档</param>
                /// <param name="row">要修改行</param>
                /// <param name="cell">要修改列</param>
                /// <param name="value">修改后的值</param>
                private static Table EditCell(Table table, Document doc, int row, int cell, string value)
                {
                        Cell c = table.Rows[row].Cells[cell];
                        Paragraph p = new Paragraph(doc);
                        p.AppendChild(new Run(doc, value));

                        p.ParagraphFormat.Style.Font.Size = 10;
                        p.ParagraphFormat.Style.Font.Name = "华文楷体";
                        c.FirstParagraph.Remove();
                        c.AppendChild(p);
                        table.Rows[row].Cells[cell].Remove();
                        table.Rows[row].Cells.Insert(cell, c);
                        return table;
}

3 >就是删除原来的单元格然后再添加一个新建的单元格,然后自定义属性(宽高等)
这个不推荐,只是原来没有想好2方法的替代品,是老版本,但是还是显示一下吧,纪念自己的努力
方法如下
        

         private static Aspose.Words.Tables.Cell CreateCell(string value, Document doc, double cellwidth)
                {
                        Aspose.Words.Tables.Cell c1 = new Aspose.Words.Tables.Cell(doc);
                        c1.CellFormat.Width = cellwidth;
                        c1.CellFormat.Borders.LineStyle = LineStyle.Single;
                        //c1.CellFormat.WrapText = false;
                        Aspose.Words.Paragraph p = new Paragraph(doc);
                        p.AppendChild(new Run(doc, value));
                        //Table table = new Table(doc);
                        //p.AppendChild(table);
                        p.ParagraphFormat.Style.Font.Size = 10;
                        p.ParagraphFormat.Style.Font.Name = "华文楷体";
                        c1.AppendChild(p);
                        return c1;

                }


4 >添加数据到单元格,只适用于表格,很是推荐,很好用,具体就是直接向某个表格的某一行的某一列添加值,示例如下:

builder.MoveToCell(0, 31, 1, 0);//先跳转到第0个表格,第31行,第1列
builder.Write("要写入的数据");

注意:行和列都是从0开始的,不是从1开始的
5 >通过替换域名的方法
和书签的方法相似,但是。。。。我不会,自己百度吧。。。哈哈哈哈哈

其他:我还准备做一个小例子,过会传上来

原文地址:https://www.cnblogs.com/itljf/p/5859445.html