We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Modifying Excel's default- built-in "Normal" style

Hi,

MS Excel 2003 has built-in styles, namely the “Normal” style that is applied by default to all cells. I wish to modify this style and can’t seem to find how to do it with Aspose.Cells v4.4.0.5.

I tried using the following code, but that only adds another style with the name “Normal”.
mStyleNormal = oWB.Styles[oWB.Styles.Add()];
mStyleNormal.Name = “Normal”;
mStyleNormal.VerticalAlignment = Aspose.Cells.TextAlignmentType.Top;

With Excel automation, you would do something like this. Notice that the style already exists, so you don’t have to add it :
Microsoft.Office.Interop.Excel.Style style = oWB.Styles[“Normal”];
style.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignTop;

Any ideas? Is this functionality implemented?

Thanks,

Dominic.

Hi Dominic,

Please use workbook.DefaultStyle property.See following codes:
workbook.DefaultStyle.VerticalAlignment = Aspose.Cells.TextAlignmentType.Top;

Thanks for pointing this out, I wasn’t looking in the right place.

Is there a similar feature for other built-in styles ? Namely “Comma”, “Percent” and “Currency” ?

Might I also suggest updating the wiki “How to Format Cells?” to explicit this feature a little more… http://www.aspose.com/Wiki/default.aspx/Aspose.Cells/FormatRoutine.html

Thanks again,

Dominic.

Hi Dominic,

Well you may create these styles, please refer to the following codes:

1). Workbook workbook = new Workbook();
Worksheet sheet1 = workbook.Worksheets[0];
Cells cells = sheet1.Cells;
cells["A1"].PutValue(3537);
//Set the percent style to a cell.
Style style = workbook.Styles[workbook.Styles.Add()];
style.Custom = "0%";
cells["A1"].Style = style;
workbook.Save("d:\\test\\stylepercent.xls");

2). Workbook workbook = new Workbook();
Worksheet sheet1 = workbook.Worksheets[0];
Cells cells = sheet1.Cells;
cells["A1"].PutValue(3537);
//Set currency style to a cell.
Style style = workbook.Styles[workbook.Styles.Add()];
style.Custom = "$#,##0;$-#,##0";
cells["A1"].Style = style;
workbook.Save("d:\\test\\stylecurrency.xls");

Thank you.

Hi again,

I’m trying the code you suggested but the values remain unchanged.

For example, while debugging, the Font.Color value remains black right after an assignement to red…

Try the following code :

Aspose.Cells.License license = new Aspose.Cells.License();
license.SetLicense(“Aspose.Cells.lic”); // license in bin folder

Aspose.Cells.Workbook oWB = null;
Aspose.Cells.Worksheet oSheet = null;

oWB = new Aspose.Cells.Workbook();

oWB.DefaultStyle.Font.Color = System.Drawing.Color.Red;
oWB.DefaultStyle.Font.IsBold = true;
oWB.DefaultStyle.VerticalAlignment = Aspose.Cells.TextAlignmentType.Top;
oWB.DefaultStyle.Rotation = 90;

oSheet = oWB.Worksheets[oWB.Worksheets.ActiveSheetIndex];
oSheet.Cells[0, 0].PutValue(“TEST STRING”);

oWB.Save(“test.xls”, Aspose.Cells.FileFormatType.Default, Aspose.Cells.SaveType.OpenInExcel, Response);

Any thoughts ?

Dominic.

Hi Dominic,

Sorry for my mistake.Please change your codes :

Aspose.Cells.Workbook oWB = null;
Aspose.Cells.Worksheet oSheet = null;

oWB = new Aspose.Cells.Workbook();
Style defaultStyle = oWB.DefaultStyle;
defaultStyle.Font.Color = System.Drawing.Color.Red;
defaultStyle.Font.IsBold = true;
defaultStyle.VerticalAlignment = Aspose.Cells.TextAlignmentType.Top;
defaultStyle.Rotation = 90;
oWB.DefaultStyle = defaultStyle;//replace default style with the new style.
oSheet = oWB.Worksheets[oWB.Worksheets.ActiveSheetIndex];
oSheet.Cells[0, 0].PutValue("TEST STRING");

Thanks for your update…

These two steps seem strange though and should be better explained in the wiki in my opinion…

Style defaultStyle = oWB.DefaultStyle;

oWB.DefaultStyle = defaultStyle; //replace default style with the new style.

Isn’t this equivalent to doing something like :
int x = y;
y = x;

When seeing something like this, my first instinct is to suppress the unnecessary intermediate variable… Maybe you should check that out in a future hot fix…

Dominic.

Hi Dominic,

We will check it soon. But now you have to call oWB.DefaultStyle = defaultStyle to replace the default style.