我的一位用户在尝试通过我的C#应用程序打开Excel文件时遇到问题。C#和Excel互操作
当我从机器上运行它并且它适用于其他用户时,一切正常。我不是Excel interop专家,所以希望你们能帮助我。
这里是它是如何设置:
我添加了一个参考我的应用程序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文件应该改变?不幸的是我无法复制它。
室内用请这方面的帮助,如果ü可以http://stackoverflow.com/questions/9962157/safely-disposing-of-an-object-using-c-sharp/ – 2012-04-01 22:39:10