Hello,
I’m trying to dynamically set individual cell styles, but having no luck. Here is a code snippet:
Workbook wb = new Workbook(FileFormatType.Excel2007Xlsx);
ThemeColor rowTc = new ThemeColor(ThemeColorType.Accent1, 20.0);
ThemeColor altRowTc = new ThemeColor(ThemeColorType.Accent1, 40.0);
Style headerStyle = wb.Styles[wb.Styles.Add()];
headerStyle.BackgroundThemeColor = new ThemeColor(ThemeColorType.Accent1, 100);
headerStyle.IsLocked = true;
headerStyle.Name = “HeaderStyle”;
Style rowStyle = wb.Styles[wb.Styles.Add()];
rowStyle.BackgroundThemeColor = rowTc;
rowStyle.IsLocked = true;
Style alternatingRowStyle = wb.Styles[wb.Styles.Add()];
alternatingRowStyle.BackgroundThemeColor = altRowTc;
alternatingRowStyle.IsLocked = true;
Worksheet ws = wb.Worksheets.Add(“TestSheet”);
// Set the columns
for (int i = 0; i < 10; i++)
{
ws.Cells[0, i].PutValue(“Column #” + i.ToString());
ws.Cells[0, i].SetStyle(headerStyle);
}
// Add a couple of rows
for (int row = 1; row < 5; row++)
{
for (int col = 0; col < 10; col++)
{
ws.Cells[row, col].PutValue(col);
if(row % 2 == 0)
ws.Cells[row, col].SetStyle(rowStyle);
else
ws.Cells[row, col].SetStyle(alternatingRowStyle);
}
}
wb.Save(Path.Combine(@“c:\temp”, Path.ChangeExtension(Path.GetFileName(Path.GetTempFileName()), “.xlsx”)), FileFormatType.Excel2007Xlsx);
What am I doing wrong here? I’ve tried a bunch of other things, including calling .Update() on the style object, setting the cell.Style.Name property, etc. I feel I am missing some simple setting somewhere.
Ultimately I need this kind of cell-level flexibility in my solution. Almost all cells in the generated workbook need to be locked, except for certain editable cells, which also need to have a different look than the rest.
Thank you,
Jim