1

データ結果を CSV としてユーザーに送信するためのコードがいくつかあります。これは Excel 2013 では正常に機能しますが、Excel 2007 では列に分割されず、データが 1 つの列にのみ挿入されます。

テキストを分割する方法をExcelに伝える方法はありますか( ; で区切られています)?これが私のコードです:

    public async Task ExcelResultList(int id)
    {
        var asString = await Resolver.Resolve<IHandoutManager>().GetResultListAsStringAsync(id);
        var handout = await Resolver.Resolve<IHandoutManager>().GetHandout(id);

        var filename = string.Format("{0} registrations - {1:yyyy-MM-dd}.csv", handout.Name, DateTime.Now);
        var contenttype = "application/csv";
        Response.Clear();
        Response.ContentType = contenttype;
        Response.AddHeader("content-disposition", "attachment;filename=" + filename);            
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentEncoding = Encoding.Unicode; 
        Response.Write(asString);
        Response.End();
    }
4

1 に答える 1

2

正しい ListSeparator ("," または ";") を使用していることを確認するには、これを使用します

System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator

ただし、サーバー側にしかアクセスできないため、この JavaScript を任意のページに含めることができます。

function getListSeparator() {
    var list = ['a', 'b'], str;
    if (list.toLocaleString) {
        str = list.toLocaleString();
        if (str.indexOf(';') > 0 && str.indexOf(',') == -1) {
            return ';';
        }
    }
    return ',';
}

キーは、クライアント側のシステム リスト セパレータを使用するtoLocaleStringメソッドにあります。

JavaScript を使用してリスト セパレーターを取得し、それを Cookie に設定して、サーバーから検出し、必要に応じてファイルを生成することができます。

また、コンテンツタイプをに変更してみください

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

また

application/vnd.ms-excel
于 2013-09-05T11:45:43.473 に答える