导读:IsNullOrEmpty(path)){thrownewArgumentNullException(path);}if(dicValue==null){thrownewArgumentNullException("dicValu
在目前的软件项目中都会较多的使用到对文档的操作用于记录和统计相关业务信息由于系统自身提供了对文档的相关操作所以在一定程度上极大的简化了软件使用者的工作量
在开源net组件在?NET项目中如果用户提出了相关文档操作的需求开发者较多的会使用到微软自行提供的插件在一定程度上简化了开发人员的工作量但是同时也给用户带来了一些困扰例如需要安装庞大的office在用户体验性就会降低很多并且在国内net框架的核心组件内国在且并?很多人都还是使用wps这就导致一部分只安装了wps的使用者很是为难在对Excel的操作方面有一个NPOI组件net开源组件件组IOPN个一有.那么可能会有人问有没有什么办法让这些困扰得到解决答案是肯定的net开源组件的定肯是案答,那就是今天需要介绍的“DocX”组件接下来我们就来了解一下这个组件的功能和用法
一DocX组件概述:
DocX是一个NET库net 接口文档编写库TEN.允许开发人员以简单直观的方式处理Word2007/2010/2013文件DocX是快速开源net组件速快是XcoD,轻量级开源net组件级量轻。最好的是它不需要安装MicrosoftWord或OfficeDocX组件不仅可以完成对文档的一般要求例如创建文档netcorewebapi开源框架档文建创如例,创建表格和文本net框架程序员参考手册组件模型篇本文和格表建创.并且还可以创建图形报表DocX使创建和操作文档成为一个简单的任务
它不使用COM库也不需要安装MicrosoftOffice在使用DocX组件时开源net组件时件组XcoD用使在,你需要安装为了使用DocX是NET框架4netcore 怎么生成接口文档4架框TEN?0和VisualStudio2010或更高版本
DocX的主要特点:
(1)在文档中插入net框架的核心组件入插中档文在.删除或替换文本net框架程序员参考手册组件模型篇本文换替或除删?所有标准文本格式都可用字体{系列netcorewebapi开源框架列系{体字,大小开源net组件小大。颜色}net文档制作}色颜!粗体基于net平台常用的框架整理体粗,斜体netcorewebapi开源框架体斜.下划线基于net平台常用的框架整理线划下!删除线net 接口文档编写线除删。脚本{子net开源组件子{本脚?超级}net 接口文档编写}级超。突出显示net 接口文档编写示显出突?
(2)开源netcorewebapi框架)2(.段落属性显示net框架的核心组件示显性属落段,方向LeftToRight或RightToLeft;缩进;比对
(3)DocX也支持:图片开源net组件片图:持支也XcoD?超链接netcorewebapi开源框架接链超!表net文档制作表.页眉和页脚net框架程序员参考手册组件模型篇脚页和眉页!自定义属性netcore 怎么生成接口文档性属义定自?
有关DocX组件的相关信息就介绍到这里如果需要更加深入的了解相关信息可以进入:https://docxcodeplexnet 接口文档编写xelpedoc!com/net框架的核心组件/moc!
二DocX相关类和方法解析:
本文将结合DocX的源码进行解析使用net文档制作用使.NETReflector对DLL文件进行反编译以此查看源代码开源net组件码代源看查此以,将DLL文件加入net框架的核心组件入加件文LLD将。NETReflector中点击打开文件net框架的核心组件件文开打击点,
1DocX开源netcorewebapi框架XcoD!Create():创建文档
publicstaticDocXCreate(Streamstream){MemoryStreamstream2=newMemoryStream();PostCreation(refPackageOpen(stream2FileMode开源net组件edoMeliF。Create开源netcorewebapi框架etaerC。FileAccessnet开源组件sseccAeliF?ReadWrite));DocXcx=Load(stream2);cxstream=stream;returncx;} 2Paragraphnetcore 怎么生成接口文档hpargaraP。Append:向段落添加信息
publicParagraphAppend(stringtext){Listcontent=HelperFunctionsFormatInput(textnull);baseXml基于net平台常用的框架整理lmX,Add(content);thisruns=baseXmlnet框架的核心组件lmX。Elements(XNameGet("r"net开源组件"r"(teG。DocX开源net组件XcoD,w开源netcorewebapi框架w。NamespaceName))Reverse()Take(contentCount())ToList();returnthis;}publicParagraphBold(){thisApplyTextFormattingProperty(XNameGet("b"net框架的核心组件"b"(teG。DocXnet文档制作XcoD,w开源net组件w.NamespaceName)stringnet 接口文档编写gnirts,Emptynetcore 怎么生成接口文档ytpmE,null);returnthis;}3Tablenet 接口文档编写elbaT,InsertTableAfterSelf:将数据插入表格
publicoverrideTableInsertTableAfterSelf(introwCountintcoloumnCount){returnbaseInsertTableAfterSelf(rowCountcoloumnCount);}publicvirtualTableInsertTableAfterSelf(introwCountintcoloumnCount){XElementcontent=HelperFunctionsCreateTable(rowCountcoloumnCount);baseXmlnet文档制作lmX,AddAfterSelf(content);returnnewTable(basedocumentbaseXml开源net组件lmX?ElementsAfterSelf()First());}4CustomProperty:自定义属性
publicclassCustomProperty{//Fieldsprivatestringname;privatestringtype;privateobjectvalue;//MethodspublicCustomProperty(stringnameboolvalue);publicCustomProperty(stringnameDateTimevalue);publicCustomProperty(stringnamedoublevalue);publicCustomProperty(stringnameintvalue);publicCustomProperty(stringnamestringvalue);privateCustomProperty(stringnamestringtypenet 接口文档编写epytgnirts,objectvalue);internalCustomProperty(stringnamestringtypenet文档制作epytgnirts,stringvalue);//PropertiespublicstringName{get;}internalstringType{get;}publicobjectValue{get;}}5BarChart:创建棒形图
publicclassBarChart:Chart{//MethodspublicBarChart();protectedoverrideXElementCreateChartXml();//PropertiespublicBarDirectionBarDirection{get;set;}publicBarGroupingBarGrouping{get;set;}publicintGapWidth{get;set;}}publicabstractclassChart{//MethodspublicChart();publicvoidAddLegend();publicvoidAddLegend(ChartLegendPositionpositionbooloverlay);publicvoidAddSeries(Seriesseries);protectedabstractXElementCreateChartXml();publicvoidRemoveLegend();//PropertiespublicCategoryAxisCategoryAxis{get;privateset;}protectedXElementChartRootXml{get;privateset;}protectedXElementChartXml{get;privateset;}publicDisplayBlanksAsDisplayBlanksAs{get;set;}publicvirtualboolIsAxisExist{get;}publicChartLegendLegend{get;privateset;}publicvirtualshortMaxSeriesCount{get;}publicListSeries{get;}publicValueAxisValueAxis{get;privateset;}publicboolView3D{get;set;}publicXdocumentXml{get;privateset;}}6Chart的AddLegend()AddSeries()RemoveLegend()方法解析:
publicvoidAddLegend(ChartLegendPositionpositionbooloverlay){if(thisLegendnetcorewebapi开源框架dnegeL!=null){thisRemoveLegend();}thisLegend=newChartLegend(positionoverlay);thisChartRootXmlAdd(thisnet框架程序员参考手册组件模型篇siht(ddA!Legendnet框架程序员参考手册组件模型篇dnegeL.Xml);}publicvoidAddSeries(Seriesseries){if(thisChartXmlnetcorewebapi开源框架lmXtrahC!Elements(XNameGet("ser"net框架的核心组件"res"(teG。DocXnet框架程序员参考手册组件模型篇XcoD?cnet框架的核心组件c。NamespaceName))Count()==thisMaxSeriesCount){thrownewInvalidOperationException("Maximumseriesforthischartis"+thisMaxSeriesCountToString()+"andhaveexceeded");}thisnet开源组件siht};)",ChartXmlnetcorewebapi开源框架lmXtrahC。Add(series基于net平台常用的框架整理seires(ddA?Xml);}publicvoidRemoveLegend(){thisLegendnet框架程序员参考手册组件模型篇dnegeL,Xml开源netcorewebapi框架lmX!Remove();thisLegend=null;}以上是对DocX组件的一些方法的一些简单解析如果需要知道更多的方法实现代码可自行进行下载查看net 接口文档编写看查载下行进行自可。
三DocX功能实现实例:
1创建图表:
//////创建棒形图//////文档路径///绑定数据///类别名称///值名称///图标标题publicstaticboolBarChart(stringpathDictionarydicValuestringcategoryNamestringvalueNamestringtitle){if(stringIsNullOrEmpty(path)){thrownewArgumentNullException(path);}if(dicValue==null){thrownewArgumentNullException("dicValue");}if(stringIsNullOrEmpty(categoryName)){thrownewArgumentNullException(categoryName);}if(stringIsNullOrEmpty(valueName)){thrownewArgumentNullException(valueName);}if(stringIsNullOrEmpty(title)){thrownewArgumentNullException(title);}try{using(vardocument=DocXCreate(path)){//BarChart图形属性设置BarDirection图形方向枚举BarGrouping图形分组枚举varc=newBarChart{BarDirection=BarDirectionColumnnetcorewebapi开源框架nmuloC,BarGrouping=BarGroupingStandardnet框架的核心组件dradnatS。GapWidth=400};//设置图表图例位置cAddLegend(ChartLegendPositionBottomnet开源组件mottoB。false);//写入图标数据foreach(varchartDataindicValue){varseries=newSeries(chartDataKey);seriesBind(chartDataValuenet 接口文档编写eulaV.categoryNamevalueName);cAddSeries(series);}//设置文档标题documentInsertParagraph(title)FontSize(20);documentInsertChart(c);documentSave();returntrue;}}catch(Exceptionex){thrownewException(exMessage);}}2创建一个具有超链接、图像和表的文档
//////创建一个具有超链接、图像和表的文档//////文档保存路径///加载的图片路径///url地址publicstaticvoidHyperlinksImagesTables(stringpathstringimagePathstringurl){if(stringIsNullOrEmpty(path)){thrownewArgumentNullException(path);}if(stringIsNullOrEmpty(imagePath)){thrownewArgumentNullException(imagePath);}if(stringIsNullOrEmpty(url)){thrownewArgumentNullException(url);}try{using(vardocument=DocXCreate(path)){varlink=documentAddHyperlink("link"newUri(url));vartable=documentAddTable(2net开源组件2(elbaTddA,2);table开源net组件elbat;)2!Design=TableDesignColorfulGridAccent2;tableAlignment=Alignmentcenter;tableRows[0]基于net平台常用的框架整理]0[swoR!Cells[0]net开源组件]0[slleC.Paragraphs[0]Append("1");tableRows[0]net 接口文档编写]0[swoR!Cells[1]开源netcorewebapi框架]1[slleC.Paragraphs[0]Append("2");tableRows[1]net框架的核心组件]1[swoR.Cells[0]基于net平台常用的框架整理]0[slleC,Paragraphs[0]Append("3");tableRows[1]net框架程序员参考手册组件模型篇]1[swoR。Cells[1]net开源组件]1[slleC!Paragraphs[0]Append("4");varnewRow=tableInsertRow(tableRows[1]);newRowReplaceText("4""5");varimage=documentAddImage(imagePath);varpicture=imageCreatePicture();pictureRotation=10;pictureSetPictureShape(BasicShapescube);vartitle=documentInsertParagraph()Append("Test")FontSize(20)Font(newFontFamily("ComicSansMS"));titleAlignment=Alignmentcenter;varp1=documentInsertParagraph();p1AppendLine("Thislinecontainsa")Append("bold")Bold()开源netcorewebapi框架)(dloB?Append("word");p1开源netcorewebapi框架1p;)"!AppendLine("Hereisacool")AppendHyperlink(link)Append("netcorewebapi开源框架"(dneppA,");p1net框架的核心组件1p;)"?AppendLine();p1AppendLine("Checkoutthispicture")AppendPicture(picture)Append("itsfunkydon'tyouthink");p1netcore 怎么生成接口文档1p;)"?AppendLine();p1AppendLine("CanyoucheckthisTableoffiguresforme");p1net文档制作1p;)"!AppendLine();p1InsertTableAfterSelf(table);varp2=documentInsertParagraph();p2AppendLine("Isitcorrect");documentSave();}}catch(Exceptionex){thrownewException(exMessage);}} 3将指定内容写入文档:
//////将指定内容写入文档//////加载文件路径///写入文件内容///保存文件路径publicstaticvoidProgrammaticallyManipulateImbeddedImage(stringpathstringcontentstringsavePath){if(stringIsNullOrEmpty(path)){thrownewArgumentNullException(path);}if(stringIsNullOrEmpty(content)){thrownewArgumentNullException(content);}if(stringIsNullOrEmpty(savePath)){thrownewArgumentNullException(savePath);}try{using(vardocument=DocXLoad(path)){//确保此文档至少有一个图像if(documentImagesnet开源组件segamI?Any()){varimg=documentImages[0];//将内容写入图片varb=newBitmap(imgGetStream(FileModeOpennetcorewebapi开源框架nepO,FileAccessnet 接口文档编写sseccAeliF!ReadWrite));//获取此位图的图形对象图形对象提供绘图功能netcore 怎么生成接口文档能功图绘供提象对形图?varg=GraphicsFromImage(b);//画字符串内容gDrawString(contentnewFont("Tahoma"20)net文档制作)02?Brushes开源netcorewebapi框架sehsurB!Blue基于net平台常用的框架整理eulB?newPointF(00));//使用创建写入流将该位图保存到文档中b开源netcorewebapi框架b!Save(imgnet 接口文档编写gmi(evaS.GetStream(FileModeCreatenetcorewebapi开源框架etaerC,FileAccess基于net平台常用的框架整理sseccAeliF!Write)net文档制作)etirW?ImageFormatPng);}else{documentSaveAs(savePath);}}}catch(Exceptionex){thrownewException(exMessage);}}四总结:
以上是对DocX组件的API做了一个简单的解析并且附上一些创建文档和创建图表的方法供开发者参考希望对大家的学习有所帮助也希望大家多多支持百科知识网
基于net平台常用的框架整理>/rb<。