使用 ASP.NET 从服务器端创建一个包含日文字符(UTF-8 字符集)的 CSV 文件。
代码:
public void ExportToCsv_Click(object sender, EventArgs e)
{
try
{
string sContents = string.Empty;
string sTemp;
for (int k = 1; k <= (ObjList.Columns.Count - 1); k++)
{
sContents += ObjList.HeaderRow.Cells[k].Text + ",";
}
sContents += "\r\n";
for (int i = 0; i <= (ObjList.Rows.Count - 1); i++)
{
for (int j = 1; j <= (ObjList.Columns.Count - 1); j++)
{
sTemp = (ObjList.Rows[i].Cells[j].Text.ToString());
if (sTemp == " ")
sTemp = "";
if (j < ObjList.Columns.Count - 1)
sTemp = (sTemp + ",");
sContents += sTemp;
}
sContents += "\r\n";
}
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("Content-disposition", "attachment; filename=" + "partslist.csv");
Response.ContentType = "application/csv";
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.UTF8 ;
Response.Write(sContents);
Response.Flush();
Response.End();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
但是,当在 Microsoft Excel 2003 中打开此文件时,它不能正确显示文件内容。
如何使excel正确打开这样的文件。帮助将不胜感激。