如何在asp.net中读取word文档c#

时间:2010-01-28 05:56:50

标签: ms-word document

我只是在asp.net c#3.5 windows应用程序中处理一个需要阅读word文档的项目。我想知道如何逐个字符地读取* .doc文件....我该怎么做?

1 个答案:

答案 0 :(得分:0)

Microsoft.Office.Interop.Word.Application WApp;  Microsoft.Office.Interop.Word.Document Wdoc;

WApp = new Microsoft.Office.Interop.Word.Application();
//Opening Word file
Thread.Sleep(5312);
Wdoc = WApp.Documents.Open(@"C:\Users\Doc.doc");
object start = 0;
object end = Wdoc.Characters.Count;
Range rng = Wdoc.Range(ref start, ref end);


int wordCount = Wdoc.Words.Count;
// Display retrieved (incomplete) text
rng.TextRetrievalMode.IncludeHiddenText = false;
rng.TextRetrievalMode.IncludeFieldCodes = false;

// Find phrase in text string
string WTest;
string[] Title;
Title = new string[10];
Title[1] = "word1 ";
Title[2] = "word2 ";
Title[3] = "word3 ";
Title[4] = "word4 ";
Title[5] = "word5 ";
Title[6] = "word6 ";
Title[7] = "word7 ";
Title[8] = "word8 ";
Title[9] = "word9 ";
int icount = 1;
int n = 1;
int i=1;

while (icount <= wordCount)
{
    WTest = Wdoc.Words[icount].Text.ToString();

    foreach(string element in Title)
    {

        if (Title[i] == WTest)
        {
            Assert.IsTrue(true);
            icount++;
            i++;
            break;
        }
        else if (i == wordCount)
        {
            Assert.Fail("Doc has no Data");
            break;
        }
        else
        {
            icount++;


        }
        break;  
    }

    continue;
}
 Wdoc.Close(true);
 WApp.Quit();

}

相关问题