zoukankan      html  css  js  c++  java
  • DocX操作word生成报表

    1.DocX简介

    1.1 简介

      DocX是一个在不需要安装word的情况下对word进行操作的开源轻量级.net组件,是由爱尔兰的一个叫Cathal Coffey的博士生开发出来的。DocX使得操作word非常轻便,有利于减轻开发负担,提升程序效率。DocX在Codeplex和Github上都有开源。

    1.2 获取与安装

    可以在http://docx.codeplex.com/releases下载获取,也可以直接利用NuGet获取。

    Install-Package DocX

    1.3 开发环境

    用DocX需要.NET framework4.0和VS2010或更高版本。

    2.DocX相关常用操作(持续更新中...)

    2.1 创建word文档

    DocX document = DocX.Create(@"docsHelloWorld.docx")

    2.2 加载word文档

    DocX document = DocX.Load(@"docsHelloWorld.docx")

     

    2.3 书签相关操作

    2.3.1 插入书签

    var paragraph = document.InsertBookmark("firstBookmark");

    2.3.2 根据书签名获取书签

    如果知道一个书签的书签名,可以直接得到。

    var b = document.Bookmarks["书签1"];

    2.3.3 在书签中插入文字

    document.Bookmarks["书签1"].SetText("Hello World!");

    2.3.4 在书签中插入图片、表格

    document.Bookmarks["书签2"].Paragraph.InsertPicture(@"pic.jpg");
    document.Bookmarks["书签3"].Paragraph.InsertTableAfterSelf(t);//t是Table类型

     

    2.4 分节符和分页符

    2.4.1 分节符

    document.InsertSectionPageBreak();//分节符

    2.4.2 分页符

      Paragraph p = document.InsertParagraph();
      p.InsertPageBreakAfterSelf();//分页符

    2.5 添加目录

    复制代码
     1  static void AddToc()
     2         {
     3             Console.WriteLine("	AddToc()");
     4 
     5             using (var document = DocX.Create(@"docsToc.docx"))
     6             {
     7                 document.InsertTableOfContents("目录", TableOfContentsSwitches.O | TableOfContentsSwitches.U | TableOfContentsSwitches.Z | TableOfContentsSwitches.H, "Heading2");
     8                 var h1 = document.InsertParagraph("Heading 1");
     9                 h1.StyleName = "Heading1";
    10                 document.InsertParagraph("Some very interesting content here");
    11                 var h2 = document.InsertParagraph("Heading 2");
    12                 document.InsertSectionPageBreak();
    13                 h2.StyleName = "Heading1";
    14                 document.InsertParagraph("Some very interesting content here as well");
    15                 var h3 = document.InsertParagraph("Heading 2.1");
    16                 h3.StyleName = "Heading2";
    17                 document.InsertParagraph("Not so very interesting....");
    18 
    19                 document.Save();
    20             }
    21         }
    复制代码

     2.6 插入图片

    Image img = document.AddImage(@"pic.jpg");
    Picture pic = img.CreatePicture();
    Paragraph p1 = document.InsertParagraph();
    p1.InsertPicture(pic);

    2.7 操作表格

    2.7.1 创建和插入表格

    Table t = document.AddTable(3, 4);//三行四列

    2.7.2 单元格合并

    Table t = document.AddTable(3,4);
    t.MergeCellsInColumn(0, 0, 1);//public void MergeCellsInColumn(int columnIndex, int startRow, int endRow);竖向合并
    t.Rows[0].MergeCells(1, 2);//public void MergeCells(int startIndex, int endIndex);横向合并

    注:合并单元格的时候注意,最好先竖向合并,再横向合并,以免报错,因为横向合并会改变列数。

    3. 资源

    开源网址:http://docx.codeplex.com/ (里面的示例代码很适合初学者学习)

    高质量博客推荐:http://www.cnblogs.com/asxinyu/archive/2013/02/22/2921861.html#_label3

    利用DocX操作word的开源小项目:https://github.com/hahahuahai/create-word-by-DocX

    参考:http://www.cnblogs.com/huahai/p/DocX.html

  • 相关阅读:
    IIS7.5应用程序池集成模式和经典模式的区别【转】
    Qt keyPressEvent keyReleaseEvent 分析
    cesium编程中级(二)源码编译
    cesium编程中级(一)添加示例到Sandcastle
    cesium编程中级开篇
    cesium编程入门(九)实体 Entity
    cesium编程入门(八)设置材质
    QPushButton 点击信号分析
    cesium编程入门(七)3D Tiles,模型旋转
    cesium编程入门(六)添加 3D Tiles,并调整位置,贴地
  • 原文地址:https://www.cnblogs.com/marblemm/p/7363654.html
Copyright © 2011-2022 走看看