Search code examples
excelepplus

Default text color in an Excel document


I created an Excel document with some c# code (see below, but this code is not part of my question).

  • The color of the text in cell "A2" is white.

  • If i choose 'Format Cells' on "A2" is shows that this cell has Color: 'Automatic'.

  • The (commented line) sheet.Cells[2, 1].Value = "test"; does create a black piece of text 'test'.

enter image description here

Where in Excel (2013) can i change the color of this cell, in such a way that my next created document has a correct (=black) text color on this link?

The file myWorkbook.xlsx is available (if needed)

C# code:

  using OfficeOpenXml;
  using OfficeOpenXml.Style;
  public static void CreateExcelDoc()
  {
       if (File.Exists(@"d:\temp\myWorkbook.xlsx")) { File.Delete(@"d:\temp\myWorkbook.xlsx"); }
       var file = new FileInfo(@"d:\temp\myWorkbook.xlsx");
       using (var package = new ExcelPackage(file))
       {
           var sheet = package.Workbook.Worksheets.Add("My Sheet");
           sheet.Cells["A1"].Value = "Hello World! Hello World! Hello World! Hello World! ";
           sheet.Cells["A1"].Style.WrapText = true;
           sheet.Cells["A1"].Style.VerticalAlignment = ExcelVerticalAlignment.Top;
           sheet.Column(1).Width = 80;

           sheet.Cells[2, 1].Hyperlink = new ExcelHyperLink("http://www.google.com/", UriKind.Absolute);
           //sheet.Cells[2, 1].Value = "test";
           sheet.Cells["B2"].Value = "test";

           // Save to file
           package.Save();
       }
  }

Solution

  • I overlooked the fact that there is no text in cell "A2", adding a text, solving my own question:

    sheet.Cells[2, 1].Hyperlink = 
       new ExcelHyperLink("http://www.google.com/", UriKind.Absolute) 
             { Display="Google" };