c# 怎么把datagirdview里的数据导入excel

发布网友 发布时间: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();

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com