Posts

Showing posts with the label ClosedXML

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)...

How To Add Cell Borders Or Grid Lines To Filled Cells In Excel Using ClosedXML

Image
Hello, When dealing with filled cells or columns of an Excel workshet via ClosedXML, you may notice that after you produced the Excel file(s), the filled columns or cells don't have borders. This assume that your report does not apply borders to the used cells or columns such as the image below. To apply borders to certain columns within a range or not, you need to set both the Outside and Inside borders with Thin borderstyle. And then apply your preferred color to the top, bottom, left and right borders. row++; xlWSheet.Range( 1 , 1 , row, 7 ).Style.Border.SetOutsideBorder(XLBorderStyleValues.Thin) .Border.SetInsideBorder(XLBorderStyleValues.Thin) .Border.SetTopBorderColor(XLColor.LightGray) .Border.SetBottomBorderColor(XLColor.LightGray) .Border.SetLeftBorderColor(XLColor.LightGray) .Border.SetRightBorderColor(XLColor.LightGray); Filled columns or cells with borders. Cheers!

Donate