我想打開和Excel 2000文件。當我在Excel 2007中試用時,它工作正常,但是當我在開發服務器上檢查它時,它無法正常工作。打開excel文件時發生錯誤。我正在使用下面的代碼。System.AccessViolationException:試圖讀取或寫入受保護的內存
// 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(String Filename,Object Upda teLinks,Object ReadOnly,Object Format,Object Password,Object WriteResPasswor d,Object IgnoreReadOnlyRecommended,Object Origin, Object Delimit呃,對象艾迪表,對象通知,目標轉換器, 對象AddToMru,在 TestExcel.Program.Main(字串[] args對象本地,物CO rruptLoad))」