Free Support Forum - aspose.com

Cell or Range Style Bug- pls help me!

I try to using the Aspose.Cells (Version 4.3), I want to set the style to single cell or range, but the all cells style changed!! all cells's style be override.

please test the following code
---------------------------------------------------------------------------------------

Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
Cells cells = worksheet.Cells;
//Filling some data into the cells
for (int i = 0; i < 50; i++)
{
for (int j = 0; j < 10; j++)
{
cells[i, j].PutValue(i.ToString() + "," + j.ToString());
}
}


//First Style Setting
Aspose.Cells.Style mStyle = workbook.Styles[workbook.Styles.Add()];
mStyle.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
mStyle.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
mStyle.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
mStyle.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;

Range range = workbook.Worksheets[0].Cells.CreateRange(0, 0, 50, 10);
range.Style = mStyle;


//add a new style
Aspose.Cells.Style stl5 = workbook.Styles[workbook.Styles.Add()];
//Set the font name.
stl5.Font.Name = "Tahoma";
//Set the font size.
stl5.Font.Size = 10;
//Set font text color.
stl5.Font.Color = Color.Red;
//Set the font bold.
stl5.Font.IsBold = true;
//Set the font italic.
stl5.Font.IsItalic = true;
//Create the style flag struct and specify which formattings
//you want to apply.
StyleFlag flag = new StyleFlag();
flag.FontName = true;
flag.FontSize = true;
flag.FontColor = true;
flag.FontBold = true;
flag.FontItalic = true;

//Create a range of cells for your need.

Range range1 = workbook.Worksheets[0].Cells.CreateRange(4, 3, cells.MaxDataRow - 3, 1);
//Apply the style to cells in the named range.
range1.ApplyStyle(stl5, flag);
//!!!!!! but the all cells style changed!!!!!!!!

//or any single cell's style changed, will changed all cells, why? if don't setting the first style, the problem is not exist!
workbook.Worksheets[0].Cells["A5"].Style.Font.IsBold = true;

workbook.Save("e:\\my_book.xls");

---------------------------------------------------------------------------------------

I just want to change the style of single cell or range, not all cells. pls help me, thanks!!!

Hi,

Please try to change your code to, it will work:

Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
Cells cells = worksheet.Cells;
//Filling some data into the cells
for (int i = 0; i < 50; i++)
{
for (int j = 0; j < 10; j++)
{
cells[i, j].PutValue(i.ToString() + "," + j.ToString());
}
}


//First Style Setting
Aspose.Cells.Style mStyle = workbook.Styles[workbook.Styles.Add()];
mStyle.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
mStyle.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
mStyle.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
mStyle.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
StyleFlag flag0 = new StyleFlag();
flag0.Borders = true;

Range range = workbook.Worksheets[0].Cells.CreateRange(0, 0, 50, 10);


range.ApplyStyle(mStyle,flag0);

//add a new style
Aspose.Cells.Style stl5 = workbook.Styles[workbook.Styles.Add()];
//Set the font name.
stl5.Font.Name = "Tahoma";
//Set the font size.
stl5.Font.Size = 10;
//Set font text color.
stl5.Font.Color = Color.Red;
//Set the font bold.
stl5.Font.IsBold = true;
//Set the font italic.
stl5.Font.IsItalic = true;
//Create the style flag struct and specify which formattings
//you want to apply.
StyleFlag flag = new StyleFlag();
flag.FontName = true;
flag.FontSize = true;
flag.FontColor = true;
flag.FontBold = true;
flag.FontItalic = true;

//Create a range of cells for your need.
//D5: D46
Range range1 = workbook.Worksheets[0].Cells.CreateRange(4, 3, cells.MaxDataRow - 3, 1);
//Apply the style to cells in the named range.
range1.ApplyStyle(stl5, flag);
//!!!!!! but the all cells style changed!!!!!!!!

//or any single cell's style changed, will changed all cells, why? if don't setting the first style, the problem is not exist!
workbook.Worksheets[0].Cells["A5"].Style.Font.IsBold = true;

workbook.Save("e:\\my_book.xls");
When you apply formattings to named ranges, cols / rows, you better utilize ApplyStyle() method with StyleFlag struct.
Thank you.

thanks!!

btw, if I need to setting style for some different range or cell, replace "range.style=xx" with "range.ApplyStyle(xx,flag)" in the all code ?

and I want to set the "ForegroundColor" or "BackgroundColor" style, how to set the StyleFlag?

mStyle = wk.Styles[wk.Styles.Add()];
mStyle.ForegroundColor = Color.LightGreen;
StyleFlag sFlag3 = new StyleFlag();
sFlag3.(What?)=true;

range = xlsCells.CreateRange(0, 5, 50, 2);
range.ApplyStyle(mStyle, sFlag3);

or

range.Style.BackgroundColor=Color.FromArgb(0, 0, 255)

but it is noneffective~~

how to set the background color for some columns or cells?

thanks!

Hi,

Kindly consult the following code:

.
.
workbook.ChangePalette(Color.LightGreen,55);
.
.

mStyle = wk.Styles[wk.Styles.Add()];
mStyle.ForegroundColor = Color.LightGreen;
mStyle.Pattern = BackgroundType.Solid;
StyleFlag sFlag3 = new StyleFlag();
sFlag3.CellShading=true;
range = xlsCells.CreateRange(0, 5, 50, 2);
range.ApplyStyle(mStyle, sFlag3);

To know more about how to set background / foreground color of the cells, please check the thread below:

http://www.aspose.com/COMMUNITY/Forums/thread/74700.aspx

Thank you.