发布网友 发布时间:2022-04-22 08:41
共1个回答
热心网友 时间:2023-06-26 02:47
先用datatable将datagirdview里的数据添加进来
DataTable
dt
=
new
DataTable();
table.Columns.Add("列名",”类型“);//这个不会的自己上网查
table.Columns.Add();
DataRow
row
=
dt.NewRow();
row[0]
=
。。
row[1]
=
。。
dt.Rows.Add(row);
GridView
gvTemp
=
new
GridView();
gvTemp.DataSource
=
dt;
gvTemp.DataBind();
Response.Clear();
Response.Buffer
=
true;
Response.Charset
=
"UTF8";
Response.AppendHeader("Content-Disposition",
"attachment;filename=aa.xls");
//设置输出流为简体中文
Response.ContentEncoding
=
System.Text.Encoding.UTF8;
//设置输出文件类型为Excel文件
Response.ContentType
=
"application/ms-excel";
this.EnableViewState
=
false;
System.Globalization.CultureInfo
myCItrad
=
new
System.Globalization.CultureInfo("ZH-CN",
true);
System.IO.StringWriter
oStringWriter
=
new
System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter
oHtmlTextWriter
=
new
System.Web.UI.HtmlTextWriter(oStringWriter);
gvTemp.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();