【发布时间】:2017-03-24 06:41:09
【问题描述】:
我想从 Word 文件中读取文本。它在本地主机上正常工作,但是当我在线运行它时,我得到了这个错误:
无法加载文件或程序集'office,版本=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' 或其之一 依赖关系。系统找不到指定的文件。
我的代码:
Application Objword = new Application();
Document objdoc = new Document();
dynamic FilePath = fileLoc;
dynamic NA = System.Type.Missing;
objdoc = Objword.Documents.Open(ref FilePath, ref NA, ref NA, ref NA, ref NA,ref NA, ref NA, ref NA, ref NA,
ref NA, ref NA, ref NA, ref NA,ref NA, ref NA, ref NA);
StringBuilder sb = new StringBuilder();
for (int Line = 0; Line < objdoc.Paragraphs.Count; Line++)
{
string Filedata = objdoc.Paragraphs[Line + 1].Range.Text.Trim();
if (Filedata != string.Empty)
{
//Append word files data to stringbuilder
sb.AppendLine(Filedata);
}
}
((_Document)objdoc).Close();
((_Application)Objword).Quit();
TextBox1.Text = Convert.ToString(sb);
我也将 dll 文件放在 bin 文件夹中,版本相同,但仍然出现错误。
【问题讨论】:
-
office dll 版本是否正确
-
是正确的版本。它在本地主机上正常工作。
-
不推荐在服务器环境中使用
Microsoft.Office.Interop(因为它很容易出错)并且微软也不支持(参见这个链接:Considerations for server-side Automation of Office)。您可能应该考虑使用诸如 OpenXML 之类的库来操作服务器上的办公文件。