Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have one simple formatting issue which i couldnt resolve.

I am trying to export a dataset to csv in asp.net.The issue is data in column1 is changing to scientific notations..

for example value 32E0 is getting converted to 3.20E+02.

I am using the below code to export the data to csv.

Can we able to format a column after exporting to csv.

Please help.

public static void CreateCSVFile(DataSet myData, string strFileName, string strReportTitle)
        {
            HttpContext.Current.Response.Clear();
 
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + strFileName + ".csv");
            HttpContext.Current.Response.Charset = string.Empty;
            HttpContext.Current.Response.ContentType = "application/text";
            
            StringBuilder sb = new StringBuilder();
 
            DataTable dt = myData.Tables[0];
 
            for (int k = 0; k < dt.Columns.Count; k++)
            {
                sb.Append(dt.Columns[k].ColumnName + ',');
            }
 
            sb.Append("\r\n");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int k = 0; k < dt.Columns.Count; k++)
                {
                    sb.Append(dt.Rows[i][k].ToString().Replace(",", ";") + ',');
                }
 
                sb.Append("\r\n");                
 
            }
 
            HttpContext.Current.Response.Output.Write(sb.ToString());
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();           
 
        }
Posted

I think, your problem is in the format of the column.
Mostly when data is having huge numeric columns, and when its tranformed in to another source, it is tend change to exponencial.
To avoid this, one fix can be applied, as this is a CSV file, solution below;
Just set the
dt.Columns["digit_column"].DataType = typeof(System.String);
And add data afterwords, coz it'll not allow to change DataType after data is assigned to table column.
 
Share this answer
 
v2
Maybe you should change your separator from the "," to the ";" character...
 
Share this answer
 
Thanks for your replies.

I tried both the solutions but nothing worked for me.

I created another table with columns having string datatype and loop through the rows..
but no luck.

Still couldnt format the column 1 values.

Any ideas?
 
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