为什么此代码不设置单元格的背景颜色(Aspose Cells)?

时间:2017-01-13 22:40:44

标签: aspose aspose-cells

我需要一个细胞有浅蓝色背景色;我有在另一个工作表中工作的代码,但相同的代码(除了vars和列索引val的名称):

Style styleShipVariance = null;
CellsFactory cfShipVariance = new CellsFactory();
Cell ShipVarianceCell = customerWorksheet.Cells[0, SHIPVARIANCE_COL];
ShipVarianceCell.PutValue("Ship Variance");
styleShipVariance = cfShipVariance.CreateStyle();
styleShipVariance.HorizontalAlignment = TextAlignmentType.Left;
styleShipVariance.Font.Name = fontForSheets;
styleShipVariance.Font.IsBold = true;
styleShipVariance.Font.Size = 11;
styleShipVariance.ForegroundColor = Color.LightBlue;
styleShipVariance.Pattern = BackgroundType.Solid;
ShipVarianceCell.SetStyle(styleShipVariance);

...不适用于其他工作表。值(" Ship Variance")显示在该单元格中,但颜色未应用。

为什么这不起作用?缺少什么?

注意:我也尝试添加:

var flag = new StyleFlag
{
    CellShading = true,
    FontName = true,
    FontSize = true,
    FontColor = true,
    FontBold = true,
    NumberFormat = true
};

...并将上面的最后一行更改为:

ShipVarianceCell.SetStyle(styleShipVariance, flag);

......但没有任何区别。

更新

我保存为xlsx:

var filename = String.Format(@"{0}\{1} - Fill Rate - {2}.xlsx", sharedFolder, _unit, fromAsYYYYMMDD);
if (File.Exists(filename))
{
    File.Delete(filename);
}
workBook.Save(filename, SaveFormat.Xlsx);

更新2

我只是注意到vals也没有加粗或左对齐。正在分配文本值本身,但没有格式化...

更新3

为什么有必要,我不知道,但是通过在数据行添加到工作表之后添加着色代码,它现在可以正常工作。

1 个答案:

答案 0 :(得分:2)

嗯,通常它应该可以正常工作。但是如果要保存到XLS文件(而不是XLSX文件),可能不会应用浅蓝色背景颜色,因此您必须首先通过代码行添加到MS Excel调色板: 例如 示例代码:

......... workbook.ChangePalette(Color.LightBlue,55);

无论如何,我们需要您的模板Excel文件,因此我们可以精确评估您的问题,并测试将颜色应用到我们这边的单元格并指导您。我想你可以在Aspose.Cells论坛中跟进你的thread

我是Aspose的支持开发人员/传播者。