Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Wed Sep 18, 2013 10:38 pm

I want to add a style to the workbook and then set it to a cell's style property. The style depends on a number of parameters at runtime so we need to set it like this.

Do you have any sample code for this?

thanks
scott

scott.dubose@virtusllc.com
 
Posts: 17
Joined: Wed Sep 18, 2013 8:49 pm

Wed Sep 18, 2013 10:46 pm

I tried something like this

Code: Select all
StylesCollection sc = wb.Styles;

CellStyle cs;
cs = sc.Add("WorksheetTitle");
cs.WrapText = true;
cs.VerticalAlignment = VerticalAlignType.Top;
cs.HorizontalAlignment = HorizontalAlignType.Left;
cs.Font.FontName = "Trebuchet MS";
cs.Font.Size = 18;
cs.Font.IsItalic = true;
cs.Font.IsBold = true;

....

worksheet.Range[1, 1].Style = styles["WorksheetTitle"];

GIVES EXCEPTION
Style name cannot be found. Name: WorksheetTitle
Parameter name: value
   at Spire.Xls.Core.Spreadsheet.Collections.XlsStylesCollection.get_Item(String
 name)
   at Spire.Xls.Collections.StylesCollection.get_Item(String name)
   at CashFlowXmlBuilder.WorkbookBuilderSpire.addWorkSheet(DataTable table, Work
  ...

scott.dubose@virtusllc.com
 
Posts: 17
Joined: Wed Sep 18, 2013 8:49 pm

Thu Sep 19, 2013 2:01 am

Dear scott,

Thanks for your inquiry.
I checked your code. There is something wrong with the code worksheet.Range[1, 1].Style = styles["WorksheetTitle"];. It should be worksheet.Range[1, 1].Style = sc["WorksheetTitle"];. Please try the modification and run your code again.

Welcome to wirte to us again in case you have further problems.

Best regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Mon Sep 23, 2013 8:40 pm

thank you. it appears the styles was copied to function call parameter via value rather than reference so I was losing all of my additions.

scott.dubose@virtusllc.com
 
Posts: 17
Joined: Wed Sep 18, 2013 8:49 pm

Tue Sep 24, 2013 6:27 am

Dear scott,

Thanks for your reply.
Has this issue been solved? Did we miss something for your issue?

Best regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.XLS