private void DG_TO_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/vnd.ms-excel";
//Response.ContentType="application/vnd.ms-word"; //导到word
            
Response.Charset="";
            
this.EnableViewState=false;
            System.Io.StringWriter str
=new System.Io.StringWriter();
            System.Web.UI.HtmlTextWriter htw
=new System.Web.UI.HtmlTextWriter(str);
//this.ClearControls(DataGrid1);
            
DataGrid1.RenderControl(htw);
            Response.write(str.ToString());
            Response.End();
}


Private wid clearcontrols(control control)
{
for(int i=control.controls.Count-1; i>=0;i--)
{
clearControls(control.controls[i])
{
if(!(control is Tablecell))
{
if(control.GetType().Getproperty("SelectedItem")!=null)
Literalcontrol literal
=new LiteralControl);
control.Parent.Controls.Add(literal);
try
{
literal.Text
=(string)control.GetType().Getproperty("SelectedItem").GetValue(control.null);
}

catch
{
throw;
}

control.Parert.Controls.Remove(control);
}

else if(control.GetType().Getproperty("Text")!=null)
{
LiteralControl literal
=new LiteralControl();
control.Parent.controls.Add(literal);
literal.Text
=(string)control.GetTypel().Getproperty("Text").GetValue(control,null)
control.parent.Controls.Remove(control);
}

}

return;
}
posted on 2006-12-08 21:21  huazi4995  阅读(213)  评论(0编辑  收藏  举报