依旧是留下代码防止以后忘记
protected void Export_Click(object sender, EventArgs e) { DataSet data = "" //数据获取 DataTable dt = data.Tables[0]; DataRow[] myrow = dt.Select(); Response.Clear(); Response.ContentType = "application/vnd.ms-excel"; Response.Charset = "gb2312"; Response.ContentEncoding = Encoding.GetEncoding("gb2312"); //用其他编码格式会出现中文乱码的情况 Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("自定义文件名称") + ".xls")); int cl = dt.Columns.Count; string colhead=""; //列标签 string item = ""; //行 for (int i = 0; i < cl; i++) { if (i == cl - 1) { colhead +=dt.Columns[i].Caption.ToString()+"\n"; } else { colhead += dt.Columns[i].Caption.ToString() + "\t"; } } Response.Write(colhead); foreach (DataRow row in myrow) { for (int i = 0; i < cl; i++) { if (i == cl - 1) { item += row[i].ToString() + "\n"; } else { item += row[i].ToString() + "\t"; } } Response.Write(item); item = ""; } //Response.Write("<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=gb2312\"/>"); //防止中文乱码 Response.End(); }