Click here to Skip to main content
15,921,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have developed application to write in .csv format. I need to change the format to number of the .csv file for all columns by default.

Kindly help me out, how to change the format to Number. I need output as below format.

for example:

USER        	Active
0168	        0124341156
0168	        0124341156
0168	        0124341156


But iam getting output as

USER        	Active
168	        124341156
168	        124341156
168	        124341156


So in order to get output in correct format, i need to change it as number format. So that i can get "0" in beginning of number.


var lines = new List<string>();
string[] columnNames = ldatatables.Exportdatatable.Columns.Cast<DataColumn>().
                                         Select(column => column.ColumnName).
                                         ToArray();
var header = string.Join(",", columnNames);
lines.Add(header);
var valueLines = ldatatables.Exportdatatable.AsEnumerable()
                  .Select(row => string.Join(",", row.ItemArray));
lines.AddRange(valueLines);
File.WriteAllLines("Test.csv", lines);


What I have tried:

var lines = new List<string>();
string[] columnNames = ldatatables.Exportdatatable.Columns.Cast<DataColumn>().
                                         Select(column => column.ColumnName).
                                         ToArray();
var header = string.Join(",", columnNames);
lines.Add(header);
var valueLines = ldatatables.Exportdatatable.AsEnumerable()
                  .Select(row => string.Join(",", row.ItemArray));
lines.AddRange(valueLines);
File.WriteAllLines("Test.csv", lines);
Posted
Updated 22-May-18 11:25am
v2
Comments
Richard MacCutchan 21-May-18 6:12am    
What do you mean by "how to change the format to Number"? Change what format?
Vinodh Muthusamy 21-May-18 6:21am    
i have updated with example, kindly view them again
Richard MacCutchan 21-May-18 6:44am    
If you are creating a .csv file then the leading zeros do not mean anything.
Vinodh Muthusamy 21-May-18 6:46am    
i need leading zeros to be included..
Richard MacCutchan 21-May-18 6:49am    
Why? In a .csv file they have no meaning and serve no purpose. The only time you need leading zeroes is when you are presenting data to a user, on screen or in printed reports.

There isn't a standard way to do that, and it wouldn't be trivial to do anyway with yoru scheme - you can't just assume the data is going to be numeric and use a ToString override with a "leading zero format" because it won't work for non-numeric items in your row.

And even if the items are all numbers, it's still not possible which your scheme, because the number of digits you want in the output isn't the same for all rows., and you need a different format for each.
While it's possible to get round that with an array of format strings, it's a messy solution.

To be honest, you probably shouldn't be doing it at all: leading zeros are a presentation feature, not a data storage feature, and should really be applied by the software that reads the CSV file and displays it, not stored in the data file itself.
 
Share this answer
 
As OriginalGriff[^] already mentioned, there's no standard way to achieve that, but... you're on the right track!

Assuming that USER and Active fields are type of integer and you want to add leading zeros, you have to change your Select statement to:
C#
var valueLines =  dt.AsEnumerable()
					.Select(row =>
						string.Concat( 
							row.Field<int>("USER").ToString("0000"),
							",",
							row.Field<int>("Active").ToString("0000000000"))
					).ToList();


Good luck!
 
Share this answer
 

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