【问题标题】:Why does this code not set the background color for a cell (Aspose Cells)?为什么此代码不为单元格(Aspose Cells)设置背景颜色?
【发布时间】:2017-01-17 02:20:41
【问题描述】:

我需要一个单元格的背景颜色为浅蓝色;我有在另一张表中工作的代码,但完全相同的代码(变量名称和列索引 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

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

更新 3

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

【问题讨论】:

    标签: aspose aspose-cells


    【解决方案1】:

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

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

    无论如何,我们需要您的模板 Excel 文件,以便我们可以准确评估您的问题并测试将颜色应用于我们这边的单元格并适当地指导您。我想你可以在 Aspose.Cells 论坛上关注你的thread

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

    【讨论】:

    • 我保存为 .xlsx
    • 好的,我给你发了一份生成的电子表格文件;我不知道您所说的“模板”是什么意思;希望这就是你所需要的。我以编程方式生成整个文件——我没有从任何模板或类似的东西开始。我将帖子更改为“私人”,因为它包含客户可能不希望“全世界”看到的信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 1970-01-01
    • 1970-01-01
    • 2010-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多