...
Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
oXL = new Excel.Application();
oWB = (Excel._Workbook)oXL.ActiveWorkbook;
oSheet = (Excel._Worksheet)oWB.Sheets[1];
oSheet.Cells[5,10] = "Value";
...
在崩溃时产生这个:
Unhandled Exception: System.NullReferenceException: Object reference not set to
an instance of an object.
at ConsoleApplication1.Program.Main(String[] args) in C:\Wherever\Visual Studio 2008\Projects\ConsoleApplication20\ConsoleApplication20\Program.
cs:line 60
在这种情况下,第 60 行是
oSheet = (Excel._Worksheet)oWB.Sheets[1];
如果该行被写入,也会发生同样的事情
oSheet = (Excel._Worksheet)oWB.ActiveSheet;
.
Excel 当时已经在屏幕上打开,并有一个新的工作表。