How to get the number of columns in a worksheet

Hi - sorry for such a lame, newbie question, but your forum search feature doesn’t work. I’m trying to determine the number of Rows and Columns in worksheet, but I can’t seem to get the Columns. Here’s a code snippet. I’m able to open the file successfully and read the values and the value for Rows is correct, but the value for Columns is always 0.

license = new Aspose.Cells.License();
license.SetLicense(licenseFilePath);

loadOptions = new LoadOptions(LoadFormat.TabDelimited);

workbook = new Workbook(inputFilePath, loadOptions);

worksheet = workbook.Worksheets[0];

rows = worksheet.Cells.Rows.Count;
columns = worksheet.Cells.Columns.Count;

for (row = 0, column = 0; column < columns; column++)
{
cell = worksheet.Cells[row, column];
value = cell.Value.ToString();
}

Thanks for your help.

Hi,

Thanks for your posting and using Aspose.Cells.

The following code finds the number of columns and rows inside the worksheet. It first finds the last cell of the worksheet and then it prints the total number of columns and rows.

I have attached the sample excel file used in this code and the console output of the code for your reference.

C#
//Load your excel file
Workbook wb = new Workbook(“sample.xlsx”);

//Access the first worksheet
Worksheet ws = wb.Worksheets[0];

//Access last cell inside the worksheet
Cell cell = ws.Cells.LastCell;

//Number of columns inside the worksheet
Console.WriteLine("Number of Columns: " + (cell.Column + 1));

//Number of rows inside the worksheet
Console.WriteLine("Number of Rows: " + (cell.Row + 1));

Console Output
Number of Columns: 8
Number of Rows: 13