Click here to Skip to main content
15,918,178 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to read the multiple Excel Rows and columns data. Currently i am only able to read first row and column data(A, 12) without any loop. While iterating i am not able to get the data of multiple rows and column at the same time. Below i have mentioned the excel like view. I need to get the data (a,12) and store them into two separate variables. So, that i can use them further for loading.

Col A Col B
Username Password

Row1. a 12

Row2. b 34

What I have tried:

<pre> int rCnt;
            int cCnt;
            string workbookPath = "f:\\Test\\TestData.xlsx";

            Excel.Application excelApp;
            Excel.Workbook excelWorkbook;
            Excel.Sheets excelSheets;
            Excel.Worksheet excelWorksheet;
            Excel.Range range;

            string currentSheet = "DataSet";
            excelApp = new Excel.Application();
            excelWorkbook = excelApp.Workbooks.Add(workbookPath);
            excelSheets = excelWorkbook.Sheets;
            excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);
            range = excelWorksheet.UsedRange;            

for (rCnt = 2; rCnt <= range.Rows.Count; rCnt++)
                for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
                    Loginname = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value;
                    Password = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value;
Updated 16-Feb-17 0:48am

1 solution

See my solution to this question: How to read columns one by one in .xlsx file through C#[^] - it's probably a class mate of yours...
Share this answer
Muhammad Islam Manzoor 16-Feb-17 6:56am    
For the link provided by you its only for one column reading. i need to read the data from two columns and from multiple rows i mean to say the combination of username and password from two column and after that shifting towards next combination that is a new row after that i would like to store them in separate variables.
Muhammad Islam Manzoor 17-Feb-17 2:32am    
Any updates?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900