开发者

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");

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜