首页 > Windows开发 > 详细

C#开源组件DocX处理Word文档基本操作(一)

时间:2020-01-23 11:53:42      阅读:718      评论:0      收藏:0      [点我收藏+]

C#中处理Word文档,是大部分程序猿绕不过的一道门。小公司或一般人员会选择使用开源组件。目前网络上出现的帖子,大部分是NPOI与DocX,其它的也有。不啰嗦了,将要使用DocX的基本方法贴出来,供参考。

经过亲测,DocX版本1.3.0.0比较稳定,基本功能使用正常(包括图片,表格,正文及页眉页脚等),建议大家选择该版本。目前为止(2020-01-23)官方最新版本为1.5.0.0,但其图片功能有问题(最先测试,其它就没深入了解了)。所以,若没有特别说明,代码中涉及的DocX版本为1.3.0.0。

DocX下载安装,有两种方式。一是开源官网下载,网址是:http://docx.codeplex.com/ ;二是在VS中使用NuGet,打开NuGet管理,查找DocX,即可看到可安装版本。当然,为了使用DocX组件,你的系统需要安装.NET框架4.0和Visual Studio 2010或更高版本。

DocX按版本不同,命名空间不一样。在1.1.0.0之前,使用 using Novacode 方式;从1.1.0.0到1.3.0.0,使用 using Xceed.Words.NET 方式;从1.4.1.0起,有两个:using Xceed.Words.NET 和 using Xceed.Document.NET。

文档组成基本类似,也按段落、表格(行、列(段落))等方式,差别在使用时基本不考虑Run,要么是Append增加,要么是Insert插入,插入有文本插入与段落(表格)的之前、之后插入,如:InsertParagraphBeforeSelf 和 InsertTableAfterSelf 等方式。实际使用请自己理解体会,这不是太难的东西。

若DocX选择版本1.3.0.0后,基本上按网上的代码与使用贴,能够处理大部分常用的Word文档了。

再次强调,若无特别说明,DocX版本为:1.3.0.0,命名空间引用:using Xceed.Words.NET,DocX组件的文件名为:Xceed.Words.NET.Dll。

代码示例如下:

private void DocX_DocMainBody()
        {
            string currPath = System.AppDomain.CurrentDomain.BaseDirectory;
            string docPath = Path.Combine(currPath, "DocxWord");
            if (!Directory.Exists(docPath))
                Directory.CreateDirectory(docPath);
            string outFile = Path.Combine(docPath, string.Format("{0}.Docx", DateTime.Now.ToString("yyyyMMddHHmmssfff")));
            string picFile = Path.Combine(currPath, "_Word.jpg");

            using (var document = File.Exists(outFile) ? DocX.Load(outFile) : DocX.Create(outFile))
            {
                Paragraph p1 = document.InsertParagraph();
                p1.InsertText("[1这是首页 - 原始段落.]");        //当前插写(或在指定位置写入)
                p1.Append("[2增加表格]");
                Table tblAdd = p1.InsertTableAfterSelf(1, 4);   //插入段落后
                tblAdd.Design = TableDesign.TableGrid;
                tblAdd.Rows[0].Cells[0].Paragraphs.First().Append("3增加的表格").Alignment = Alignment.center;
                tblAdd.InsertParagraphAfterSelf("[4表格后增加段落]");

                p1.InsertParagraphBeforeSelf("[5原始段落前插入新段落]").Append("[6增加的新文本]").Bold().InsertText(0,"[7插入的新文本]");

                Table tbl = p1.InsertTableBeforeSelf(6, 3);     //插入段落前部
                tbl.Design = TableDesign.LightShading;
                tbl.Rows[0].Cells[0].Paragraphs.First().InsertText("[8增加表格]");
                tbl.Rows[0].Cells[1].Paragraphs.First().InsertText("[9共6行3列]");
                tbl.Rows[0].Cells[2].Paragraphs.First().InsertText("[10第3列]");
                tbl.Rows[1].Cells[0].Paragraphs.First().InsertText("[11Cell10]");
                tbl.Rows[1].Cells[1].Paragraphs.First().InsertText("[12Cell11]");
                tbl.Rows[1].Cells[2].Paragraphs.First().InsertText("[13Cell12]");

                Paragraph p1_1 = p1.InsertParagraphBeforeSelf("[14原始段落前插入的段落. 本段落后插入表格]");
                Table t_1 = p1_1.InsertTableAfterSelf(tblAdd);  //先插入段落再在插入的段落之前插入表格
                t_1.Rows[0].Cells[0].Paragraphs.First().InsertText("[15步骤14插入的表格]");
                t_1.InsertRow().Cells[1].Paragraphs.First().InsertText("[16增加一行]");
                t_1.InsertRow(0).Cells[0].Paragraphs.First().Append("[17在首行插入一行]");

                Paragraph p1_2 = p1.InsertParagraphAfterSelf("[18原始段落后插入的段落。本段落后插入3段空段落]");
                Paragraph p1_3 = p1_2.InsertParagraphAfterSelf("").InsertParagraphAfterSelf("").InsertParagraphAfterSelf("");

                p1_3.InsertPageBreakAfterSelf();  //该段落之后插入换页符, 优先于同段落的表格插入

                Paragraph p2 = document.InsertParagraph();  //插入新段落
                p2 = p2.InsertParagraphAfterSelf("");
                p2 = p2.Append("[19这是第二页.]");  //AppendLine: 会增加换行(先换行再写入文本), Append: 不会换行
                p2 = p2.InsertParagraphAfterSelf("");
                Table t_2 = p2.InsertParagraphBeforeSelf("[20测试InsertParagraphAfterSelf(\"\")即增加空段落行,本段落是InsertParagraphBeforeSelf在InsertParagraphAfterSelf(\"\")的段落行之前插入,接着在本段落前插入2行3列的表格]").InsertTableBeforeSelf(2, 3);
                t_2.Design = TableDesign.TableGrid; //TableDesign.None;

                Paragraph pPic = document.InsertParagraph("[21以下插入图片]", false);
                var image = document.AddImage(picFile);
                var picture = image.CreatePicture();
                //picture.Rotation = 10;  //旋转
                picture.SetPictureShape(BasicShapes.cube);
                picture.Height = 48;
                picture.Width = 48;
                document.InsertParagraph().AppendPicture(picture);

                document.Save();  //document.SaveAs(outFile);
MessageBox.Show(Path.GetFileName(outFile) + " 完成!"); } }

代码说明:以上代码中字串的[##打头的数字,是我测试处理时分析代码作用效果用。在最后生成的Word文档中,你从这些数字打头的段落或位置上,可以直接看到代码的作用,方便你初学时理解。

生成文档效果:

技术分享图片

基本的使用就是这些,希望能帮到你。

下一篇介绍用DocX来处理页眉页脚

C#开源组件DocX处理Word文档基本操作(一)

原文:https://www.cnblogs.com/hnllhq/p/12230377.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!