Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to center text with the C# lib ClosedXML

I am trying to center text with the MVC lib ClosedXML but it only works when the cell has a background color set and I want to set the alignment without it:

var workbook = new XLWorkbook("c:\\temp\\file.xlsx");
var worksheet = workbook.Worksheet("Sheet");

worksheet .Cell(1, 1).Style.Fill.BackgroundColor = XLColor.White; // without this line it doe not work
worksheet.Cell(1, 1).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
like image 533
Patrick Avatar asked Sep 10 '25 21:09

Patrick


1 Answers

This works for me:

worksheet.Cell(1, 1).Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Center);

but as far as I know it's just a wrapper around ...Horizontal = XLAlignmentHorizontalValues.Center

like image 179
mcalex Avatar answered Sep 13 '25 11:09

mcalex