Mask
Mask

Reputation: 657

How to get first unoccupied rows from Excel file using c#.net?

I am on a winform application which performs operations with excel files.

Now i have got an issue that i need to get first unused rows occured before a used row(edited row).

I have used the below code and which gives only the usedrange.

        string fileName = Directory.GetCurrentDirectory()+"\\123.xlsx";
        Excel.Application xlApp;
        Excel.Workbook xlWorkbook;
        xlApp = new Excel.Application();
        xlWorkbook = xlApp.Workbooks.Open(fileName);
        Excel._Worksheet xlWorksheet = (Excel._Worksheet)xlApp.Workbooks[1].Worksheets[1];
        Excel.Range excelCell = xlWorksheet.UsedRange;
        Object[,] values = (Object[,])excelCell.Value2;
        int rows = values.GetLength(0);
        int cols = values.GetLength(1);

Exmlple:

enter image description here

From the above example i need to get the result as '3' Cols and '10' rows.

Now its '3'Cols and '7'rows.

Please help me if there is any idea for this. Thank You.

Upvotes: 1

Views: 388

Answers (1)

Chris Spicer
Chris Spicer

Reputation: 2194

Quick and dirty - you could use something like the following:

int rows = excelCell.Rows.Count + excelCell.Row - 1;

And something similar for columns.

Upvotes: 1

Related Questions