DataGrid中数据导出到Excel中的问题 DataGrid本来就是runat = server的 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 因为你是读取的前端HTML代码来写入到excel的,在你的DataGrid控件中是否有按钮列或模板列之类的列?估计应该是导出时你把按钮列或模板列也进行导出所引起的错误。你可以试着在DataGrid控件中只有数据列来看看 //自己生成"<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/mswordstring 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;} 我也出现了类似的问题:类型“DataGridLinkButton”的控件“DataGrid1__ctl2__ctl0”必须放在具有 runat=server 的窗体标记内。 此中方法是不是用excel来显示datagrid中的数据呀???? 你的DataGrid是不是有模板列啊!必须先动态去掉就好了! asp.net 控件查找 调用外面网站的webservice PetShop4.0的一些问题 关于同一网站目录下的session共享问题 asp.net使用非托管socket组件问题 请帮我把如下方法用类写出来.急用.分不够再加 急!急!急!一个从数据库读取文件的问题 请帮看一下这段代码,为什么保存不了datagrid编辑后的值。 ASP.net操作IIS高手请进!高分!!急急 大家帮我看看这段代码有什么错误 用VB.NET写ASP.NET程序怎么才不会把代码编译到DLL文件里? 现在有一个问题:
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/mswordstring 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;
}
就好了!