1、从 数据库 中 导出 excel 表格
定义 一个 gridview1
protected void Button1_Click(object sender, EventArgs e) //倒出数据
{
if (GridView1.Rows.Count == 0)
{
return;
}
else
{
GridView1.AllowPaging = false;//先将数据分页取消,才能全部导出数据
bind();
Export("application/ms-excel", "FileName.xls", GridView1);
GridView1.AllowPaging = true;//绑定完数据后,才重新绑定数据
bind();
}
public void Export(string fileType, string fileName, GridView gd)//数据导出函数
{
Response.Clear();
Response.Buffer = true;
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-7");
HttpContext.Current.Response.ContentType = fileType;
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF7));
gd.Page.EnableViewState = false;
System.IO.StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
gd.RenderControl(hw);
Response.Output.Write(tw.ToString());
Response.Flush();
Response.End();
}
//如果没有下面方法会报错类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内
public override void VerifyRenderingInServerForm(Control control)
{
}
2、从excel 导入到 数据库 中