I am trying to set the data type to an excel column in C#, in this case the data types number, text and date.
How does one set a format to an entire excel column?
You just have to change the format of that column or cell to text. '//Format to text ExcelWorksheet. Columns[1]. NumberFormat = "@";' if you are still having trouble let me know.
Select the field (the column) that you want to change. On the Fields tab, in the Properties group, click the arrow in the drop-down list next to Data Type, and then select a data type. Save your changes.
To set a range to text:
xlYourRange.NumberFormat = "@";
You can also prefix a value you put in a cell with an apostrophe for it to format it as text:
xlYourRange.Value = "'0123456";
To set a range to number
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;
EDIT:
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";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With