导出datagrid的内容到Excel文件中。private void BTExport_Click(object sender, System.EventArgs e)
{
string strDay = "";
strDay = DateTime.Now.Year.ToString().Substring(2,2);
if (DateTime.Now.Month < 10)
strDay += "0" + DateTime.Now.Month.ToString();
else
strDay += DateTime.Now.Month.ToString();
if (DateTime.Now.Day < 10)
strDay += "0" + DateTime.Now.Day.ToString();
else
strDay += DateTime.Now.Day.ToString();
Response.ContentType = "appication/vnd.ms-excel";
Response.ContentEncoding = Encoding.UTF8;
Response.AppendHeader("Content-Disposition","attachment;filename=" + ttpUtility.UrlEncode("导出数据"+strDay+".xls",Encoding.UTF8));
Response.Charset = "gb2312";
this.EnableViewState = true;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);this.DGRDataInfo.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
执行到this.DataGrid1.RenderControl(hw); 时出错;
错误:服务器错误:类型“DataGridLinkButton”的控件“DGRDataInfo__ctl2__ctl0”必须放在具有 runat=server 的窗体标记内。
{
string strDay = "";
strDay = DateTime.Now.Year.ToString().Substring(2,2);
if (DateTime.Now.Month < 10)
strDay += "0" + DateTime.Now.Month.ToString();
else
strDay += DateTime.Now.Month.ToString();
if (DateTime.Now.Day < 10)
strDay += "0" + DateTime.Now.Day.ToString();
else
strDay += DateTime.Now.Day.ToString();
Response.ContentType = "appication/vnd.ms-excel";
Response.ContentEncoding = Encoding.UTF8;
Response.AppendHeader("Content-Disposition","attachment;filename=" + ttpUtility.UrlEncode("导出数据"+strDay+".xls",Encoding.UTF8));
Response.Charset = "gb2312";
this.EnableViewState = true;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);this.DGRDataInfo.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
执行到this.DataGrid1.RenderControl(hw); 时出错;
错误:服务器错误:类型“DataGridLinkButton”的控件“DGRDataInfo__ctl2__ctl0”必须放在具有 runat=server 的窗体标记内。
错误:“TEXTBOX”必须放在具有 runat=server 的窗体标记内。
这是datagrid导出到Excel中代码,你也可以参考一下
Response.Clear();
Response.Buffer= true;
Response.Charset="GB2312";
Response.AppendHeader("Content-Disposition","attachment;filename=FileName.xls");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
//设置输出流为简体中文
Response.ContentType = "application/ms-excel";
//设置输出文件类型为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);
this.dgData.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
是或,那怎么解决呢>
{
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls"); HttpContext.Current.Response.Charset ="UTF-8";
HttpContext.Current.Response.ContentEncoding
=System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType
="application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword ctl.Page.EnableViewState =false;
System.IO.StringWriter tw = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter
(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
} 用法:ToExcel(datagrid1);
现在的问题是我想向里面加入一列或一行并赋值怎么办?