【问题标题】:Why is my font and background (foreground) colorization not working here (Aspose Cells)?为什么我的字体和背景(前景)着色在这里不起作用(Aspose Cells)?
【发布时间】:2017-01-27 23:37:25
【问题描述】:

在我的工作表上的两个地方,我需要具有白色字体和黑色背景的单元格。在一个地方(标题行),它可以工作;在另一个(日期值)中,它没有,而且我看不出我正在做的不同会导致此失败。

这是有效的代码(用于标题行):

CellsFactory cfHeaderRow = new CellsFactory();
Cell headerRowCell;
Style styleHeaderRow;
for (int x = 0; x < drPrices.FieldCount-1; x++)
{
    headerRowCell = pricePushSheet.Cells[6, x]; 
    headerRowCell.PutValue(drPrices.GetName(x));
    pricePushSheet.Cells.SetColumnWidth(x, 9); 
    styleHeaderRow = cfHeaderRow.CreateStyle();
    styleHeaderRow.HorizontalAlignment = TextAlignmentType.Center;
    styleHeaderRow.Font.Color = Color.White;
    styleHeaderRow.ForegroundColor = Color.Black;
    styleHeaderRow.Pattern = BackgroundType.Solid;
    styleHeaderRow.IsTextWrapped = true;
    headerRowCell.SetStyle(styleHeaderRow);
}

..这是不起作用的代码(对于下面屏幕截图中圈出的日期行):

CellsFactory cfDate = new CellsFactory();
Cell dateCell = pricePushSheet.Cells[3, 4];
Style styleDate = cfDate.CreateStyle();
dateCell.PutValue(pricePushSheet.Cells[7, 1]);
StyleFlag flag2 = new StyleFlag();
flag2.NumberFormat = true;
styleDate.Font.IsBold = true;
styleDate.HorizontalAlignment = TextAlignmentType.Center;
styleDate.Font.Color = Color.White;
styleDate.ForegroundColor = Color.Black;
styleDate.Pattern = BackgroundType.Solid;
styleDate.Custom = "mm/dd/yyyy";
dateCell.SetStyle(styleDate, flag2);

因此,在非工作代码中似乎不同(可能很重要)的是它使用了 StyleFlag(因为它需要设置日期格式):

StyleFlag flag2 = new StyleFlag();
flag2.NumberFormat = true;
styleDate.Custom = "mm/dd/yyyy";

...当然,SetStyle 将标志作为第二个参数(代码的工作位不需要标志,因为它没有做任何花哨的事情)。

如下所示,相关单元格中没有日期 (E4);它从尚未出现在工作表上的地方抓取它。但事实上字体是黑色而不是白色,背景(Aspose 称为前景)是白色而不是黑色。

那么为什么着色不起作用?我需要做什么或更改才能使其在单元格 E4 中工作?

【问题讨论】:

    标签: excel aspose aspose-cells background-foreground


    【解决方案1】:

    请注意,由于您希望将单元格阴影和字体颜色应用于相关单元格,因此您需要将相应的 StyleFlag 属性设置为 true,以便上述样式方面生效。请检查以下产生预期结果的代码。

    var workbook = new Workbook(dir + "book1.xlsx");
    var pricePushSheet = workbook.Worksheets[0];
    var cfDate = new CellsFactory();
    var dateCell = pricePushSheet.Cells[3, 4];
    var styleDate = cfDate.CreateStyle();
    dateCell.PutValue(pricePushSheet.Cells[7, 1].Value);
    var flag2 = new StyleFlag();
    flag2.NumberFormat = true;
    flag2.CellShading = true;
    flag2.Font = true;
    styleDate.Font.IsBold = true;
    styleDate.HorizontalAlignment = TextAlignmentType.Center;
    styleDate.Font.Color = Color.White;
    styleDate.ForegroundColor = Color.Black;
    styleDate.Pattern = Aspose.Cells.BackgroundType.Solid;
    styleDate.Custom = "mm/dd/yyyy";
    dateCell.SetStyle(styleDate, flag2);
    

    注意:我在 Aspose 担任开发人员宣传员。

    【讨论】:

    • 什么是“flag2.Font = true;”是什么意思?
    • 嗯,它将所有与字体相关的选项(字体下的属性(子),例如IsBold,Color等)指定为“true”。
    【解决方案2】:

    我认为您需要启用相关的 StyleFlag 选项才能对单元格应用正确的格式。请参阅更新的代码段供您参考: 例如 示例代码:

    CellsFactory cfDate = new CellsFactory();
    Cell dateCell = pricePushSheet.Cells[3, 4];
    Style styleDate = cfDate.CreateStyle();
    dateCell.PutValue(pricePushSheet.Cells[7, 1]);
    styleDate.Font.IsBold = true;
    styleDate.HorizontalAlignment = TextAlignmentType.Center;
    styleDate.Font.Color = Color.White;
    styleDate.ForegroundColor = Color.Black;
    styleDate.Pattern = BackgroundType.Solid;
    styleDate.Custom = "mm/dd/yyyy";
    StyleFlag flag2 = new StyleFlag();
    flag2.NumberFormat = true;
    flag2.CellShading = true;
    flag2.HorizontalAlignment = true;
    flag2.FontColor = true;
    flag2.FontBold = true;
    dateCell.SetStyle(styleDate, flag2);
    

    我在 Aspose 担任支持开发人员/宣传员。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-07
      • 2016-04-14
      • 1970-01-01
      • 2014-09-08
      • 2022-08-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多