我正在尝试将数据类型设置为 C# 中的 excel 列,在本例中为数据类型数字、文本和日期。
如何为整个 excel 列设置格式?
最佳答案
设置文本范围:
xlYourRange.NumberFormat = "@";
您还可以在放置在单元格中的值前加上撇号,以便将其格式化为文本:
xlYourRange.Value = "'0123456";
将范围设置为数字
xlYourRange.NumberFormat = "0";
显然,如果您想为整个列设置格式,那么您的范围就是该列。
xlYourRange = xlWorksheet.get_Range("A1").EntireColumn;
编辑:
日期有点复杂,也取决于您的区域设置:
// 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# - 在 c# 中使用 Microsoft.Office.Interop.Excel 在 excel 列中设置数据类型,如数字、文本和日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13827709/