This project has moved. For the latest updates, please go here.

How to open and read excel file

Oct 26, 2011 at 8:01 PM

Please
Does anyone have an example of how open and read a excel file with NetOffice?

Coordinator
Oct 26, 2011 at 10:45 PM
Edited Oct 26, 2011 at 10:57 PM

hello

// this example in C# open a workbook and write the cell content from cell [5,2] from the first worksheet to console

Excel.Workbook  book  = _excelApplication.Workbooks.Open("C:\\myExcelFile.xlsx");
Excel.Worksheet sheet = book.Worksheets[1] as Excel.Worksheet; // index base in all office products is 1 and not 0
Excel.Range     range = sheet.Cells[5, 2];
Console.WriteLine(range.Value);

you find various excel examples C#/VB in  the examples folder and online.
http://netoffice.codeplex.com/wikipage?title=Excel_ExampleOverview

*with best regards
Sebastian

Mar 7, 2012 at 5:06 AM
SebastianDotNet wrote:

hello

// this example in C# open a workbook and write the cell content from cell [5,2] from the first worksheet to console

Excel.Workbook  book  = _excelApplication.Workbooks.Open("C:\\myExcelFile.xlsx");
Excel.Worksheet sheet = book.Worksheets[1] as Excel.Worksheet; // index base in all office products is 1 and not 0
Excel.Range     range = sheet.Cells[5, 2];
Console.WriteLine(range.Value);

you find various excel examples C#/VB in  the examples folder and online.
http://netoffice.codeplex.com/wikipage?title=Excel_ExampleOverview

*with best regards
Sebastian


Thank sebastian for such a great helper.... i would like to ask how do i read file excel with a multiple range rows and column, i know the column , example i want to read column like : range = sheet.get_Range("$A6:$J6");  but i don't know how to read a dynamic row.

My question, how do i read dynamic row if theirs multiple value/multiple row which is don't know how many rows will have data inside the rows.

I mean the row could be 5 row, but could be 10 row, dynamic value from excel, but how do i loop to get those values. Thank before for you contribution of this great netoffice

 

 

Coordinator
Mar 7, 2012 at 10:31 AM

something like that?

 Excel.Worksheet workSheet = (Excel.Worksheet)workBook.Worksheets[1];

 // fetch used range, means top-left to right-bottom corner from content
 foreach (Excel.Range cell in workSheet.UsedRange)
 {
   int rowIndex = cell.Row;
   int columnIndex = cell.Column;

   Console.WriteLine("This cell is in row {0} and column {1}", rowIndex, columnIndex);
 }

 // get a specific row ( the third in this example )
 Excel.Range row = workSheet.Rows[3, Type.Missing] as Excel.Range;

 // get all cells from the row
 Excel.Range rowCells = row.Cells;

 // get a specific cell via x,y coordinates
 Excel.Range specificCell = workSheet.Cells[3, 5] as Excel.Range;  // 3,5 is $E3 

 Console.WriteLine("This adress of specificCell is {0}", specificCell.Address(0, 0));