System.AccessViolationException:尝试读取或写入受保护的内存

发布于 2024-11-30 22:10:17 字数 1243 浏览 0 评论 0原文

我正在尝试打开 Excel 2000 文件。当我使用 Excel 2007 尝试此操作时,它工作正常,但当我在开发服务器上检查它时,它无法工作。打开Excel文件时出现错误。我正在使用下面的代码。

 // file
 string sFile = @"c:\testfile.xls";

 // create excel file
 Excel.Application oExcel;
 Excel.Workbook oBook;
 Excel.Worksheet oSheet;
 DataTable dt = new DataTable();
 DataRow dr = dt.NewRow();


oExcel = new Excel.ApplicationClass();
oBook = oExcel.Workbooks.Add(Type.Missing);
oBook = oExcel.Workbooks.Open(sFile, 0, false, 5, "", "", true, 
    Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 
    0, true, true, false);
oSheet = (Excel.Worksheet)oBook.Worksheets.get_Item(2);
oSheet.Cells[2, 1] = "Successfully Created";
oSheet.Cells[2, 2] = "Sample excel updated";
oExcel.DisplayAlerts = false;

// save & close
oBook.Close(true, sFile,false);
oExcel.Quit();
Marshal.ReleaseComObject(oExcel);
oExcel = null;
GC.Collect();

错误消息如下:

“未处理的异常:System.AccessViolationException:尝试 读或写受保护的内存。这通常表明 其他内存已损坏。在 Microsoft.Office.Interop.Excel.Workbooks.Open(字符串文件名,对象 更新链接、对象只读、对象格式、对象密码、对象 WriteResPassword、对象忽略只读推荐、对象起源、 对象分隔符、对象 Edi 表、对象通知、对象转换器、 对象 AddToMru、对象本地、对象 CorruptLoad)位于 TestExcel.Program.Main(String[] args)"

I am trying to open and Excel 2000 file. When I try this with Excel 2007 it is working fine but when I checked it on the development server it is not working. It is thowing an error while opening the excel file. I am using the below code.

 // file
 string sFile = @"c:\testfile.xls";

 // create excel file
 Excel.Application oExcel;
 Excel.Workbook oBook;
 Excel.Worksheet oSheet;
 DataTable dt = new DataTable();
 DataRow dr = dt.NewRow();


oExcel = new Excel.ApplicationClass();
oBook = oExcel.Workbooks.Add(Type.Missing);
oBook = oExcel.Workbooks.Open(sFile, 0, false, 5, "", "", true, 
    Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 
    0, true, true, false);
oSheet = (Excel.Worksheet)oBook.Worksheets.get_Item(2);
oSheet.Cells[2, 1] = "Successfully Created";
oSheet.Cells[2, 2] = "Sample excel updated";
oExcel.DisplayAlerts = false;

// save & close
oBook.Close(true, sFile,false);
oExcel.Quit();
Marshal.ReleaseComObject(oExcel);
oExcel = null;
GC.Collect();

The error msg is the following:

"Unhandled 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
Upda teLinks, Object ReadOnly, Object Format, Object Password, Object
WriteResPasswor d, Object IgnoreReadOnlyRecommended, Object Origin,
Object Delimiter, Object Edi table, Object Notify, Object Converter,
Object AddToMru, Object Local, Object Co rruptLoad) at
TestExcel.Program.Main(String[] args)"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆流 2024-12-07 22:10:17

不幸的是,这几乎不可能说。你说:

但是当我在开发服务器上检查它时它不起作用

这在某种程度上暗示这是一项服务。如果是这种情况,那么您无能为力,Microsoft Office 应用程序并非设计为在服务中运行< /a>.

Unfortunately, it's almost impossible to say. You say:

but when I checked it on the development server it is not working

This implies somewhat that this is a service. If that is the case, then there's not much you can do, Microsoft Office applications are not designed to run in services.

抱猫软卧 2024-12-07 22:10:17

您的服务器是否也安装了 Office 2007?在我看来,情况并非如此,这意味着您需要使用与服务器上安装的版本兼容的旧库。

Does your server also have Office 2007 installed? It seems to me like that is not the case, which means you need to use older libraries that are compatible with the version installed on your server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文