Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot Set Header FontColor Of Table To Black #689

Closed
1 task done
ClemensGerstung opened this issue Feb 8, 2018 · 15 comments
Closed
1 task done

Cannot Set Header FontColor Of Table To Black #689

ClemensGerstung opened this issue Feb 8, 2018 · 15 comments

Comments

@ClemensGerstung
Copy link

ClemensGerstung commented Feb 8, 2018

Do you want to request a feature or report a bug?

  • Bug

Version of ClosedXML

0.91.0

What is the current behavior?

I have some data which are displayed as a standard table. If I want to set the FontColor of the Table's Header to Black the FontColor is still white. If I set the color to almost black (#FF010101 ) or some other color (e.g. XLColor.Red) it works as expected.

What is the expected behavior or new feature?

The FontColor of the Table's Header should be black.

Did this work in previous versions of our tool? Which versions?

No

Reproducibility

Code to reproduce problem:

class Data
{
  public int Property1 { get; set; }
  public int Property2 { get; set; }
  public int Property3 { get; set; }
  public int Property4 { get; set; }
  public int Property5 { get; set; }
}

class Program
{
  static List<Data> GenerateData()
  {
    List<Data> list = new List<Data>();
    Random r = new Random();
    for (int i = 0; i < 15; i++)
    {
      list.Add(new Data { Property1 = r.Next(0, 100), Property2 = r.Next(0, 100), Property3 = r.Next(0, 100), Property4 = r.Next(0, 100), Property5 = r.Next(0, 100) });
    }
    return list;
  }

  static void Main(string[] args)
  {
    var data = GenerateData();
    var typeAccessor = TypeAccessor.Create(typeof(Data));
    var path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.xlsx");

    using (var workbook = new XLWorkbook(XLEventTracking.Disabled))
    {
      using (var sheet = workbook.AddWorksheet("test"))
      {
        int row = 1;
        int column = 1;

        foreach (var member in typeAccessor.GetMembers())
        {
          sheet.Cell(row, column++).Value = member.Name;
        }
        row++;

        foreach (var item in data)
        {
          column = 1;
          foreach (var member in typeAccessor.GetMembers())
          {
            sheet.Cell(row, column++).Value = typeAccessor[item, member.Name];
          }
          row++;
        }

        using (var table = sheet.Range(1, 1, row - 1, column - 1).CreateTable())
        {
          using (var rangeRow = table.HeadersRow())
          {
            rangeRow.Style.Font.FontColor = XLColor.Black;  // doesn't work
            rangeRow.Style.Font.FontColor = XLColor.FromHtml("#FF010101");  // works
            rangeRow.Style.Fill.BackgroundColor = XLColor.NoColor;
          }
        }
      }
      workbook.SaveAs(path);
    }
  }
}
@igitur
Copy link
Member

igitur commented Mar 2, 2018

I can't reproduce this problem. Can you try with the latest CI build at https://ci.appveyor.com/project/Pyropace/ClosedXML/branch/develop/artifacts ?

@igitur
Copy link
Member

igitur commented Mar 2, 2018

Sidenote: you can significantly reduce your code by using the InsertTable method:

internal class Data
{
    public int Property1 { get; set; }
    public int Property2 { get; set; }
    public int Property3 { get; set; }
    public int Property4 { get; set; }
    public int Property5 { get; set; }
}

internal class Program
{
    private static List<Data> GenerateData()
    {
        List<Data> list = new List<Data>();
        Random r = new Random();
        for (int i = 0; i < 15; i++)
        {
            list.Add(new Data { Property1 = r.Next(0, 100), Property2 = r.Next(0, 100), Property3 = r.Next(0, 100), Property4 = r.Next(0, 100), Property5 = r.Next(0, 100) });
        }
        return list;
    }

    private static void Main(string[] args)
    {
        var data = GenerateData();
        var path = "test.xlsx";

        using (var workbook = new XLWorkbook(XLEventTracking.Disabled))
        {
            var table = workbook.AddWorksheet("Sheet1").FirstCell().InsertTable(data);

            using (var rangeRow = table.HeadersRow())
            {
                rangeRow.Style.Font.FontColor = XLColor.Blue;  
                rangeRow.Style.Fill.BackgroundColor = XLColor.NoColor;
            }

            workbook.SaveAs(path);
        }
    }
}

@ClemensGerstung
Copy link
Author

ClemensGerstung commented Mar 2, 2018

I still have the same problem, even with the latest CI build (see attached files).
Also tested this with Office 2010, 2013 and 2016 (mainly 2016) on different PCs, always the same behavior.

I'm using the "long" way because in the actual application the properties have to be exported in a special order and some may get concatenated with others (this structure is defined in an XML file). So I can't use InsertTable(...) function.

EDIT: I might use InsertTable(...) and requery the data with LINQ into a anonymous class (as you did in your sample) but this wouldn't make much difference to the actual code.

test_FF010101.xlsx
test_Black.xlsx

@igitur
Copy link
Member

igitur commented Mar 2, 2018

Ok, can you run my code sample as is and attach the output file that it generates?

@igitur
Copy link
Member

igitur commented Mar 2, 2018

PS: Thanks for the good code sample. It makes it a lot easier, even though I can't replicate the problem... yet.

@ClemensGerstung
Copy link
Author

ClemensGerstung commented Mar 2, 2018

Your sample (looks good): test.xlsx

@ClemensGerstung
Copy link
Author

also tested with setting the FontColor = XLColor.Black. Still same problem
test_black.xlsx

@igitur
Copy link
Member

igitur commented Mar 2, 2018

If I open test.xlsx I see this:
image

@igitur
Copy link
Member

igitur commented Mar 2, 2018

Ah, but if I change it to XLColor.Black, I see this. This is obviously wrong. I'll look into it.

image

@igitur
Copy link
Member

igitur commented Mar 2, 2018

Hmm, I think this is an Excel bug! Open one of those files and try setting the colour manually to black. Don't use theme colours. Use actual RGB:

image

If I do that, the text becomes transparent. What happens for you?

@ClemensGerstung
Copy link
Author

ClemensGerstung commented Mar 2, 2018

I have the exact same behaviour.
Text is getting transparent if I set the color to black via RGB

@igitur
Copy link
Member

igitur commented Mar 2, 2018

You could try rangeRow.Style.Font.FontColor = XLColor.FromTheme(XLThemeColor.Text1);.

@ClemensGerstung
Copy link
Author

This works, it sets the FontColor to Black (as expected).

@igitur
Copy link
Member

igitur commented Mar 14, 2018

Can this be closed?

@ClemensGerstung
Copy link
Author

Yes, it's not a bug in ClosedXML.

@igitur igitur closed this as completed Mar 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants