ASP.NET 导出Excel后 打开Excel文件发现是乱码
大家好:最近根据需求 要我们做一个ASP.NET的导出EXCEL ,我在网上借鉴了一段代码,但是用在我这里 ,导出后打开EXCEL发现很多都是乱码 我勒个去!求各位大神们 帮忙看看 这代码哪里有问题:
以下是导出功能的代码:
public static void ExportToSpreadsheet(DataTable table, string name)
{
Random r = new Random();
string rf = "";
for (int j = 0; j < 10; j++)
{
rf = r.Next(int.MaxValue).ToString();
}
HttpContext context = HttpContext.Current;
context.Response.Clear();
context.Response.ContentType = "text/csv";
context.Response.ContentEncoding = System.Text.Encoding.UTF8 ;
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + name + rf + ".xls");
context.Response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble());
foreach (DataColumn column in table.Columns)
{
context.Response.Write(column.ColumnName + ",");
//context.Response.Write(column.ColumnName + "(" + column.DataType + "),");
}
context.Response.Write(Environment.NewLine);
double test;
foreach (DataRow row in table.Rows)