C# Microsoft.Office.Interop.Excel.Worksheet.Cells[x,y].Style?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15926578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 18:30:37  来源:igfitidea点击:

Microsoft.Office.Interop.Excel.Worksheet.Cells[x,y].Style?

c#automationexport-to-excelexcel-interop

提问by King King

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:

我发现了一些使用 Style 属性的代码,Microsoft.Office.Interop.Excel.Worksheet.Cells[x,y]但它在我的 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?

I'm using Microsoft Excel 15.0 Objects Library (goes with Microsoft Office 2013). Does that matter?

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

Could you please explain this to me? Thank you.

你能向我解释一下吗?谢谢你。

采纳答案by Nevyn

you have to cast the object as a Range.

您必须将对象转换为Range.

The Range interface/object contains all the style and value information for the cell or range that you are specifying.

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

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: http://msdn.microsoft.com/en-us/library/office/ff834326.aspx

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

回答by STiTCHiCKED

You might also like to check out stackoverflow.com/questions/15366340/which includes cell formatting while exporting to excel.

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