Microsoft.Office.Interop.Excel.Worksheet.Cells .Style?

我找到了一些使用Microsoft.Office.Interop.Excel.Worksheet.Cells[x,y] Style属性的代码,但它在我的Visual Studo代码编辑器中被视为一个对象:

 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? 

我正在使用Microsoft Excel 15.0对象库(与Microsoft Office 2013一起使用)。 那有关系吗?

你可以向我解释一下吗? 谢谢。

你必须将对象强制转换为Range

Range接口/对象包含您指定的单元格或范围的所有样式和值信息。

一些例子:

 ((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" 

等等等。

有一个链接: http : //msdn.microsoft.com/en-us/library/office/ff834326.aspx

您可能还想查看stackoverflow.com/questions/15366340/ ,其中包括导出到Excel时的单元格格式。