在C#中使用类似的Microsoft.Office.Interop.Excel数字,文本和日期在Excel中列设置数据类型 [英] Set data type like number, text and date in excel column using Microsoft.Office.Interop.Excel in c#

查看:1444
本文介绍了在C#中使用类似的Microsoft.Office.Interop.Excel数字,文本和日期在Excel中列设置数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,我想数据类型设置为在C#中的Excel列,在这种情况下,数据类型的数字,文本和日期。我使用的DLL的Microsoft.Office.Interop.Excel。
我不知道如何使用C#有编程语言,这些格式设置为整个Excel列的单元格。

My problem is that I am trying to set data type to an excel column in C#, in this case the data types number, text and date. I am using the DLL Microsoft.Office.Interop.Excel. I don´t know how to set those formats to the cells of an entire excel column using C# has programming language.

推荐答案

要设定一个范围,文本:

To set a range to text:

xlYourRange.NumberFormat = "@";

您也可以preFIX你把一个单元与撇号的值,为它格式化文本:

You can also prefix a value you put in a cell with an apostrophe for it to format it as text:

xlYourRange.Value = "'0123456";

要设定一个范围,数

xlYourRange.NumberFormat = "0";

显然,如果你想为整个列的格式,然后你的范围将是列。

Obviously if you want to set the format for the entire column then your range will be the column.

xlYourRange = xlWorksheet.get_Range("A1").EntireColumn;

编辑:

日期是一个比较复杂,也将取决于您的区域设置:

Dates are a bit more complicated and will also depend on your regional settings:

// Results in a Date field of "23/5/2011"

xlRange.NumberFormat = "DD/MM/YYYY";
xlRange.Value = "23/5/2011";

// Results in a Custom field of "23/5/2011"

xlRange.NumberFormat = "DD-MM-YYYY";
xlRange.Value = "23/5/2011";

// Results in a Custom field of "05/23/2011"

xlRange.NumberFormat = "MM/DD/YYYY";
xlRange.Value = "5/23/2011";

// Results in a Custom field of "05-23-2011"

xlRange.NumberFormat = "MM-DD-YYYY";
xlRange.Value = "5/23/2011";

// Results in a Date field of "23/05/2011"

xlRange.NumberFormat = "DD/MM/YYYY";
xlRange.Value = "5/23/2011";

// Results in a Custom field of "23-05-2011"

xlRange.NumberFormat = "DD-MM-YYYY";
xlRange.Value = "5/23/2011";

// Results in a Custom field of "23/5/2011"

xlRange.NumberFormat = "MM/DD/YYYY";
xlRange.Value = "23/5/2011";

// Results in a Custom field of "23/5/2011"

xlRange.NumberFormat = "MM-DD-YYYY";
xlRange.Value = "23/5/2011";

这篇关于在C#中使用类似的Microsoft.Office.Interop.Excel数字,文本和日期在Excel中列设置数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆