zoukankan      html  css  js  c++  java
  • 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);
                }
                //从内存中释放此文档。
            }
  • 相关阅读:
    关于调试 SharePoint 2010 中的爬网问题的疑难解答提示
    JS 获取 本周、本月、本季度、本年、上月、上周、上季度、去年
    jqGrid动态列
    【原创】博客“缘”
    讀取內部網站8/22
    開啟VS2005速度慢8/25
    仲秋節快樂
    外部表不是預期的格式8/28
    要考試10/17
    DataTable導入Excel 8/27
  • 原文地址:https://www.cnblogs.com/pengze0902/p/5962594.html
Copyright © 2011-2022 走看看