C#运用DocX操作文档
在项目开发中,一般需要对文档进行操作,但是使用微软提供的插件,需要安装一些程序,并且如果使用wps类的文档软件就无法操作了,第三方插件DocX就可以很好的解决这些文档,结合官方提供的文档,稍作修改,总结如下的一些方法:
1.创建一个具有超链接、图像和表的文档:
///<summary>///创建一个具有超链接、图像和表的文档。///</summary>///<paramname="path">文档保存路径</param>///<paramname="p_w_picpathPath">加载的图片路径</param>publicstaticvoidHyperlinksImagesTables(stringpath,stringp_w_picpathPath){//创建一个文档using(vardocument=DocX.Create(path)){//在文档中添加超链接。varlink=document.AddHyperlink("link",newUri("http://www.google.com"));//在文档中添加一个表。vartable=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");varnewRow=table.InsertRow(table.Rows[1]);newRow.ReplaceText("4","5");//将图像添加到文档中。varp_w_picpath=document.AddImage(p_w_picpathPath);//创建一个图片(一个自定义视图的图像)。varpicture=p_w_picpath.CreatePicture();picture.Rotation=10;picture.SetPictureShape(BasicShapes.cube);//在文档中插入一个新段落。vartitle=document.InsertParagraph().Append("Test").FontSize(20).Font(newFontFamily("ComicSansMS"));title.Alignment=Alignment.center;//在文档中插入一个新段落。varp1=document.InsertParagraph();//附加内容到段落p1.AppendLine("Thislinecontainsa").Append("bold").Bold().Append("word.");p1.AppendLine("Hereisacool").AppendHyperlink(link).Append(".");p1.AppendLine();p1.AppendLine("Checkoutthispicture").AppendPicture(picture).Append("itsfunkydon'tyouthink?");p1.AppendLine();p1.AppendLine("CanyoucheckthisTableoffiguresforme?");p1.AppendLine();//在第1段后插入表格。p1.InsertTableAfterSelf(table);//在文档中插入一个新段落。Paragraphp2=document.InsertParagraph();//附加内容到段落。p2.AppendLine("Isitcorrect?");//保存当前文档document.Save();}}
2.设置文档的标题和页脚:
///<summary>///设置文档的标题和页脚///</summary>///<paramname="path">文档的路径</param>publicstaticboolHeadersAndFooters(stringpath){try{//创建新文档using(vardocument=DocX.Create(path)){//这个文档添加页眉和页脚。document.AddHeaders();document.AddFooters();//强制第一个页面有一个不同的头和脚。document.DifferentFirstPage=true;//奇偶页页眉页脚不同document.DifferentOddAndEvenPages=true;//获取本文档的第一个、奇数和甚至是头文件。HeaderheaderFirst=document.Headers.first;HeaderheaderOdd=document.Headers.odd;HeaderheaderEven=document.Headers.even;//获取此文档的第一个、奇数和甚至脚注。FooterfooterFirst=document.Footers.first;FooterfooterOdd=document.Footers.odd;FooterfooterEven=document.Footers.even;//将一段插入到第一个头。Paragraphp0=headerFirst.InsertParagraph();p0.Append("HelloFirstHeader.").Bold();//在奇数头中插入一个段落。Paragraphp1=headerOdd.InsertParagraph();p1.Append("HelloOddHeader.").Bold();//插入一个段落到偶数头中。Paragraphp2=headerEven.InsertParagraph();p2.Append("HelloEvenHeader.").Bold();//将一段插入到第一个脚注中。Paragraphp3=footerFirst.InsertParagraph();p3.Append("HelloFirstFooter.").Bold();//在奇数脚注中插入一个段落。Paragraphp4=footerOdd.InsertParagraph();p4.Append("HelloOddFooter.").Bold();//插入一个段落到偶数头中。Paragraphp5=footerEven.InsertParagraph();p5.Append("HelloEvenFooter.").Bold();//在文档中插入一个段落。Paragraphp6=document.InsertParagraph();p6.AppendLine("HelloFirstpage.");//创建一个第二个页面,显示第一个页面有自己的头和脚。p6.InsertPageBreakAfterSelf();//在页面中断后插入一段。Paragraphp7=document.InsertParagraph();p7.AppendLine("HelloSecondpage.");//创建三分之一页面显示,奇偶页不同的页眉和页脚。p7.InsertPageBreakAfterSelf();//在页面中断后插入一段。Paragraphp8=document.InsertParagraph();p8.AppendLine("HelloThirdpage.");//将属性保存入文档document.Save();returntrue;}}catch(Exceptionex){thrownewException(ex.Message);}//从内存中释放此文档。}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。