1.如果生成的EXCEL表无数据,是绑定出错
2. 含模板列里的控件,可以设为 visible=false;
3.某些事先设为 visible=false的字段不能出来,可以另用一个gridview绑定,其 CssClass="hidden"
4. autogeneratecolumn=false;
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "p_w_upload;filename=" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls");
Response.ContentEncoding = System.Text.Encoding.UTF7;
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);
gvexcel.AllowPaging = false;
//进行绑定操作。。
string connString = "server=.; uid=skdb; pwd=skdb; database=skdb";
SqlConnection conn = new SqlConnection(connString);
SqlDataAdapter sda = new SqlDataAdapter(str1, conn);
DataTable result = new DataTable();
sda.Fill(result);
gvexcel.DataSource = result;
gvexcel.DataBind();
gvexcel.RenderControl(oHtmlTextWriter);
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
转载于:https://blog.51cto.com/davidangel/813065