Excel单元格着色

时间:2016-10-12 13:21:36

标签: c# excel aspose aspose-cells

我在excel中有一张名为导出的表格,我希望它的标题是灰色的。 这是代码:

 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方法。没有任何作用。我该怎么办?

2 个答案:

答案 0 :(得分:0)

来自文档: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);

答案 1 :(得分:0)

@Jess Wss,

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

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

    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 });
    
  3. enter image description here

    注意:我使用Aspose作为开发者布道者。