C#开源组件DocX处理Word文档基本操作(一) 您所在的位置:网站首页 新建docx文档时格式怎么调整 C#开源组件DocX处理Word文档基本操作(一)

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

2024-07-17 09:56| 来源: 网络整理| 查看: 265

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) + " 完成!"); } } View Code

 

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

生成文档效果:

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

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



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

      专题文章
        CopyRight 2018-2019 实验室设备网 版权所有