connection with Excel
I'm trying to get dynamic connection from external file with excel vi C# My 开发者_运维问答code is
Excel.connection objconn;
objconn=null;
Excel.workconnection wc=objconn.AddfromFile("C:\\test.odc");
when i do debuging the compiler give me exception Object reference not set to an instance an object on the last statement please help me
That is because your object is null. You actuallty set it to null explicitly in the preceding line.
If Excel.connection is a class with a default constructor, you can do:
objconn = new Excel.connection();
(Otherwise, modify appropiately to match the correct constructor).
//Create a new Excel object
var app = new Microsoft.Office.Interop.Excel.Application();
//Grab a reference to the current WorkBook
var WB = app.ThisWorkbook;
//Add a connection to the WorkBook
WB.Connections.AddFromFile("C:\\test.odc");
you set objconn to null , so it's normal to have Object reference not set to an instance error when executing objconn.AddfromFile("C:\\test.odc");
精彩评论