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

Page 1 of 1

6 Replies - 8779 Views - Last Post: 28 February 2008 - 09:14 PM Rate Topic: -----

#1 Rajasree  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 27-February 08

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

Posted 27 February 2008 - 11:20 PM

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.?

 

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.

Is This A Good Question/Topic? 0
  • +

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

#2 davegeek  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 81
  • Joined: 30-January 08

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

Posted 28 February 2008 - 02:24 AM

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.
Was This Post Helpful? 0
  • +
  • -

#3 orcasquall  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 158
  • Joined: 14-September 07

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

Posted 28 February 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
strArrRows[k] = " " + dt.Rows[j].ItemArray[k].ToString("d4");


in your loop. The "d4" part forces leading zeros.
Was This Post Helpful? 0
  • +
  • -

#4 Rajasree  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 27-February 08

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

Posted 28 February 2008 - 04:56 AM

View Postorcasquall, on 28 Feb, 2008 - 04:48 AM, said:

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
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.
Was This Post Helpful? 0
  • +
  • -

#5 Nayana  Icon User is offline

  • DIC Hawk - 나야나 नयन:
  • member icon

Reputation: 31
  • View blog
  • Posts: 824
  • Joined: 14-November 07

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

Posted 28 February 2008 - 05:13 AM

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.

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


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

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 February 2008 - 05:13 AM

Was This Post Helpful? 0
  • +
  • -

#6 davegeek  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 81
  • Joined: 30-January 08

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

Posted 28 February 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)

This post has been edited by davegeek: 28 February 2008 - 07:49 AM

Was This Post Helpful? 0
  • +
  • -

#7 Rajasree  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 27-February 08

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

Posted 28 February 2008 - 09:14 PM

View Postdavegeek, on 28 Feb, 2008 - 07:48 AM, said:

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1