0

I have this sheet in excel called Export and I want its header to be gray. Here is the code:

 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);
}

I included the button code as well. I have tried something like this:

style.BackgroundColor = Color.DarkGrey;

or

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

And I don't have the .Interior method.Nothing works. What can I do?

Jess Wss
  • 39
  • 1
  • 1
  • 7

2 Answers2

0

From the docs: 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);
Slai
  • 22,144
  • 5
  • 45
  • 53
0

@Jess Wss,

Please check the following piece of code to apply the cell shading on 1st row of the spreadsheet. Please note, there are two issues in your code.

  1. In case you wish to apply the cell shading, you have to set the Style.Pattern property as well.
  2. You also require to turn on the relevant StyleFlag property. In this case, the StyleFlag.CellShading has to be true before applying the style.

    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 });
    

enter image description here

Note: I work with Aspose as Developer Evangelist.

Prorata
  • 483
  • 3
  • 10