10
我正在使用下面的代碼來更新excel數據格式,在這裏我希望標題以粗體顯示,整個數據以斜體顯示,但是當我運行代碼中的所有功能時似乎工作很好,除了粗體和斜體。代碼也可以在沒有任何錯誤的情況下完成執行,但在excel文件中,沒有任何單元格使用粗體或斜體格式的數據。粗體和斜體不能在excel中工作EPPLUS
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();
}
}
厄尼感謝您的寶貴答覆。 – Ash1994