C#如何提取word文档的内容,就是知道一个word文档的路径,如何把里面的内容通过程序给读出来?这个如何实现,谢谢!

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.Text;namespace ControlWord
    {
        
        public class CCWordApp
        {
            private Word.ApplicationClass oWordApplic;// a reference to Word application
            private Word.Document oDoc;// a reference to the document        //用于读取打开Word的字符数
            public int Count = 0;        public CCWordApp()
            {            oWordApplic = new Word.ApplicationClass();
            }       
            public void Open(string strFileName)
            {
                object fileName = strFileName;
                object readOnly = false;
                object isVisible = true;
                object missing = System.Reflection.Missing.Value;            oDoc = oWordApplic.Documents.Open(ref fileName, ref missing, ref readOnly,
                    ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing);
                
                Count = oDoc.Characters.Count;
                oDoc.Activate();
            }
       
            // 功能:WORD退出   
            public void Quit()
            {
                object missing = System.Reflection.Missing.Value;
                oWordApplic.Application.Quit(ref missing, ref missing, ref missing);
            }        public void Save()
            {
                oDoc.Save();
            }        public void SaveAs(string strFileName)
            {
                object missing = System.Reflection.Missing.Value;
                object fileName = strFileName;            oDoc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            }
            public void SaveAsHtml(string strFileName)
            {
                object missing = System.Reflection.Missing.Value;
                object fileName = strFileName;
                object Format = (int)Word.WdSaveFormat.wdFormatHTML;
                oDoc.SaveAs(ref fileName, ref Format, ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
                oDoc.Close(ref missing, ref missing, ref missing);        }
            public void CopyAll()
            {
                oWordApplic.Selection.WholeStory();
                oWordApplic.Selection.Copy();        }        public void PasetAll()
            {            oWordApplic.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault);
            }
            public void Clear()
            {            object Unit = (int)Word.WdUnits.wdCharacter;
                object Count = 1;
                oWordApplic.Selection.WholeStory();
                oWordApplic.Selection.Delete(ref Unit, ref Count);
            }
            public void InsertText(string strText)
            {
                oWordApplic.Selection.TypeText(strText);
            }        public void InsertLineBreak()
            {
                oWordApplic.Selection.TypeParagraph();
            }
            public void InsertLineBreak(int nline)
            {
                for (int i = 0; i < nline; i++)
                    oWordApplic.Selection.TypeParagraph();
            }
            public void SetAlignment(string strType)
            {
                switch (strType)
                {
                    case "Center":
                        oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                        break;
                    case "Left":
                        oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
                        break;
                    case "Right":
                        oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
                        break;
                    case "Justify":
                        oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify;
                        break;
                }        }        public void SetFont(string strType)
            {
                switch (strType)
                {
                    case "Bold":
                        oWordApplic.Selection.Font.Bold = 1;
                        break;
                    case "Italic":
                        oWordApplic.Selection.Font.Italic = 1;
                        break;
                    case "Underlined":
                        oWordApplic.Selection.Font.Subscript = 0;
                        break;
                }        }
      

  2.   

    =======================接上 public void SetFont()
            {
                oWordApplic.Selection.Font.Bold = 0;
                oWordApplic.Selection.Font.Italic = 0;
                oWordApplic.Selection.Font.Subscript = 0;        }        public void SetFontName(string strType)
            {
                oWordApplic.Selection.Font.Name = strType;        }        public void SetFontSize(int nSize)
            {
                oWordApplic.Selection.Font.Size = nSize;        }        public void InsertPagebreak()
            {            object pBreak = (int)Word.WdBreakType.wdPageBreak;
                oWordApplic.Selection.InsertBreak(ref pBreak);
            }        public void GotoBookMark(string strBookMarkName)
            {            object missing = System.Reflection.Missing.Value;            object Book = (int)Word.WdGoToItem.wdGoToBook;
                object NameBookMark = strBookMarkName;
                oWordApplic.Selection.GoTo(ref Book, ref missing, ref missing, ref NameBookMark);
            }        public void GoToTheEnd()
            {            object missing = System.Reflection.Missing.Value;
                object unit;
                unit = Word.WdUnits.wdStory;
                oWordApplic.Selection.EndKey(ref unit, ref missing);        }
            public void GoToTheBeginning()
            {            object missing = System.Reflection.Missing.Value;
                object unit;
                unit = Word.WdUnits.wdStory;
                oWordApplic.Selection.HomeKey(ref unit, ref missing);        }        public void GoToTheTable(int ntable)
            {            object missing = System.Reflection.Missing.Value;
                object what;
                what = Word.WdUnits.wdTable;
                object which;
                which = Word.WdGoToDirection.wdGoToFirst;
                object count;
                count = 1;
                oWordApplic.Selection.GoTo(ref what, ref which, ref count, ref missing);
                oWordApplic.Selection.Find.ClearFormatting();            oWordApplic.Selection.Text = "";
            }        public void GoToRightCell()
            {            object missing = System.Reflection.Missing.Value;
                object direction;
                direction = Word.WdUnits.wdCell;
                oWordApplic.Selection.MoveRight(ref direction, ref missing, ref missing);
            }        public void GoToLeftCell()
            {            object missing = System.Reflection.Missing.Value;
                object direction;
                direction = Word.WdUnits.wdCell;
                oWordApplic.Selection.MoveLeft(ref direction, ref missing, ref missing);
            }        public void GoToDownCell()
            {            object missing = System.Reflection.Missing.Value;
                object direction;
                direction = Word.WdUnits.wdLine;
                oWordApplic.Selection.MoveDown(ref direction, ref missing, ref missing);
            }        public void GoToUpCell()
            {            object missing = System.Reflection.Missing.Value;
                object direction;
                direction = Word.WdUnits.wdLine;
                oWordApplic.Selection.MoveUp(ref direction, ref missing, ref missing);
            }
            public void InsertPageNumber(string strType, bool bHeader)
            {
                object missing = System.Reflection.Missing.Value;
                object alignment;
                object bFirstPage = false;
                object bF = true;
                switch (strType)
                {
                    case "Center":
                        alignment = Word.WdPageNumberAlignment.wdAlignPageNumberCenter;
                        oWordApplic.Selection.HeaderFooter.PageNumbers.Item(1).Alignment = Word.WdPageNumberAlignment.wdAlignPageNumberCenter;
                        break;
                    case "Right":
                        alignment = Word.WdPageNumberAlignment.wdAlignPageNumberRight;
                        oWordApplic.Selection.HeaderFooter.PageNumbers.Item(1).Alignment = Word.WdPageNumberAlignment.wdAlignPageNumberRight;
                        break;
                    case "Left":
                        alignment = Word.WdPageNumberAlignment.wdAlignPageNumberLeft;
                        oWordApplic.Selection.HeaderFooter.PageNumbers.Add(ref alignment, ref bFirstPage);
                        break;
                }        }    }
    }
      

  3.   

    ================接上
     调用方法:
     CCWordApp test;
                        test = new CCWordApp();
                        test.Open(fi.FullName);
                        //这里为什么是2不是1呢?没整明白
                        if (test.Count < 2)
                        {
                            test.GoToTheEnd();
                            test.InsertText("略");
                            test.Save();
                            Content.Text += "处理了下面的文件:" + fi.Name + "\r\n";
                        }
                        else
                        {
                            Content.Text += "略过了下面的文件:" + fi.Name + "\r\n";
                        }                    test.Quit();