How to set the DefaultStyle for a Workbook

Hello,

I would like to change de default style (actualy, the default font) for all the workbook.

I have try this code, but it does not work:



Excel xls = new Excel();

xls.DefaultStyle.Font.Name = "Tahoma";

xls.Worksheets[0].Cells[0,0].PutValue("Hello World");

xls.Save(@"C:\XlsTest.xls");


The "Hello World" is in Arial.

Is it a way to set the default style without setting the style of all Cells ?

Thanks !

Ludovic DE FREITAS


I will check and add this feature in the future release.

Hi,
Is there an ETA on when this might be implemented? Any guesses?

I’ve downloaded and been playing with version 3.2.3.0, and the DefaultStyle attributes still behave as described above (i.e., don’t get changed when set). This functionality would definitely be a BIG help, and it would be great if it were being added in the near future.

Thanks. It’s a great component.

Please try the following sample code :

Excel xls = new Excel();
Style style = xls.DefaultStyle;
style.Font.Name = “Tahoma”;
xls.DefaultStyle = style;
xls.Worksheets[0].Cells[0,0].PutValue(“Hello World”);
xls.Save(@“C:\XlsTest.xls”);

Thanks for the suggestion. I believe I had tried most everything else (e.g., changing the DefaultStyle elements, creating a style and assigning it to DefaultStyle, etc.), but not this particular tack.

It seems (to me at least) almost counterintuitive that to change elements of DefaultStyle you need to “off-load” it to a style instance, make changes to that instance, and then re-apply the instance to DefaultStyle. It works, no question about that, and now that I know, it’s simple enough to do. The documentation (chm and IntelliSense) seems to suggest something a little more straightforward: in VS.net, coding “xls.DefaultStyle.Font.Name” shows it as a get/set property, but set doesn’t work in that context; to do a set, one has to follow what you show, above.

But, as I said, that works. Thanks again for the reply.

Your suggestion is helpful. Thank you.
I will check this issue and change it if possible. If it's impossible to change the property, I will change the documentation to clear this confusion.

@jlloyd,
Aspose.Cells has replaced Aspose.Excel that is no more available now. This new product contains all the features of its predecessor product as well as supports all the latest features available in different versions of MS Excel. You can configure the default style of the workbook easily as demonstrated in the following example.

Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
Style style = workbook.DefaultStyle;
style.Font.Name = "Tahoma";
style.Font.Size = 20;
worksheet.Cells["A1"].Value = "Hello World";
worksheet.AutoFitColumns();
workbook.DefaultStyle = style;
workbook.Save("Output.xlsx");

For more information about workbook default style, refer to the following API reference:
Workbook Default Style

Give a try to this new product by downloading the latest free trial version here:
Aspose.Cells for .NET(Latest version)

A runnable solution is available here that can be used to test the new product without writing any code.