Welcome to Dream.In.Code
Become a C# Expert!

Join 150,391 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,046 people online right now. Registration is fast and FREE... Join Now!




while writing a csv file from application, not able to write the zero

 
Reply to this topicStart new topic

while writing a csv file from application, not able to write the zero, while writing a csv file from application, not able to write the zero

Rajasree
27 Feb, 2008 - 10:20 PM
Post #1

New D.I.C Head
*

Joined: 27 Feb, 2008
Posts: 10

Hi,

I am writing a .csv file from application.

One column of the data contain zero padded numbers like 0021,0034 etc .

while writing the data into csv file , the numbers in the column is coming like 21 , 34 etc.

but I want the zero padding to be done.

Can anyone help me out to resolve this.?

CODE


DialogResult result;


SaveFileDialog objSaveDlg = new SaveFileDialog();

objSaveDlg.DefaultExt = ".csv";

objSaveDlg.OverwritePrompt = false;


result = objSaveDlg.ShowDialog();

if (result == DialogResult.OK)

{

GenerateCSV(objSaveDlg);

}

//-------------------GenerateCSV function -------------------

private void  GenerateCSV( SavefileDialog objSaveDlg)
{

     StreamWriter objStreamWriter = new StreamWriter(objSaveDlg.FileName);
    
    DataTable dt = new DataTable();

   dt = Filldata();


    // write headers

    string[] strArrColumns = new string[dt.Columns.Count];

   // add column headers

     for (int i = 0; i < dt.Columns.Count; i++)

     {
            strArrColumns = dt.Columns.ColumnName;
     }

    // write column headers

    p_objStreamWriter.WriteLine(string.Join(",", strArrColumns));

   // write data

   string[] strArrRows = new string[dt.Columns.Count];

   for (int j = 0; j < dt.Rows.Count; j++)

  {

     for (int k = 0; k < dt.Columns.Count; k++)

    {

         strArrRows[k] = " " + dt.Rows[j].ItemArray[k].ToString();

    }
    p_objStreamWriter.WriteLine(string.Join(",", strArrRows));


}


}



The data which is needed to write to csv is taking from data table dt.


User is offlineProfile CardPM
+Quote Post

davegeek
RE: While Writing A Csv File From Application, Not Able To Write The Zero
28 Feb, 2008 - 01:24 AM
Post #2

D.I.C Head
Group Icon

Joined: 30 Jan, 2008
Posts: 81



Thanked: 2 times
My Contributions
IMHO, the problem is that when u open the file in MS Excel it'll automatically convert numbers into text format. Try using some special or any other format to represent the data in the cell.
User is offlineProfile CardPM
+Quote Post

orcasquall
RE: While Writing A Csv File From Application, Not Able To Write The Zero
28 Feb, 2008 - 03:48 AM
Post #3

D.I.C Head
Group Icon

Joined: 14 Sep, 2007
Posts: 158



Thanked: 3 times
Dream Kudos: 50
My Contributions
As davegeek mentioned, Excel truncates the leading zeros if the cells aren't properly formatted beforehand.

If you're able to do something about that, or you want your csv file to have the right output first, then try this
csharp

strArrRows[k] = " " + dt.Rows[j].ItemArray[k].ToString("d4");

in your loop. The "d4" part forces leading zeros.
User is offlineProfile CardPM
+Quote Post

Rajasree
RE: While Writing A Csv File From Application, Not Able To Write The Zero
28 Feb, 2008 - 03:56 AM
Post #4

New D.I.C Head
*

Joined: 27 Feb, 2008
Posts: 10

QUOTE(orcasquall @ 28 Feb, 2008 - 04:48 AM) *

As davegeek mentioned, Excel truncates the leading zeros if the cells aren't properly formatted beforehand.

If you're able to do something about that, or you want your csv file to have the right output first, then try this
csharp

strArrRows[k] = " " + dt.Rows[j].ItemArray[k].ToString("d4");

in your loop. The "d4" part forces leading zeros.




Many Thanks to both of you...

But ToString function in C# will not support parameters.


User is offlineProfile CardPM
+Quote Post

Nayana
RE: While Writing A Csv File From Application, Not Able To Write The Zero
28 Feb, 2008 - 04:13 AM
Post #5

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
I assume you're outputting the text like this "0004" etc.

Excel is probably recognising them as numbers and reformatting them. Just change the way Excel formats that column. Excel can put the leading 0's in for you, it's clever like that.

csharp

int i = 4;
i.ToString("d4");

The above works, but excel will still change it from "0004" to "4"

csharp

string i = "0004";
i.ToString("d4");

Will not work, being a string it accepts no params, so that will be a compile error.

This post has been edited by Nayana: 28 Feb, 2008 - 04:13 AM
User is offlineProfile CardPM
+Quote Post

davegeek
RE: While Writing A Csv File From Application, Not Able To Write The Zero
28 Feb, 2008 - 06:48 AM
Post #6

D.I.C Head
Group Icon

Joined: 30 Jan, 2008
Posts: 81



Thanked: 2 times
My Contributions
Guys let's keep the KISS principle.
Why don't we use some standard template for Excel - just create a template where specific columns will be in the predefined format. Then whenever u read or open the file, it'll be open with the correct data set (whichever number of zeros u need before the number)

This post has been edited by davegeek: 28 Feb, 2008 - 06:49 AM
User is offlineProfile CardPM
+Quote Post

Rajasree
RE: While Writing A Csv File From Application, Not Able To Write The Zero
28 Feb, 2008 - 08:14 PM
Post #7

New D.I.C Head
*

Joined: 27 Feb, 2008
Posts: 10

QUOTE(davegeek @ 28 Feb, 2008 - 07:48 AM) *

Guys let's keep the KISS principle.
Why don't we use some standard template for Excel - just create a template where specific columns will be in the predefined format. Then whenever u read or open the file, it'll be open with the correct data set (whichever number of zeros u need before the number)



Thanks,

But I cant create a template, because in my application, fields keep on changing, dynamic,and in one column only I want the zero padding to be done.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 05:54PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month