惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
C
Check Point Blog
博客园_首页
B
Blog
D
Docker
U
Unit 42
量子位
I
InfoQ
有赞技术团队
有赞技术团队
Martin Fowler
Martin Fowler
GbyAI
GbyAI
L
LangChain Blog
云风的 BLOG
云风的 BLOG
博客园 - Franky
美团技术团队
T
The Blog of Author Tim Ferriss
阮一峰的网络日志
阮一峰的网络日志
月光博客
月光博客
Vercel News
Vercel News
Recent Announcements
Recent Announcements
雷峰网
雷峰网
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Google DeepMind News
Google DeepMind News

博客园 - huazi4995

prototype.js开发笔记 JS应用 java环境配置 关于梅花雪的js树 - huazi4995 - 博客园 MagicAjax的使用 - huazi4995 - 博客园 div+css制作的选项卡 - huazi4995 - 博客园 关于word文档的数据库存取 SQL查询语句精华使用简要 - huazi4995 - 博客园 身份证号码验证-支持新的带x身份证 c#常用函数和方法集汇总 动态创建层 C#操作Word文档(Office 2007) datagrid控件使用技巧大集合 网站设计常用技巧 jsp编程技巧 ArrayList的使用方法(转载) js中的小技巧 ajax+xml数据岛,增加编辑与删除按钮 利用webservice实现省、市的无刷新
将datagrid里的资料导入到excel
huazi4995 · 2006-12-08 · via 博客园 - huazi4995

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;
}