2010-01-28 88 views
1

一個word文檔我只是在工作,一個項目在asp.net C#3.5的Windows應用程序需要讀取word文檔。我想知道如何按字符讀取* .doc文件字符....我該怎麼做?如何讀取asp.net C#

+0

首先,你能告訴我們你的平臺,語言等... – 2010-01-28 06:06:47

回答

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(); 

}

+0

你能有點進一步解釋這個解決方案? – ChrisCamp 2013-06-06 19:43:54