Excel Automation in C#
-
I want to open an Excel file in my C# application and read the contents . I tried the msdn samples. But most of them deal with writing back values into Excel file. Also since the range of values that I have to read from the Excel file is dynamic, I would like to know how this can be done. The foll. is the code that I have written and I get error while instatiating an Excel aplication obejct as it is not able to understand my typecasting. Excel.Application objApp=null ; Excel.Workbooks objBooks=null; Excel.Workbook objBook= null; Excel.Range range; string str=""; try { // Instantiate Excel and start a new workbook. objApp = (Excel.Application)new Excel._ExcelApplicationClass(); objBooks = (Workbooks)objApp.Workbooks(1); objBook = (Workbook)objBooks.Open(DataFile,null,null,null,null,null,null,null,null,null,null,null); Excel.Worksheet ws= (Worksheet)objBook.Worksheets(1); if (ws == null) Console.WriteLine ("ERROR: worksheet == null"); Excel.Range rng=(Excel.Range)ws.Cells(1,1); str=rng.Value.ToString();