Donate

Excel Interop Get Last Row Logic Conversion To ClosedXML

Hello,

I was assigned to convert a VB.NET project that will generate an excel file using Excel COM interop into C# with ClosedXML as the tool to produce the report. When the project was finished coding and proceed to testing, the logic to get the last row isn't working and throws "Object Reference Not Set To An Instance" error. The VB.NET logic that I need to convert to ClosedXML is presented below.
Private Function GetLastRow() As Int32
     xlRange = xlWSheet.Range("A65536").End(Excel.XlDirection.xlUp)
     GetLastRow = xlRange.Row() 
     xlRange = Nothing
End Function
After reading the ClosedXML documentation and doing some debugging stuff, I successfully converted the Excel Interop function to ClosedXML counterpart. Below is the function that returns the last row used. All I need to do is to check first if last row used is null, then return row one. Otherwise, return the succeeding rows.
private int GetLastRow(IXLWorksheet xlWSheet)
{
	if (xlWSheet.LastRowUsed() == null)
		return 1;

	return xlWSheet.LastRowUsed().RowNumber();
}


Cheers!

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

TypeScript Error Or Bug: The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program.