Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# how to iterate over excel columns

Tags:

c#

excel

I want to get a specific column of an excel sheet and then iterate through it's cells. I want it to look something like this:

Excel.Workbook workbook = app.Workbooks.Open(svDropPath);
Excel.Worksheet xlWorkSheet = (Excel.Worksheet)workbook.Sheets["Sheet Name"];
var col = xlWorkSheet.UsedRange.Columns["C:C", Type.Missing]; // I want the 3rd column
foreach(Cell c in col)
....

How do I actually make this foreach loop?

like image 309
Yonatan Nir Avatar asked Oct 24 '25 15:10

Yonatan Nir


1 Answers

Your loop will looks as follow:

    foreach (Excel.Range item in col.Cells)
    {
        //whatever you want to do with your cells, here- msgbox of cells value
        MessageBox.Show(Convert.ToString(item.Value));
    }
like image 162
Kazimierz Jawor Avatar answered Oct 26 '25 06:10

Kazimierz Jawor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!