Free Support Forum - aspose.com

Applying Styles to the Worksheet

I have a query related to styling in Apose.Cells. Am using two methods, on one method creating one style ( HeaderStyle ) , and on another method am accessing the style . Style is not getting applied to my Worksheet.

Below is the code snippet i have used .

// Creating Style :
Style HeaderStyle = myWorkBook.Styles[myWorkBook.Styles.Add()];
HeaderStyle.Font.IsBold = true;
HeaderStyle.Font.Name = "Calibri";
HeaderStyle.Font.Size = 11;
HeaderStyle.IsTextWrapped = false;

//Applying Style

myWorksheet.Cells["A1"].SetStyle(workbook.Styles["HeaderStyle"]);


Please suggest on this .

Regards,
Nagalakshmi V

Hi,


Please try the following sample code, it works fine:
e.g
Sample code:

Workbook myWorkBook = new Workbook();
Worksheet myWorksheet = myWorkBook.Worksheets[0];
// Creating Style :
Style HeaderStyle = myWorkBook.Styles[myWorkBook.Styles.Add()];
HeaderStyle.Font.IsBold = true;
HeaderStyle.Font.Name = “Calibri”;
HeaderStyle.Font.Size = 11;
HeaderStyle.IsTextWrapped = false;

//Applying Style
myWorksheet.Cells[“A1”].SetStyle(HeaderStyle);

myWorkBook.Save(“e:\test2\out1applyingstyles1.xlsx”);

Let us know if you still have any issue.

Thank you.
Hi ,

Thanks for your response .

The piece of code which you have given will work , when am creating the style and accessing the style on same method and i used this on one of my requirement.

My case i have one generic(common) method for creating styles and i want to access the styles on 4 different methods.

How can we access the styles on another method ?


Regards
Nagalakshmi V

Hi Nagalakshmi,


Thank you for writing back.

If you wish to keep on using your logic of creating the Style objects and accessing them by their names then you need to assign a name to the Style object. Please check the following modified code.

C#

var myWorkBook = new Workbook();
var myWorksheet = myWorkBook.Worksheets[0];
// Creating Style :
Style HeaderStyle = myWorkBook.Styles[myWorkBook.Styles.Add()];
HeaderStyle.Name = “HeaderStyle”;
HeaderStyle.Font.IsBold = true;
HeaderStyle.Font.Name = “Calibri”;
HeaderStyle.Font.Size = 11;
HeaderStyle.IsTextWrapped = false;

//Applying Style
myWorksheet.Cells[“A1”].SetStyle(myWorkBook.Styles[“HeaderStyle”]);
myWorkBook.Save(“D:/output.xlsx”);