我已经在 EPPlus 版本 3.1.3.0 和最新的 4.0.4.0 中尝试过,两者都表现出相同的行为。
我正在尝试将单元格中的文本居中对齐,但它不起作用。单元格中的数字可以正常工作,而字符串则不行。以下是无法生成所需 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 be left-aligned";
workSheet.Cells["A1"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;
workSheet.Cells["A2"].Value = "This should be center-aligned";
workSheet.Cells["A2"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; // <- This doesn't work.
workSheet.Cells["A3"].RichText.Add("This should be center-aligned");
workSheet.Cells["A3"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; // <- This doesn't work.
workSheet.Cells["A4"].Value = "This should be right-aligned";
workSheet.Cells["A4"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Right; // <- This doesn't work.
workSheet.Cells["A5"].RichText.Add("This should be right-aligned");
workSheet.Cells["A5"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Right; // <- This doesn't work.
//workSheet.Cells["A2:A3"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; // <- This doesn't work.
//workSheet.Cells["A4:A5"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; // <- This doesn't work.
package.Save();
}
这让我有点疯狂。任何想法为什么字符串不能对齐?