我的一个用户在尝试通过我的 C# 应用程序打开 Excel 文件时遇到了问题。
当我从我的计算机运行它并且对其他用户也起作用时,一切正常。由于我不是 Excel 互操作性专家,希望你们能帮助我解决这个问题。
这是设置方式:
我向我的应用程序添加了对 Microsoft.Office.Interop.Excel.dll 的引用,版本为 10.0.4504.0(我相信是 Excel 2002)。在我的计算机上,我安装了 Excel 2007。在我的代码中,我尝试像这样打开一个工作表:
using Microsoft.Office.Interop
...
Microsoft.Office.Interop.Excel.ApplicationClass _excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
Microsoft.Office.Interop.Excel.Workbook excelWorkbook = _excelApp.Workbooks.Open(workbookPath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", false, false, 0, true, false, false);
Microsoft.Office.Interop.Excel.Sheets excelSheets = excelWorkbook.Worksheets;
Microsoft.Office.Interop.Excel.Worksheet excelWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item(1);
我将用户的版本记录为Excel 9.0(即2000年版)。用户收到的错误信息是:
Exception='System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at Microsoft.Office.Interop.Excel.Workbooks.Open(String Filename, Object UpdateLinks, Object ReadOnly, Object Format, Object Password, Object WriteResPassword, Object IgnoreReadOnlyRecommended, Object Origin, Object Delimiter, Object Editable, Object Notify, Object Converter, Object AddToMru, Object Local, Object CorruptLoad)
看起来无法打开工作簿。确认文件存在,但它只是在用户的C:驱动器上松散存在。我认为使用PIA后就不用担心Excel版本问题了。我知道其他用户正在使用Excel 2000,并且在我的开发机器上可以正常运行。
有什么想法吗?也许我打开Excel文件的调用应该被更改?不幸的是,我无法重现这个问题。