我正在嘗試使用 Microsoft.Office.Interop.Excel 將我的類屬性值寫入 Excel 單元格,但出現以下錯誤: System.Runtime.InteropServices.InvalidComObjectException
這是我的代碼:
...
private Microsoft.Office.Interop.Excel.Application xlapp = new Microsoft.Office.Interop.Excel.Application();
...
//open excel file
Workbook wbAngebot = xlapp.Workbooks.Open(abs); //abs = absolute path to file
//assign worksheet
Worksheet wsAngebotsinfo = wbAngebot.Worksheets["Angebotsinformation"];
//write value to cell
wsAngebotsinfo.Cells[1,1] = AnredeVerk; //--> causes the exception, AnredeVerk is a string property
AskToUpdateLinks 或 ScreenUpdating 或 Visible xlapp-properties 在設定為 false 時是否可能導致錯誤?我也嘗試過wsAngebotsinfo.Range["A1"].Value,Value2但出現同樣的錯誤。任何想法為什么會發生這種情況?
uj5u.com熱心網友回復:
這就是我使用 excel interop 設定單元格值的方式。
// Get the range object that represents the cell
//
var cell = (Range)worksheet.Cells[row, col];
// Set the cell value
//
cell.Value = value;
// Release the com object
//
Marshal.ReleaseComObject(cell);
重要的是要記住釋放您訪問的所有物件。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/467196.html
