【问题标题】:How to read a word document in asp.net c#如何在asp.net c#中读取word文档
【发布时间】:2024-04-30 17:35:02
【问题描述】:

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

【问题讨论】:

  • 首先,您可以告诉我们您的平台、语言等...

标签: ms-word document


【解决方案1】:

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

}

【讨论】:

  • 你能再解释一下这个解决方案吗?