DocX在C#中的基本操作方法

    用了一个星期把园子里2016年中有关.net的文章都看了,有些只是大致的看了一下,在看的同时也在记录一些通用的方法。发现有很多对NPOI的文档,主要是操作Excl的方法,却很少有关文档类型的方法。

    在项目开发中,一般需要对文档进行操作,但是使用微软提供的插件,需要安装一些程序,并且如果使用wps类的文档软件就无法操作了,第三方插件DocX就可以很好的解决这些文档,结合官方提供的文档,稍作修改,总结如下的一些方法:

    1.创建一个具有超链接、图像和表的文档:

  

        /// <summary>
        /// 创建一个具有超链接、图像和表的文档。
        /// </summary>
        /// <param name="path">文档保存路径</param>
        /// <param name="imagePath">加载的图片路径</param>
        public static void HyperlinksImagesTables(string path, string imagePath)
        {
            // 创建一个文档
            using (var document = DocX.Create(path))
            {
                // 在文档中添加超链接。
                var link = document.AddHyperlink("link", new Uri("http://www.google.com"));
                // 在文档中添加一个表。
                var table = document.AddTable(2, 2);
                table.Design = TableDesign.ColorfulGridAccent2;
                table.Alignment = Alignment.center;
                table.Rows[0].Cells[0].Paragraphs[0].Append("1");
                table.Rows[0].Cells[1].Paragraphs[0].Append("2");
                table.Rows[1].Cells[0].Paragraphs[0].Append("3");
                table.Rows[1].Cells[1].Paragraphs[0].Append("4");
                var newRow = table.InsertRow(table.Rows[1]);
                newRow.ReplaceText("4", "5");
                // 将图像添加到文档中。    
                var image = document.AddImage(imagePath);
                //创建一个图片(一个自定义视图的图像)。
                var picture = image.CreatePicture();
                picture.Rotation = 10;
                picture.SetPictureShape(BasicShapes.cube);
                // 在文档中插入一个新段落。
                var title = document.InsertParagraph().Append("Test").FontSize(20).Font(new FontFamily("Comic Sans MS"));
                title.Alignment = Alignment.center;
                // 在文档中插入一个新段落。
                var p1 = document.InsertParagraph();
                // 附加内容到段落
                p1.AppendLine("This line contains a ").Append("bold").Bold().Append(" word.");
                p1.AppendLine("Here is a cool ").AppendHyperlink(link).Append(".");
                p1.AppendLine();
                p1.AppendLine("Check out this picture ").AppendPicture(picture).Append(" its funky don't you think?");
                p1.AppendLine();
                p1.AppendLine("Can you check this Table of figures for me?");
                p1.AppendLine();
                // 在第1段后插入表格。
                p1.InsertTableAfterSelf(table);
                // 在文档中插入一个新段落。
                Paragraph p2 = document.InsertParagraph();
                // 附加内容到段落。
                p2.AppendLine("Is it correct?");
                // 保存当前文档
                document.Save();
            }
        }

2.设置文档的标题和页脚:

        /// <summary>
        /// 设置文档的标题和页脚
        /// </summary>
        /// <param name="path">文档的路径</param>
        public static bool HeadersAndFooters(string path)
        {
            try
            {
                // 创建新文档
                using (var document = DocX.Create(path))
                {
                    // 这个文档添加页眉和页脚。
                    document.AddHeaders();
                    document.AddFooters();
                    // 强制第一个页面有一个不同的头和脚。
                    document.DifferentFirstPage = true;
                    // 奇偶页页眉页脚不同
                    document.DifferentOddAndEvenPages = true;
                    // 获取本文档的第一个、奇数和甚至是头文件。
                    Header headerFirst = document.Headers.first;
                    Header headerOdd = document.Headers.odd;
                    Header headerEven = document.Headers.even;
                    // 获取此文档的第一个、奇数和甚至脚注。
                    Footer footerFirst = document.Footers.first;
                    Footer footerOdd = document.Footers.odd;
                    Footer footerEven = document.Footers.even;
                    // 将一段插入到第一个头。
                    Paragraph p0 = headerFirst.InsertParagraph();
                    p0.Append("Hello First Header.").Bold();
                    // 在奇数头中插入一个段落。
                    Paragraph p1 = headerOdd.InsertParagraph();
                    p1.Append("Hello Odd Header.").Bold();
                    // 插入一个段落到偶数头中。
                    Paragraph p2 = headerEven.InsertParagraph();
                    p2.Append("Hello Even Header.").Bold();
                    // 将一段插入到第一个脚注中。
                    Paragraph p3 = footerFirst.InsertParagraph();
                    p3.Append("Hello First Footer.").Bold();
                    // 在奇数脚注中插入一个段落。
                    Paragraph p4 = footerOdd.InsertParagraph();
                    p4.Append("Hello Odd Footer.").Bold();
                    // 插入一个段落到偶数头中。
                    Paragraph p5 = footerEven.InsertParagraph();
                    p5.Append("Hello Even Footer.").Bold();
                    // 在文档中插入一个段落。
                    Paragraph p6 = document.InsertParagraph();
                    p6.AppendLine("Hello First page.");
                    // 创建一个第二个页面,显示第一个页面有自己的头和脚。
                    p6.InsertPageBreakAfterSelf();
                    // 在页面中断后插入一段。
                    Paragraph p7 = document.InsertParagraph();
                    p7.AppendLine("Hello Second page.");
                    // 创建三分之一页面显示,奇偶页不同的页眉和页脚。
                    p7.InsertPageBreakAfterSelf();
                    // 在页面中断后插入一段。
                    Paragraph p8 = document.InsertParagraph();
                    p8.AppendLine("Hello Third page.");
                    // 将属性保存入文档
                    document.Save();
                    return true;
                }

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            //从内存中释放此文档。
        }

 

转载于:https://www.cnblogs.com/pengze0902/p/5962594.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#处理Word文档的表格,可以使用Microsoft.Office.Interop.Word命名空间提供的API进行操作。以下是一个示例代码,可以在Word文档的指定位置创建一个表格,并设置表格的行列数、表头和内容: ```csharp using System; using Microsoft.Office.Interop.Word; namespace WordTableDemo { class Program { static void Main(string[] args) { // 创建Word文档对象 Application wordApp = new Application(); Document wordDoc = wordApp.Documents.Add(); // 在指定位置插入表格 Range range = wordDoc.Range(0, 0); Table table = wordDoc.Tables.Add(range, 4, 3); // 设置表头 table.Cell(1, 1).Range.Text = "姓名"; table.Cell(1, 2).Range.Text = "年龄"; table.Cell(1, 3).Range.Text = "性别"; // 设置表格内容 table.Cell(2, 1).Range.Text = "张三"; table.Cell(2, 2).Range.Text = "18"; table.Cell(2, 3).Range.Text = "男"; table.Cell(3, 1).Range.Text = "李四"; table.Cell(3, 2).Range.Text = "20"; table.Cell(3, 3).Range.Text = "女"; table.Cell(4, 1).Range.Text = "王五"; table.Cell(4, 2).Range.Text = "22"; table.Cell(4, 3).Range.Text = "男"; // 保存Word文档 wordDoc.SaveAs2(@"D:\test.docx"); wordDoc.Close(); // 关闭Word应用程序 wordApp.Quit(); } } } ``` 在上述代码,我们首先创建了一个Word文档对象,然后通过`Range`对象在指定位置插入了一个4行3列的表格。接着,我们通过`table.Cell(row, column)`方法获取表格某个单元格,并设置了表头和表格内容。最后,我们将Word文档保存到本地,并关闭Word应用程序。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值