c# - EPPlus isn't honoring ExcelHorizontalAlignment Center or Right -
i've tried in epplus versions 3.1.3.0 , latest 4.0.4.0 , both exhibiting same behavior.
i'm trying center align text in cells it's not working. numbers in cells work fine, strings not. following example of code fails produce desired excelhorizontalalignment:
var newfile = new fileinfo(@"c:\temp\sample.xlsx"); using (var package = new excelpackage(newfile)) { var worksheet = package.workbook.worksheets.add("content"); worksheet.column(1).width = 50; worksheet.cells["a1"].value = "this should left-aligned"; worksheet.cells["a1"].style.horizontalalignment = excelhorizontalalignment.left; worksheet.cells["a2"].value = "this should center-aligned"; worksheet.cells["a2"].style.horizontalalignment = excelhorizontalalignment.center; // <- doesn't work. worksheet.cells["a3"].richtext.add("this should center-aligned"); worksheet.cells["a3"].style.horizontalalignment = excelhorizontalalignment.center; // <- doesn't work. worksheet.cells["a4"].value = "this should right-aligned"; worksheet.cells["a4"].style.horizontalalignment = excelhorizontalalignment.right; // <- doesn't work. worksheet.cells["a5"].richtext.add("this should right-aligned"); worksheet.cells["a5"].style.horizontalalignment = excelhorizontalalignment.right; // <- doesn't work. //worksheet.cells["a2:a3"].style.horizontalalignment = excelhorizontalalignment.center; // <- doesn't work. //worksheet.cells["a4:a5"].style.horizontalalignment = excelhorizontalalignment.center; // <- doesn't work. package.save(); }
it's making me little crazy. ideas why strings cannot aligned?
it seems though libreoffice partially @ fault. if generate file epplus, , open in calc, center , right alignment not displayed. if open file in epplus, formatting general.
if, however, generate file using epplus , read in epplus, alignment specified.
i have solution works in calc , excel. if create namedstyle, , apply cells or cell-ranges, works expected.
var newfile = new fileinfo(filepath); using (var package = new excelpackage(newfile)) { var worksheet = package.workbook.worksheets.add("content"); worksheet.column(1).width = 50; worksheet.cells["a1"].value = "this should left-aligned"; worksheet.cells["a1"].style.horizontalalignment = excelhorizontalalignment.left; var centerstyle = package.workbook.styles.createnamedstyle("center"); centerstyle.style.horizontalalignment = excelhorizontalalignment.center; worksheet.cells["a2"].value = "this should center-aligned"; worksheet.cells["a3"].richtext.add("this should center-aligned"); worksheet.cells["a2:a3"].stylename = "center"; var rightstyle = package.workbook.styles.createnamedstyle("right"); rightstyle.style.horizontalalignment = excelhorizontalalignment.right; worksheet.cells["a4"].value = "this should right-aligned"; worksheet.cells["a5"].richtext.add("this should right-aligned"); worksheet.cells["a4:a5"].stylename = "right"; package.save(); }
Comments
Post a Comment