Exporting a Unicode .csv (comma separated) file to

2019-08-07 23:02发布

Is there anything we can do either in code (ASP/JavaScript) or in Excel so that the comma separated values end up in separate columns in Excel?

9条回答
神经病院院长
2楼-- · 2019-08-07 23:33

I just tried using Tab characters for field seperators instead of commas and it worked in Excel 2007

(at least I think it's 2007, can't find Help/About in the stupid ribbon)

查看更多
够拽才男人
3楼-- · 2019-08-07 23:33

You can try saving the file as .txt, not .csv, this forces Excel to parse text lines into columns

查看更多
▲ chillily
4楼-- · 2019-08-07 23:36

If you use "," Excel 2007 will read it, but not 2003. And if you use ";", its the other way around.

So, the best way is to generate a html table and output it as .xls. Excel 2007 will ask if its from a trusted source.

Here is a code example of how to do it:

private void ExportToXLSFromDataTable(DataTable dtExport, string filename)
{
    StringBuilder dataToExport = new StringBuilder();

    dataToExport.Append("<table>");
    dataToExport.Append("<tr>");

    foreach (DataColumn dCol in dtExport.Columns)
    {
        dataToExport.Append("<td>");
        dataToExport.Append(Server.HtmlEncode(dCol.ColumnName));
        dataToExport.Append("</td>");
    }

    dataToExport.Append("</tr>");

    foreach (DataRow dRow in dtExport.Rows)
    {
        dataToExport.Append("<tr>");
        foreach (object obj in dRow.ItemArray)
        {
            dataToExport.Append("<td>");
            dataToExport.Append(Server.HtmlEncode(obj.ToString()));
            dataToExport.Append("</td>");
        }
        dataToExport.Append("</tr>");
    }

    dataToExport.Append("</table>");

    if (!string.IsNullOrEmpty(dataToExport.ToString()))
    {
        Response.Clear();

        HttpContext.Current.Response.ContentType = "application/ms-excel";
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);

        HttpContext.Current.Response.Write(dataToExport.ToString());
        HttpContext.Current.Response.End();
    }
}
查看更多
来,给爷笑一个
5楼-- · 2019-08-07 23:38

once it is imported, you can go to 'Tools' -> 'Text to Columns...' menu and specify a delimiting character.

查看更多
放荡不羁爱自由
6楼-- · 2019-08-07 23:44

If you open the file via Excel's 'File --> Open' menu, then it will separate the values in the cells.

查看更多
成全新的幸福
7楼-- · 2019-08-07 23:44

Excel seems to get confused by UTF-16/UTF-8 byte order marks, so try getting rid of them.

With CSV, the contents of the cells can be unicode characters, but the separator, quote and newline characters always must be be ASCII. You can think about CSV as always being ASCII but each cell as a blob of binary and might be some unicode text.

Also, have a look at: http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm for more info.

查看更多
登录 后发表回答