Click here to Skip to main content
14,971,296 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Paul,

I've try to use your great code "C# How To Read .xlsx Excel File With 3 Lines of Code" to extract content cells of a simple sheet.

My problem is I can retrieve the column number with cell.ColumnIndex and cell content with cell.Text, but I don't know how to get the row number of the cell, So I can't extract the data successfully.

Can you lend me a hand?

Regards and thanks in advance

David Luque

What I have tried:

long MaxCol = 0;
            long MaxRow = 0;

            foreach (var worksheet in Workbook.Worksheets(@"T:\Libro1.xlsx"))
            {
                MaxCol = worksheet.MaxColumnIndex;
                foreach (var row in worksheet.Rows)
                {
                    foreach (var cell in row.Cells)
                        if (cell != null)
                        {                            
                            MessageBox.Show("Column: " + cell.ColumnIndex + "\nContent: " + cell.Text);
                        }
Posted
Updated 14-Jun-21 1:19am
Comments
Richard Deeming 26-Mar-19 15:49pm
   
If your question is about the code in an article, you should post it in the forum at the bottom of that article:
C# How To Read .xlsx Excel File With 3 Lines of Code[^]

Quote:
My problem is I can retrieve the column number with cell.ColumnIndex and cell content with cell.Text, but I don't know how to get the row number of the cell, So I can't extract the data successfully.

Did you tried Cell.RowIndex ?
[Update]
Quote:
There is no RowIndex in Cell, there is only ColumnIndex.

Then try with cell.Row and cell.Column.
   
v2
Comments
TassadarNET 25-Mar-19 17:38pm
   
Hi Patrice,

Many thanks for your answer. There is no RowIndex in Cell, there is only ColumnIndex.

I've try to count the rows in the foreach look, of course it works since it's counting the analyzed rows, but it I have a complete row in blank this row is not counted and all the rest fails.

Now I'm trying to use NPOI and it seems to be a good solution for my need, I've create a function that makes a new .xlsx file, now I'm trying to find out how to read an existent .xlsx

Regards
Then try with cell.Row and cell.Column.

There is no cell.Column, but many thanks for the try. I've finally use NPOI and it already works

Regards
   
Comments
Member 15245413 14-Jun-21 7:22am
   
Can You provide me your code once and explain NPOI
Then try with cell.Row and cell.Column.

There is no cell.Column, but many thanks for the try. I've finally use NPOI and it already works

Regards


Can U give me solution of yours?
   

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