首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在asp.net中编写csv文件

问在asp.net中编写csv文件
EN

Stack Overflow用户
提问于 2011-02-15 14:43:24
回答 1查看 8.4K关注 0票数 6

我正在尝试将数据导出到csv文件,因为在我必须使用unicode的数据中有中文字符。但是在添加unicode的前导之后,逗号不被识别为分隔符,所有数据现在都写入第一列。我不知道哪里出了问题。下面是我在.ashx文件中编写的代码。

代码语言:javascript
复制
    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);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-02-15 15:19:19

Looks like MS Office does not support Unicode csv files.

但是some users reported that tab delimiter works.

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5000638

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档