简体   繁体   中英

excel cell formatting

如何以编程方式将单元格格式更改为Excel中的文本?

You can use:

Range("A1").NumberFormat = "@"

for text

Range("A1").NumberFormat = "dd/mm/yyyy hh:mm:ss" 

for a date.

Range("A1").NumberFormat = "#,###"

for money, etc.

The way that I answer this and other excel queries it to record a macro in Excel, perform the action that I want to see how to do, and then look at the macro to see what it recorded.

Doing that, this questions answer is:

Selection.NumberFormat = "@"

To apply a style to a named range

Create a new style and set its attributes.

Excel.Style style = Globals.ThisWorkbook.Styles.Add("NewStyle", missing);

style.Font.Name = "Verdana";
style.Font.Size = 12;
style.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
style.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Gray);
style.Interior.Pattern = Excel.XlPattern.xlPatternSolid;

Create a NamedRange control, assign text to it, and then apply the new style.

Microsoft.Office.Tools.Excel.NamedRange rangeStyles = this.Controls.AddNamedRange(this.Range["A1", missing], "rangeStyles");

rangeStyles.Value2 = "'Style Test";
rangeStyles.Style = "NewStyle";
rangeStyles.Columns.AutoFit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM