生成逗号分隔的字符串不起作用

时间:2015-07-09 14:50:09

标签: c# excel csv asp.net-web-api

[WebGet(UriTemplate = "csv")]
public Stream GetCollectionAsCsv()
{
    string[] myValues = { "test", "test","test" };
    string csvString = string.Join(",", myValues);

    byte[] csvBytes = Encoding.UTF8.GetBytes(csvString);


    if (WebOperationContext.Current != null)
    {
        WebOperationContext.Current.OutgoingResponse.ContentType = "text/csv";
        WebOperationContext.Current.OutgoingResponse.Headers["Content-Disposition"] = "attachment; filename=\"testss.csv\"";
    }


    return new MemoryStream(csvBytes);

}

当我运行此代码时,我可以下载csv文件,虽然我将逗号添加到cvs string excel文件中,将所有数据放入单个列中

**OUTPUT EXCEL**

ROW A | ROW B | ROW C

test,test,test | NOTHING | NOTHING 


**My Expectation**

ROW A | ROW B | ROW C

test|test|test

1 个答案:

答案 0 :(得分:0)

最有可能与区域设置有关。尝试用分号替换逗号:

string csvString = string.Join(";", myValues);

有关更多选项,请参阅此页面: http://kb.paessler.com/en/topic/2293-i-have-trouble-opening-csv-files-with-microsoft-excel-is-there-a-quick-way-to-fix-this

E.g。你可以尝试添加 九月=, 作为CSV文件中的第一行

或更改您的区域设置以确保逗号是列表分隔符