Reputation: 63357
I found some code which uses Style property of Microsoft.Office.Interop.Excel.Worksheet.Cells[x,y]
but it is treated as an object in my Visual Studo code editor:
Workbook wb = new Application.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Worksheet ws = wb.Sheets[1];
ws.Cells[x,y] is simply treated as an object so how can I use its Style property?
I'm using Microsoft Excel 15.0 Objects Library (goes with Microsoft Office 2013). Does that matter?
Could you please explain this to me? Thank you.
Upvotes: 4
Views: 34968
Reputation: 2683
you have to cast the object as a Range
.
The Range interface/object contains all the style and value information for the cell or range that you are specifying.
some examples:
((Excel.Range)ws.Cells[r, c]).NumberFormat = format;
((Excel.Range)ws.Cells[r, c]).Value2 = cellVal;
((Excel.Range)ws.Cells[r, c]).Interior.Color = ColorTranslator.ToOle(Color.Red);
((Excel.Range)ws.Cells[r, c]).Style.Name = "Normal"
etc etc and so on.
Have a link: https://learn.microsoft.com/en-us/office/vba/api/Excel.Range.Style
Upvotes: 8
Reputation: 526
You might also like to check out stackoverflow.com/questions/15366340/ which includes cell formatting while exporting to excel.
Upvotes: 0