This code shows how to retrive values from an Excel file
Namesspaces to import : using System; using System.Data; using Excel;
References to ADD: Microsoft.Office.Core
static void Main(string[] args) { //Declarations Excel.Application oXL; Excel._Workbook oWB; Excel._Worksheet oSheet; Excel.Range oRng;
try { //Start Excel and get Application object. oXL = new Excel.Application();
//Get a new workbook. int index = 0; object rowIndex = 2; object colIndex1 = 1; object colIndex2 = 2;
oWB = (Excel._Workbook)(oXL.Workbooks.Add("c:/Test.xls")); // Gets the Workbook oSheet = (Excel._Worksheet)oWB.Sheets[1]; // Gets the first Sheet of the workbook while ( ((Excel.Range)oSheet.Cells[rowIndex,colIndex1]).Value2 != null ) { rowIndex = 2+index; string firstName = ((Excel.Range)oSheet.Cells[rowIndex,colIndex1]).Value2.ToString(); // retrives the firstColumn Value string lastName = ((Excel.Range)oSheet.Cells[rowIndex,colIndex2]).Value2.ToString(); // retrives the secondColumn Value Console.WriteLine("Name : {0},{1} ",firstName,lastName); index++; } } catch(Exception ex) { ex.Message.ToString(); } }
|
No responses found. Be the first to respond and make money from revenue sharing program.
|