【问题标题】:Excel cells coloringExcel 单元格着色
【发布时间】:2016-10-12 13:21:35
【问题描述】:

我在 Excel 中有一张名为 Export 的表格,我希望它的标题为灰色。 代码如下:

 protected void btnExcel_OnClick(object sender, EventArgs e)
 {
      var ex = new Aspose.Cells.Workbook();
      ex.Worksheets.Clear();
      Aspose.Cells.Worksheet ws = ex.Worksheets.Add("Export");
      ws.Cells.ImportTable(Export.GetExportList(GetWhereClause(), ConfigurationManager.AppSettings();
      ws.Cells[0, 0].PutValue("A");
      ws.Cells[0, 1].PutValue("B");
      ws.Cells[0, 2].PutValue("C");
      ws.Cells[0, 3].PutValue("D");
      ws.Cells[0, 4].PutValue("E");
      var style = ws.Cells.Rows[0].Style;
      style.Font.IsBold = true;
      ws.Cells.Rows[0].ApplyStyle(style, new StyleFlag { FontBold = true });
      ex.Save(string.Format("Export_{0}.xlsx", DateTime.Now.ToString("yyyyMMdd_HHmmss")), FileFormatType.Excel2007Xlsx, SaveType.OpenInExcel, Response);
}

我也包含了按钮代码。 我尝试过这样的事情:

style.BackgroundColor = Color.DarkGrey;

ws.Cells[0, 0].Style.BackgroundColor = Color.DarkGrey;

而且我没有.Interior 方法。没有任何效果。我能做什么?

【问题讨论】:

  • “没有任何效果”是什么意思?究竟是什么不起作用?
  • 您是否尝试过为单元格单独设置样式?像这样 ws.Cells[0,0].SetStyle(style);
  • @haindl 我在上面尝试的代码行。颜色根本没有出现。
  • 你试过stackoverflow.com/questions/17696458/…这里的方法了吗?
  • @Jacobr365,是的。没用。

标签: c# excel aspose aspose-cells


【解决方案1】:

@Jess Wss,

请检查以下代码以在电子表格的第一行应用单元格阴影。请注意,您的代码中有两个问题。

  1. 如果您希望应用单元格阴影,您还必须设置 Style.Pattern 属性。
  2. 您还需要打开相关的 StyleFlag 属性。在这种情况下,StyleFlag.CellShading 在应用样式之前必须为真。

    var style = ws.Cells.Rows[0].Style;
    style.Font.IsBold = true;
    style.ForegroundColor = System.Drawing.Color.LightGray;
    style.Pattern = BackgroundType.Solid;
    ws.Cells.Rows[0].ApplyStyle(style, new StyleFlag { FontBold = true, CellShading = true });
    

注意:我与 Aspose 一起担任开发人员宣传员。

【讨论】:

    【解决方案2】:

    来自文档:http://www.aspose.com/docs/display/cellsnet/Colors+and+Background+Patterns

    Style style = worksheet.Cells["A1"].GetStyle();
    style.BackgroundColor = Color.Yellow;
    worksheet.Cells["A1"].SetStyle(style);
    

    【讨论】:

      猜你喜欢
      • 2011-08-19
      • 1970-01-01
      • 2011-08-17
      • 2014-07-31
      • 2013-09-01
      • 1970-01-01
      • 2012-10-15
      • 1970-01-01
      相关资源
      最近更新 更多