Excel导入导出
近来在项目中经常使用Excel的导入导出方法,经常在网上搜有关的文章,也做了一些试验。现把学习Excel的导入导出的方法记录下来,以备日后查看和与大家交流。
Excel 导出方法基本有以下几种:
1、使用GridView绑定数据,通过Response.write()将GridView中的数据以流的方式输出到Excel中,这种方法执行效率较快,使用起来也很方便,我比较喜欢用。代码如下:
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=filename.xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-7");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView1.AllowPaging = false;
GridViewBind();
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();