Coloring workbook cell using Aspose.Cells for .NET in C#

I know this has been covered in other postings but even after reading those posts I can’t seem to get things to work. Attached is my sample program that creates an XLS file and tries to set background colors for several cells in the first row. Opening the XLS file that’s created by the program in OpenOffice 3.1 or MS Office 2007 shows the font attributes (bold, italic) work but neither foreground nor background colors are observed.


Using a style object doesn’t seem to have any different result (see second row example).

Any ideas? This formatting was one of the main reasons I chose this component, and it would be really nice to have.

Thanks,
M.

Hi,

Thank you for considering Aspose.

Well, as you are using Style.Pattern = BackgroundType.Solid, so only foreground color (or fill color) will take effect and background color will not be applied (as MS Excel does when Style.Pattern is set as Solid). Now, as you are setting foreground color as white, so it seems that no color is applied in the generated file( as the default cell fill color is white too). Please see the following documentation link for further details on Colors & background Patterns:

https://docs.aspose.com/display/cellsjava/Cells+Formatting#CellsFormatting-SettingColors&BackgroundPatterns

Thank You & Best Regards,

Hi,

I would like to add more. Well, I think you are mixing the two things foreground and background. I think you are taking foreground color as font text color, it not it? And, you are thinking of background color as cell shading color. For your info, Foreground color is the cell's outline color and Background color is the back color of the cell actually. But, mind you, if you want to set a solid shading color for cells, you should use Foreground color with Solid pattern type. There is no need to use Background color then. Also, if a color is not present on the standard color palette you should add it to the color palette of MS Excel first, check the topic: Colors and Palette

I have changed your code accordingly for your need, please see it:

using System;

using System.Collections.Generic;

using System.Drawing;

using System.Text;

using Aspose.Cells;

namespace AsposeCellsBackgroundColor {

public class Program {

static void Main(string[] args) {

string outputFile = System.IO.Path.GetTempFileName();

if (args.Length > 0)

outputFile = args[0];

Console.WriteLine(@"Writing test workbook to {0}", outputFile);

Console.WriteLine(@"Creating workbook object");

Workbook book = new Aspose.Cells.Workbook();

//Since Brown, Light Gray and Orange colors are not present on

//MS Excel standard color palette (that has only 56 colors (0-55) indexed), so you need to add it to the MS Excel

//color palette firt

book.ChangePalette(Color.Brown, 55);

book.ChangePalette(Color.Orange, 54);

book.ChangePalette(Color.LightGray, 53);

Console.WriteLine(@"Clearing existing worksheets");

book.Worksheets.Clear();

Console.WriteLine(@"Adding new worksheet");

string sheetName = @"TestWorksheet";

book.Worksheets.Add(sheetName);

Worksheet sheet = book.Worksheets[sheetName];

Console.WriteLine(@"Found worksheet {0} in workbook", sheetName);

int col = 0;

SetCell(sheet, 0, col++, Color.Blue, Color.White, @"Blue");

SetCell(sheet, 0, col++, Color.Brown, Color.White, @"Brown");

SetCell(sheet, 0, col++, Color.Red, Color.White, @"Red");

SetCell(sheet, 0, col++, Color.Green, Color.White, @"Green");

SetCell(sheet, 0, col++, Color.Yellow, Color.Black, @"Yellow");

SetCell(sheet, 0, col++, Color.Orange, Color.White, @"Orange");

SetCell(sheet, 0, col++, Color.LightGray, Color.Black, @"Light Gray");

Style s = book.Styles[book.Styles.Add()];

s.ForegroundColor = Color.Black;

s.Pattern = BackgroundType.Solid;

s.Font.Name = @"Courier New";

s.Font.IsBold = false;

s.Font.IsItalic = true;

s.Font.Color = Color.White;

sheet.Cells[1, 0].Style = s;

sheet.Cells[1, 0].PutValue(@"Styled cell");

book.Save(outputFile, FileFormatType.Excel2003);

Console.WriteLine(@"Saved workbook to {0}", outputFile);

Console.Write(@"Done...");

Console.ReadLine();

}

private static void SetCell(Worksheet sheet, int row, int col, System.Drawing.Color fgcolor, System.Drawing.Color fontcolor, string text) {

sheet.Cells[row, col].Style.ForegroundColor = fgcolor;

sheet.Cells[row, col].Style.Pattern = BackgroundType.Solid;

sheet.Cells[row, col].Style.Font.IsBold = true;

sheet.Cells[row, col].Style.Font.Color = fontcolor;

sheet.Cells[row, col].PutValue(text);

Console.WriteLine(@"Sheet cell {0}, {1} text is {2}, fgcolor is {3}", row, col, sheet.Cells[row, col].StringValue, sheet.Cells[row, col].Style.ForegroundColor.Name);

}

}

}

Feel free to contact us any time if you need further help.

Thank you.

Okay, so I need to set the ForegoundColor, but use BackgroundType.Solid. That’s a bit counter-intuitive but it works and I’m happy.


What I’m really after is coloring entire columns based on their purpose to the user. I can definitely see how to do this a cell at a time, but why doesn’t this code work?

sheet.Cells.Columns[col].Style.ForegroundColor = bgcolor;
sheet.Cells.Columns[col].Style.Pattern = BackgroundType.Solid;
sheet.Cells.Columns[col].Style.Font.Color = textColor;

Thanks!
M.

Hi,

Thank you for considering Aspose.

Please try Column.ApplyStyle method or Cells.ApplyColumnStyle method to apply style to a complete column. Please see the following updated code as per your need:

Style style;

StyleFlag flag;


//Set Number Style
style = sheet.Cells.Columns[col].Style;

style.ForegroundColor = bgcolor;

style.Pattern = BackgroundType.Solid

style.Font.Color = textColor;

flag = new StyleFlag();

flag.FontColor = true;

flag.CellShading = true;

sheet.Cells.Columns[col].ApplyStyle(style, flag);

//OR YOU MAY ALSO USE THE FOLLOWING LINE TO APPLY COLUMN STYLE
// sheet.Cells.ApplyColumnStyle(col,style,flag);

Thank You & Best Regards,