为什么我的字体和背景(前景)着色不起作用(Aspose Cells)?

在我的工作表的两个地方,我需要有白色字体和黑色背景的单元格。 在一个地方(标题行),它起作用; 在另一个(date值),它没有,我不明白我在做什么不同,会导致这种失败。

这是正在工作的代码(对于标题行):

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); } 

..这里是不工作的代码(对于在下面的屏幕截图中圈出的date行):

 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(因为它需要设置date格式):

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

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

正如你在下面看到的,在这个单元格中没有一个date(E4)。 它从一个不在表单上的地方抓取它。 但是,字体是黑色而不是白色,背景(由Aspose称为前景)是白色而不是黑色。

在这里输入图像说明

那么为什么着色不起作用? 我需要做些什么或改变,以使其在单元格E4中工作?

请注意,由于您希望将单元格着色以及字体颜色应用于所涉及的单元格,因此您需要将相应的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作为Developer Evangelist工作。

我认为你需要使相关的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担任Support developer / Evangelist。