粗体和斜体在 excel 中与 EPPLUS 不兼容
Bold and Italics not working in excel with EPPLUS
我正在使用下面的代码更新 excel 数据格式,这里我希望标题为粗体,整个数据为斜体格式,但是当我 运行 代码中的所有功能时除了粗体和斜体,它似乎工作正常。代码也完成了执行,没有任何错误,但在 excel 文件中 none 的单元格的数据为粗体或斜体格式。
public void FormatExcel()
{
string currentDate = DateTime.Now.ToString("yyyyMMdd");
FileInfo File = new FileInfo("G:\Selenium\Test66.xlsx");
using (ExcelPackage excel = new ExcelPackage(File))
{
ExcelWorksheet worksheet = excel.Workbook.Worksheets[currentDate];
int totalRows = worksheet.Dimension.End.Row;
int totalCols = worksheet.Dimension.End.Column;
var headerCells = worksheet.Cells[1, 1, 1, totalCols];
var headerFont = headerCells.Style.Font;
headerFont.Bold = true;
headerFont.Italic = true;
headerFont.SetFromFont(new Font("Times New Roman", 12));
headerFont.Color.SetColor(Color.DarkBlue);
var headerFill = headerCells.Style.Fill;
headerFill.PatternType = ExcelFillStyle.Solid;
headerFill.BackgroundColor.SetColor(Color.Gray);
var dataCells = worksheet.Cells[2, 1, totalRows, totalCols];
var dataFont = dataCells.Style.Font;
dataFont.Italic = true;
dataFont.SetFromFont(new Font("Times New Roman", 10));
dataFont.Color.SetColor(Color.DarkBlue);
var dataFill = dataCells.Style.Fill;
dataFill.PatternType = ExcelFillStyle.Solid;
dataFill.BackgroundColor.SetColor(Color.Silver);
var allCells = worksheet.Cells[1, 1, totalRows, totalCols];
allCells.AutoFitColumns();
allCells.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
var border = allCells.Style.Border;
border.Top.Style = border.Left.Style = border.Bottom.Style = border.Right.Style = ExcelBorderStyle.Thin;
excel.Save();
}
}
问题是您 setting/overwriting 字体 在 您设置 bold/italic 之后。只需像这样先设置字体:
headerFont.SetFromFont(new Font("Times New Roman", 12)); //Do this first
headerFont.Bold = true;
headerFont.Italic = true;
或者你甚至可以像这样缩短它:
headerFont.SetFromFont(new Font("Times New Roman", 12, FontStyle.Italic | FontStyle.Bold));
headerFont.SetFromFont("Times New Roman", 16, true);
我正在使用下面的代码更新 excel 数据格式,这里我希望标题为粗体,整个数据为斜体格式,但是当我 运行 代码中的所有功能时除了粗体和斜体,它似乎工作正常。代码也完成了执行,没有任何错误,但在 excel 文件中 none 的单元格的数据为粗体或斜体格式。
public void FormatExcel()
{
string currentDate = DateTime.Now.ToString("yyyyMMdd");
FileInfo File = new FileInfo("G:\Selenium\Test66.xlsx");
using (ExcelPackage excel = new ExcelPackage(File))
{
ExcelWorksheet worksheet = excel.Workbook.Worksheets[currentDate];
int totalRows = worksheet.Dimension.End.Row;
int totalCols = worksheet.Dimension.End.Column;
var headerCells = worksheet.Cells[1, 1, 1, totalCols];
var headerFont = headerCells.Style.Font;
headerFont.Bold = true;
headerFont.Italic = true;
headerFont.SetFromFont(new Font("Times New Roman", 12));
headerFont.Color.SetColor(Color.DarkBlue);
var headerFill = headerCells.Style.Fill;
headerFill.PatternType = ExcelFillStyle.Solid;
headerFill.BackgroundColor.SetColor(Color.Gray);
var dataCells = worksheet.Cells[2, 1, totalRows, totalCols];
var dataFont = dataCells.Style.Font;
dataFont.Italic = true;
dataFont.SetFromFont(new Font("Times New Roman", 10));
dataFont.Color.SetColor(Color.DarkBlue);
var dataFill = dataCells.Style.Fill;
dataFill.PatternType = ExcelFillStyle.Solid;
dataFill.BackgroundColor.SetColor(Color.Silver);
var allCells = worksheet.Cells[1, 1, totalRows, totalCols];
allCells.AutoFitColumns();
allCells.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
var border = allCells.Style.Border;
border.Top.Style = border.Left.Style = border.Bottom.Style = border.Right.Style = ExcelBorderStyle.Thin;
excel.Save();
}
}
问题是您 setting/overwriting 字体 在 您设置 bold/italic 之后。只需像这样先设置字体:
headerFont.SetFromFont(new Font("Times New Roman", 12)); //Do this first
headerFont.Bold = true;
headerFont.Italic = true;
或者你甚至可以像这样缩短它:
headerFont.SetFromFont(new Font("Times New Roman", 12, FontStyle.Italic | FontStyle.Bold));
headerFont.SetFromFont("Times New Roman", 16, true);