DataGrid中数据导出到Excel中的问题

muzili 2003-10-23 05:12:21
private void ToExcel(System.Web.UI.Control ctl,string Table_Name)
{
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+""+Table_Name+".xls");
HttpContext.Current.Response.Charset ="gb-2312";
HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default ;
HttpContext.Current.Response.ContentType ="application/ms-excel/ms-word";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
ctl.Page.EnableViewState =true;
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();
}

以上代码运行到ctl.RenderControl(hw);的时候报错:
类型“DataGridLinkButton”的控件“DataGrid1__ctl14__ctl1”必须放在具有 runat=server 的窗体标记内。
怎么回事?怎么解决?
...全文
43 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
srj911 2003-10-30
  • 打赏
  • 举报
回复
学习
wayneliu 2003-10-30
  • 打赏
  • 举报
回复
你的DataGrid是不是有模板列啊!必须先动态去掉
就好了!
neekerss 2003-10-30
  • 打赏
  • 举报
回复
此中方法是不是用excel来显示datagrid中的数据呀????
honeyrose 2003-10-30
  • 打赏
  • 举报
回复
我也出现了类似的问题:类型“DataGridLinkButton”的控件“DataGrid1__ctl2__ctl0”必须放在具有 runat=server 的窗体标记内。
阿拉丁 2003-10-27
  • 打赏
  • 举报
回复
//自己生成"<table><tr><td>aaa</td></tr></table>"格式吧 好控制
public void Export()
{

HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+""+ExportFileName);
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default ;
HttpContext.Current.Response.ContentType ="application/ms-excel/ms-word";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword

string strData = "<table border=1>";
for ( int i=-1; i< this.Items.Count ; i++ )
{
strData += "<tr>";
for ( int j=0; j< this.Columns.Count ; j++ )
{
// Igore none visible columns or not .
if ( !this.Columns[j].Visible && !ExportInvisibleColumns ) continue;

strData += "<td>";
if ( i < 0 )
strData += "<b>"+this.GetHeaderText( j )+"</b>";
else
strData += this.GetItemString( i,j );
strData += "</td>";

}
strData += "</tr>";
}
strData += "</table>";

HttpContext.Current.Response.Write( strData );
HttpContext.Current.Response.End();

}

public string GetItemString( int Row,int ColumnIndex )
{
if ( Row > this.Items.Count ) return "";
DataGridItem dgItem = this.Items[Row];
if ( ColumnIndex < 0 || ColumnIndex > dgItem.Cells.Count ) return "";

foreach( Control ctrl in dgItem.Cells[ColumnIndex].Controls )
{
if ( ctrl is CheckBox ) return (( CheckBox )ctrl).Checked.ToString();
if ( ctrl is HyperLink )return (( HyperLink )ctrl).Text;
if ( ctrl is Label ) return (( Label )ctrl).Text;
if ( ctrl is Literal ) return (( Literal )ctrl).Text;
if ( ctrl is TextBox ) return (( TextBox )ctrl).Text;
}
return this.Items[ Row ].Cells[ ColumnIndex ].Text;
}
muzili 2003-10-23
  • 打赏
  • 举报
回复
只有数据列可以导出,但是有模版列或者有分页排序等的,为什么就不行呢,该怎么解决呢?
cloud_scorpion 2003-10-23
  • 打赏
  • 举报
回复
因为你是读取的前端HTML代码来写入到excel的,在你的DataGrid控件中是否有按钮列或模板列之类的列?估计应该是导出时你把按钮列或模板列也进行导出所引起的错误。你可以试着在DataGrid控件中只有数据列来看看
muzili 2003-10-23
  • 打赏
  • 举报
回复
?????
muzili 2003-10-23
  • 打赏
  • 举报
回复
DataGrid本来就是runat = server的

62,072

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧