在asp.net中编写csv文件 [英] Writing csv file in asp.net

查看:244
本文介绍了在asp.net中编写csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图导出数据到csv文件,因为在数据中有一些中文字符,我不得不使用unicode ..但在添加unicode的前导后,逗号不会被识别为分隔符,所有的数据现在写入第一列。我不知道是什么问题。
下面是我在.ashx文件中写的代码。

I'm trying to export data to a csv file, as there are chinese characters in the data i had to use unicode.. but after adding the preamble for unicode, the commas are not recognized as delimiters and all data are now written to the first column. I'm not sure what is wrong. Below is my code which i wrote in a .ashx file.

    DataView priceQuery = (DataView)context.Session["priceQuery"];
    String fundName = priceQuery.Table.Rows[0][0].ToString().Trim().Replace(' ', '_');

    context.Response.Clear();
    context.Response.ClearContent();
    context.Response.ClearHeaders();

    context.Response.ContentType = "text/csv";
    context.Response.ContentEncoding = System.Text.Encoding.Unicode;        
    context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fundName + ".csv");
    context.Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());                

    String output = fundName + "\n";        
    output += "Price, Date" + "\n";

    foreach (DataRow row in priceQuery.Table.Rows)
    {
        string price = row[2].ToString();
        string date = ((DateTime)row[1]).ToString("dd-MMM-yy");

        output += price + "," + date + "\n";
    }

    context.Response.Write(output);


推荐答案

看起来MS Office不支持Unicode csv文件。

某些用户报告了制表符分隔符的作用。

我会尝试UTF-8而不是Unicode,这会导致分隔符被视为ASCII字符。

I would try UTF-8 rather than Unicode, this can cause delimiters treated as ASCII characters.

这篇关于在asp.net中编写csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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