C# 使用word互操作提取word内容会导致错误
我正在尝试使用以下代码提取word内容:C# 使用word互操作提取word内容会导致错误,c#,ms-office,office-interop,C#,Ms Office,Office Interop,我正在尝试使用以下代码提取word内容: // Open a doc file. Application application = new Application(); Document document = application.Documents.Open("d:\\a.doc"); // Loop through all words in the document. int count =
// Open a doc file.
Application application = new Application();
Document document = application.Documents.Open("d:\\a.doc");
// Loop through all words in the document.
int count = document.Words.Count;
for (int i = 1; i <= count; i++)
{
// Write the word.
string text = document.Words[i].Text;
Console.WriteLine("Word {0} = {1}", i, text);
}
// Close word.
application.Quit();
我已经安装了office 2013我终于下载了aspire.docnuget,并使用它提取word文件的内容,如您所见:
Document document = new Document();
document.LoadFromFile(@"d:\a.docx");
//Initialzie StringBuilder Instance
StringBuilder sb = new StringBuilder();
//Extract Text from Word and Save to StringBuilder Instance
foreach (Section section in document.Sections)
{
foreach (Paragraph paragraph in section.Paragraphs)
{
sb.AppendLine(paragraph.Text);
}
}
//Create a New TXT File to Save Extracted Text
Console.WriteLine(sb.ToString());
Console.ReadLine();
但是在运行之后,我在哪一行得到这个错误?@MattBurland Document=application.Documents.Open(“d:\\a.doc”);尝试
var application=新单词。_application()代码>和var document=application.Documents.Open(@“D:\a.doc”)代码>也许我搞错了,试试var-application=new-Word.application()代码>。我不记得您是否需要下划线。@Equalsk无法解析Word.application!!!
Document document = new Document();
document.LoadFromFile(@"d:\a.docx");
//Initialzie StringBuilder Instance
StringBuilder sb = new StringBuilder();
//Extract Text from Word and Save to StringBuilder Instance
foreach (Section section in document.Sections)
{
foreach (Paragraph paragraph in section.Paragraphs)
{
sb.AppendLine(paragraph.Text);
}
}
//Create a New TXT File to Save Extracted Text
Console.WriteLine(sb.ToString());
Console.ReadLine();