【问题标题】:Why does setting a color to a cell not work (Aspose Cells)?为什么为单元格设置颜色不起作用(Aspose Cells)?
【发布时间】:2016-11-23 16:38:55
【问题描述】:

我有这段代码可以尝试设置单元格的背景颜色(除其他外):

private static readonly Color CONTRACT_ITEM_COLOR = Color.FromArgb(255, 255, 204);
. . .
cell = pivotTableSheet.Cells[4, 0];
cell.PutValue(AnnualContractProductsLabel);
style = cell.GetStyle();
style.HorizontalAlignment = TextAlignmentType.Center;
style.VerticalAlignment = TextAlignmentType.Center;
style.Font.IsBold = true;
pivotTableSheet.Cells.SetRowHeight(4, 25);
style.BackgroundColor = CONTRACT_ITEM_COLOR;
pivotTableSheet.Cells[4, 0].SetStyle(style);

水平和垂直对齐的设置有效,粗体和高度也有效 - 除了颜色之外的所有内容:

还需要什么?我什至尝试将 ForegroundColor 和 Background 颜色设置为:

style.ForegroundColor = Color.Red;
style.BackgroundColor = Color.Blue;

...但两者都没有 - 单元格看起来仍然与上面的屏幕截图完全相同。

【问题讨论】:

    标签: excel colors aspose aspose-cells background-foreground


    【解决方案1】:

    请将您的代码段更改为(请参阅突出显示的行): 例如 示例代码:

    . . .
    cell = pivotTableSheet.Cells[4, 0];
    cell.PutValue(AnnualContractProductsLabel);
    style = cell.GetStyle();
    style.HorizontalAlignment = TextAlignmentType.Center;
    style.VerticalAlignment = TextAlignmentType.Center;
    style.Font.IsBold = true;
    pivotTableSheet.Cells.SetRowHeight(4, 25);
    **style.ForegroundColor = CONTRACT_ITEM_COLOR;
    style.Pattern = BackgroundType.Solid;**
    pivotTableSheet.Cells[4, 0].SetStyle(style);
    

    .......

    它应该可以正常工作。

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

    【讨论】:

      【解决方案2】:

      设置背景有时有效,有时无效。 Aspose 充满了错误—— ClosedXML 更可靠但更难使用。希望我不会把钱花在由第三方在第三世界国家开发的产品上。

      var cells = worksheet.Cells;     //get cells collection from active worksheet <br>
      var srcCells = workbook.Worksheets[1].Cells;<br>
      
      for (int i = 0; i<rowCount; i++)<br>
      {<br>
          var srcStyle = srcCells[i, 0].GetStyle();<br>
          var destStyle = cells[i, 0].GetStyle();<br>
          destStyle.Pattern = BackgroundType.Solid;<br>
          destStyle.ForegroundColor = Color.FromArgb(srcStyle.ForegroundArgbColor);<br>
          cells[i, 0].SetStyle(destStyle);<br>
      }<br>
      

      以上代码不起作用。 srcStyle 前景色 argb 为 170,215,255。
      调试代码 destStyle ForegroundColor 设置为 170,215,255 但保存为 xlsx 时,所有单元格背景都是白色的。

      在 ClosedXML 代码中,以下代码完美运行

      worksheet.Row(i).Cell(0).Style.BackgroundColor = Color.FromArgb(argb)
      

      结论:节省 $$$$$ 并使用 ClosedXML

      【讨论】:

      • 我们需要先评估文件。 OldGeek,你能分享你的示例文件吗,我们可以检查一下。您可以将示例文件上传到 Dropbox 或 Google Drive 等,并分享下载链接。 PS。我在 Aspose 担任支持开发人员/布道者
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-06
      • 1970-01-01
      • 1970-01-01
      • 2019-07-07
      相关资源
      最近更新 更多