c# - 在 c# 中使用 Microsoft.Office.Interop.Excel 在 excel 列中设置数据类型,如数字、文本和日期

标签 c# excel format ms-office office-interop

我正在尝试将数据类型设置为 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/

相关文章:

c# - Azure Function 停止工作 - 无法加载类型 'Microsoft.Azure.WebJobs.BlobTriggerAttribute'

vba - 无法使 CommandBar 可见

excel - 工作簿中跨工作表的 VLOOKUP ID#

android - 计时器格式更改为 0 :0 to 00:00?

c# - 来自 C# 的 VirtualBox COM API

c# - 可以在 C# 中获取别名类型的类型吗?

C# - 通过 Gmail 或其他方式发送电子邮件?

excel - 如何将数据从我的谷歌驱动器中的 xlsx 文件复制到谷歌表格?

linux - 使用 linux shell 命令将多行输出合并为一行重新格式化报告文件

c - 如何让 printf 语句只有这么长?