C#读取Word文件示例代码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

C#读取Word文档示例代码在VisualStudio2005中新建控制台应用程序,命名为GetWordContent添加如下引用:Microsoft.Office.Interop.WordSystem.DrawingSystem.Windows.Forms★详细代码如下:usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.IO;namespaceGetWordContent{classProgram{[STAThread]staticvoidMain(string[]args){//根¨´据Y需¨¨要°a输º?入¨?正y确¨¡¤的Ì?OA文?档̦Ì路¡¤径?stringfileName=@E:\ylq_2010\GetWordContent\GetWordContent\bin\Debug\OA文?档̦Ì.doc;boolisOpen=OfficeDocIO.OpenWord(fileName);Console.WriteLine(打䨰开aWord文?档̦Ì[{0}]是º?否¤?成¨¦功|:êo{1},Path.GetFileName(fileName),isOpen);Console.WriteLine();Console.WriteLine(获?取¨?分¤?段?落?文?本À?内¨²容¨Y,ê?段?落?索¡Â引°y从䨮1开a始º?:êo);intparagraph=OfficeDocIO.getParCount();Console.WriteLine(共2有®D[{0}]段?,paragraph);//段?落?索¡Â引°y从䨮1开a始º??ê?for(inti=1;i=paragraph;i++){Console.WriteLine(段?落?[{0}]:{1},i,OfficeDocIO.ReadPar(i));}Console.WriteLine();Console.WriteLine(直¡À接¨®获?取¨?Word的Ì?全¨?部?文?本À?内¨²容¨Y:êo);Console.WriteLine(OfficeDocIO.GetDocContent());OfficeDocIO.CloseWord();Console.ReadLine();}}///summary///操¨´作Á¡ÂWord的Ì?接¨®口¨²帮ã?助¨²类¤¨¤////summarypublicclassOfficeDocIO{privatestaticMicrosoft.Office.Interop.Word._ApplicationappWord=null;privatestaticMicrosoft.Office.Interop.Word._DocumentdocWord=null;///summary///打䨰开a一°?个?Word文?档̦Ì,ê?相¨¤对?的Ì?方¤?法¤¡§是º?CloseWord()////summary///paramname=file文?件t名?/param///returns/returnspublicstaticboolOpenWord(stringfile){appWord=newMicrosoft.Office.Interop.Word.Application();objectpath=file;objectmissing=System.Reflection.Missing.Value;objectreadOnly=true;try{docWord=appWord.Documents.Open(refpath,refmissing,refreadOnly,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing);returntrue;}catch(Exceptione){Console.WriteLine(e.Message);appWord=null;returnfalse;}}///summary///读¨¢取¨?word文?档̦Ì的Ì?第̨²i段?////summary///paramname=i/param///returns/returnspublicstaticobjectReadPar(inti){try{stringtemp=docWord.Paragraphs[i].Range.Text;returntemp;}catch(Exceptione){Console.WriteLine(Error:+e.ToString());returnnull;}}///summary///返¤¦Ì回?word文?档̦Ì一°?共2几?段?////summary///returns/returnspublicstaticintgetParCount(){returndocWord.Paragraphs.Count;}///summary///获?取¨?文?档̦Ì的Ì?文?本À?内¨²容¨Y////summary///returns/returnspublicstaticobjectGetDocContent(){returndocWord.Content.Text;}///summary///提¬¨¢取¨?word文?档̦Ì中D表À¨ª格?的Ì?内¨²容¨Y////summary///paramname=tableid表À¨ª格?索¡Â引°y/param///paramname=rowIndex行D号?/param///paramname=colIndex列¢D号?/param///paramname=type内¨²容¨Y类¤¨¤型¨ª1为a文?本À?2为a图ª?片?/param///returns提¬¨¢取¨?出?来¤¡ä的Ì?内¨²容¨Y/returnspublicstaticobjectGetWordTableContent(inttableid,introwIndex,intcolIndex,inttype){if(docWord!=null&&docWord.Tables.Counttableid-1){objectcontent=null;try{switch(type){case1:if(docWord.Tables[tableid].Rows.Count(rowIndex-1)&&docWord.Tables[tableid].Columns.Count(colIndex-1)){content=docWord.Tables[tableid].Cell(rowIndex,colIndex).Range.Text.ToString();}break;case2:if(docWord.Tables[tableid].Rows.Count(rowIndex-1)&&docWord.Tables[tableid].Columns.Count(colIndex-1)){if(docWord.Tables[tableid].Cell(rowIndex,colIndex).Range.InlineShapes.Count0){foreach(Microsoft.Office.Interop.Word.InlineShapeishindocWord.Tables[tableid].Cell(rowIndex,colIndex).Range.InlineShapes)//目?前¡ã处ä|理¤¨ª的Ì?word文?档̦Ì中D只?有®D一°?张?图ª?片?{if(ish.Type==Microsoft.Office.Interop.Word.WdInlineShapeType.wdInlineShapePicture||ish.Type==Microsoft.Office.Interop.Word.WdInlineShapeType.wdInlineShapeLinkedPicture){ish.Select();appWord.Selection.Copy();System.Drawing.Imageimage=System.Windows.Forms.Clipboard.GetImage();content=image;if(image==null){content=ish;}}}}}break;default:break;}returncontent;}catch(Exceptione){Console.WriteLine(e.Message);}}returnnull;}///summary///关?闭À?打䨰开a的Ì?word文?档̦Ì////summarypublicstaticvoidCloseWord(){objectmissing=System.Reflection.Missing.Value;try{if(docWord!=null){docWord.Close(refmissing,refmissing,refmissing);docWord=null;}if(appWord!=null){appWord.Quit(refmissing,refmissing,refmissing);appWord=null;}}catch(Exceptione){Console.WriteLine(e.Message);}}}}★程序运行结果如图:

1 / 6
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功