在线咨询: 80015105 点击进行对话
联系我们
代理商登录 代理合作
客户服务管理登录
企业/个人应用
教育行业应用
网站运营/推广
软件外包    
鸿诺软件温馨提示,请先登录或者注册: 登录 | 注册 | 搜索
将DataGrid控件中的数据导出Execl - 产品论坛 [北京鸿诺软件技术有限公司]
产品论坛 [北京鸿诺软件技术有限公司]软件外包、研究与开发技术研究将DataGrid控件中的数据导出Execl
    
 

鸿诺软件产品均提供免费下载。有任何建议,请登录发表建议或评论。鸿诺软件电话:010-51650063 QQ:80015105  帖子排序:
2008-3-29 9:48:39
it_it





发 帖 数:9
注册时间:2008-3-29
将DataGrid控件中的数据导出Execl


上述方法虽然实现了导出的功能,但同时把按钮、分页框等html中的所有输出信息导了进去。而我们一般要导出的是数据,DataGrid控件上的数据。<DIV class=quote>System.Web.UI.Control ctl=this.DataGrid1;
//DataGrid1是你在窗体中拖放的控件
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";
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();</DIV>

如果你的DataGrid用了分页,它导出的是当前页的信息,也就是它导出的是DataGrid中显示的信息。而不是你select语句的全部信息。

为方便使用,写成方法如下:

<DIV class=quote>public void DGToExcel(System.Web.UI.Control ctl)  
{
   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";
   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();
}</DIV>

   用法:DGToExcel(datagrid1);
  
3、将DataSet中的数据导出Execl

有了上边的思路,就是将在导出的信息,输出(Response)客户端,这样就可以导出了。那么把DataSet中的数据导出,也就是把DataSet中 的表中的各行信息,以ms-excel的格式Response到http流,这样就OK了。说明:参数ds应为填充有数据表的DataSet,文件名是全 名,包括后缀名,如execl2006.xls

<DIV class=quote>

public void CreateExcel(DataSet ds,string FileName)
{
HttpResponse resp;
resp = Page.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-Disposition", "attachment;filename="+FileName);   
string colHeaders= "", ls_item="";   

//定义表对象与行对象,同时用DataSet对其值进行初始化
DataTable dt=ds.Tables[0];
DataRow[] myRow=dt.Select();//可以类似dt.Select("id>10")之形式达到数据筛选目的
        int i=0;
        int cl=dt.Columns.Count;

    
//取得数据表各列标题,各标题之间以\t分割,最后一个列标题后加回车符
for(i=0;i<cl;i++)
{
if(i==(cl-1))//最后一列,加\n
{
colHeaders +=dt.Columns.Caption.ToString() +"\n";
}
else
{
colHeaders+=dt.Columns.Caption.ToString()+"\t";
}
      
}
resp.Write(colHeaders);
//向HTTP输出流中写入取得的数据信息
   
//逐行处理数据  
foreach(DataRow row in myRow)
{     
//当前行数据写入HTTP输出流,并且置空ls_item以便下行数据    
for(i=0;i<cl;i++)
{
if(i==(cl-1))//最后一列,加\n
{
ls_item +=row.ToString()+"\n";
}
else
{
ls_item+=row.ToString()+"\t";
}
  
}
resp.Write(ls_item);
ls_item="";
    
}    
resp.End();
}

</DIV>
返回页首↑
北京鸿诺软件技术有限公司
中国 北京 海淀区罗庄西里13号东达商务写字楼2层
邮编: 100088   总机:010- 51650063/82357500
产品论坛 [北京鸿诺软件技术有限公司]