Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi, i have to bind mm/dd/yyyy format date in to text format in excel. i used below code.But its still in Custom format how can change this to Text in excel

ie: Any format coming from excel i need to bind back to mm/dd/yyyy format and i have to change the excel Date coloumn format to Text format

following code is not working .I am getting the mm/dd/yyyy format but format not changing to text still its in custom.how to change?

What I have tried:

string effdate = xlWorksheet.Cells[i, datecolm].Value.ToString();
                        var words1 = effdate.Split(' ');
                        var white1 = words1.FirstOrDefault();

                        DateTime dt = DateTime.Parse(white1);
                        xlWorksheet.Cells[i, datecolm] = dt.ToString("MM/dd/yyyy");
                        xlWorksheet.Cells[i, datecolm].NumberFormat  = "MM/DD/YYYY";
Posted
Updated 1-Sep-21 23:43pm
Comments
Richard MacCutchan 2-Sep-21 3:49am    
You should store dates and times in Excel as proper Excel DateTime types. The formatting should only be done when the cell is diaplayed.

1 solution

You have not changed the NumberFormat to text, you have changed it to the custom format "MM/DD/YYYY".

This will change the format to text
C#
xlWorksheet.Cells[i, datecolm] = dt.ToString("MM/dd/yyyy");
xlWorksheet.Cells[i, datecolm].NumberFormat  = "@";
However, take note of the comment from Richard MacCutchan - you should store this date as a Date not Text and use one of the standard NumberFormats for display only.

For example
C#
xlWorksheet.Cells[i, datecolm] = dt;
xlWorksheet.Cells[i, datecolm].NumberFormat  = "m/d/yyyy";
Excel should automatically change that "m/d/yyyy" to your locale display settings

EDIT after OP Comment:

Try setting the format first then adding the value i.e.
C#
xlWorksheet.Cells[i, datecolm].NumberFormat  = "@";
xlWorksheet.Cells[i, datecolm].Value = dt.ToString("MM/dd/yyyy");
If that doesn't work then put a single quote in front of the string e.g.
C#
xlWorksheet.Cells[i, datecolm].NumberFormat  = "@";
xlWorksheet.Cells[i, datecolm].Value = String.Format("'{0}", dt.ToString("MM/dd/yyyy"));
However, if you have to "convert" this to a pipe delimited file why not just write out a pipe delimited file in the first place and build it using StringBuilder[^], or String.Join[^] or Linq[^]?
 
Share this answer
 
v2
Comments
[no name] 2-Sep-21 5:55am    
issue is i need to change the excel column format.I am able to bind my excel like 1/3/2020 but thats not the issue.I need to convert this sheet to pipe deliamitted .text file.when i am convering i am getting full date time.That should not happen.So i need to make the excel coloumn format "Text" how to do? xlWorksheet.Cells[i, datecolm].NumberFormat = "@"; this is giving like "44676" but i need like 4/4/2021
CHill60 2-Sep-21 12:15pm    
I've added some suggestions to my solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900