2015-07-11 9 views
10

Korzystam z poniższego kodu do aktualizacji formatu danych excel, tutaj chcę, aby nagłówek był pogrubiony i całe dane w formacie kursywy, ale po uruchomieniu kodu wszystkie funkcje w nim Wydaje się, że działa dobrze, z wyjątkiem pogrubienia i kursywy. Kod kończy także wykonywanie bez żadnych błędów, ale w pliku excel żadna z komórek nie ma danych w formacie pogrubionym lub kursywą. ustawiania/nadpisywania czcionkę po ustawić pogrubienie/kursywaPogrubienie i kursywa nie działa w programie Excel z 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(); 
     } 
    } 

Odpowiedz

13

jest problem. Wystarczy ustawić czcionkę pierwszy tak:

headerFont.SetFromFont(new Font("Times New Roman", 12)); //Do this first 
headerFont.Bold = true; 
headerFont.Italic = true; 

lub można nawet skrócić to trochę tak:

headerFont.SetFromFont(new Font("Times New Roman", 12, FontStyle.Italic | FontStyle.Bold)); 
+0

Thanks Ernie za cenne odpowiedzi. – Ash1994

Powiązane problemy