将数据集导出到CSV时出现格式问题 [英] Formatting issues while exporting dataset to csv

查看:107
本文介绍了将数据集导出到CSV时出现格式问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个无法解决的简单格式设置问题.

我正在尝试将数据集导出到asp.net中的csv.问题是column1中的数据正在更改为科学计数法.
例如,值32E0已转换为3.20E + 02.

我正在使用以下代码将数据导出到csv.

导出到csv后,我们可以格式化列吗?

请帮忙.

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();           
 
        }

推荐答案

我认为,您的问题在于列的格式.
通常,当数据具有巨大的数字列时,并且当数据转换为另一个数据源时,它倾向于变为指数.
为避免这种情况,可以应用一种修复程序,因为这是CSV文件,下面是解决方案;
只需设置
dt.Columns["digit_column"].DataType = typeof(System.String);
并添加数据后缀,因为将数据分配给表列后将不允许更改DataType.
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.


也许您应该将分隔符从,"更改为;"字符...
Maybe you should change your separator from the "," to the ";" character...


感谢您的回复.

我尝试了两种解决方案,但对我没有任何帮助.

我创建了另一个表,该表的列具有字符串数据类型,并在各行之间循环..
但没有运气.

仍无法格式化第1列的值.

有什么想法吗?
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?


这篇关于将数据集导出到CSV时出现格式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆