DataGrid数据导入excel或word

.net里将DataGrid中的数据导入到excel或word时,当datagrid允许分页的时候,转换会出现错误,因为没有显示的数据项没有在网页中。为了解决这个问题,可以重新建立一个临时的datagrid和dataset对象,同时指定该临时datagrid不允许分页,再将该临时datagrid中的所有数据导入excel或word中,就可以了!
以下的C#代码:
None.gif          private   void  Page_Load( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            SqlConnection con
=new SqlConnection("server=.;database=pubs;uid=sa;pwd=;");
InBlock.gif            con.Open();
InBlock.gif            SqlDataAdapter sda
=new SqlDataAdapter();
InBlock.gif            sda.SelectCommand
=new SqlCommand("select * from txtInsert",con);
InBlock.gif            DataSet ds
=new DataSet();
InBlock.gif            sda.Fill(ds,
"emp");
InBlock.gif            
this.DgSource.DataSource=ds.Tables["emp"];
InBlock.gif            
this.DgSource.DataBind();
InBlock.gif            con.Close();
ExpandedBlockEnd.gif        }

ExpandedBlockStart.gifContractedBlock.gif        
/**/ /// <summary>
InBlock.gif        
/// 导出Datagrid里所有数据到Office
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="grdTemp">要导出的Datagrid</param>
ExpandedBlockEnd.gif        
/// <param name="dsTemp">Datagrid的数据源</param>

None.gif          public   void  DataGridToExcel(DataGrid grdTemp,DataSet dsTemp)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            grdTemp.AllowPaging
=false;   //设置不能分页
InBlock.gif

InBlock.gif            grdTemp.DataSource
=dsTemp;  //重新绑定数据源
InBlock.gif
            grdTemp.DataBind();
InBlock.gif   
InBlock.gif            
//常规导出方法
InBlock.gif

InBlock.gif            System.IO.StringWriter SW 
= new System.IO.StringWriter();
InBlock.gif            System.Web.UI.HtmlTextWriter HTW
=new System.Web.UI.HtmlTextWriter(SW);
InBlock.gif            grdTemp.RenderControl(HTW);
InBlock.gif
InBlock.gif            
//Page为要导出的对象,当前是Page,如果是DataGrid,DataList等都可以
InBlock.gif
            Response.Buffer=true;
InBlock.gif            Response.Clear();
InBlock.gif            Response.ClearContent();
InBlock.gif            Response.ClearHeaders();
InBlock.gif            Response.ContentType 
= "application/vnd.ms-excel";
InBlock.gif            
//Response.ContentType是输出流的 HTTP MIME 类型
InBlock.gif            
//Response.ContentType     --- word文件
InBlock.gif            
//application/vnd.ms-excel --- excel文件
InBlock.gif            
//dot.gif
InBlock.gif
            Response.Charset="utf-8";
InBlock.gif            Response.ContentEncoding
=System.Text.Encoding.GetEncoding("utf-8");
InBlock.gif            Response.AddHeader(
"Content-Disposition""attachment;filename=aaa.xls");
InBlock.gif            
//attachment --- 作为附件下载
InBlock.gif            
//inline --- 在线打开
InBlock.gif            
//filename如过是中文,则可以用HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8)
InBlock.gif            
//进行进行编码,以解决文件名乱码的问题
InBlock.gif
            Response.Write(SW.ToString());
InBlock.gif            Response.Flush();
InBlock.gif            Response.Close();
ExpandedBlockEnd.gif        }

ContractedBlock.gifExpandedBlockStart.gif        
Web 窗体设计器生成的代码 #region Web 窗体设计器生成的代码
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.Button1.Click += new System.EventHandler(this.Button1_Click);
InBlock.gif            
this.Button2.Click += new System.EventHandler(this.Button2_Click);
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif        
#endregion

None.gif
None.gif        
private   void  Button1_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            SqlConnection con
=new SqlConnection("server=.;database=pubs;uid=sa;pwd=;");
InBlock.gif            con.Open();
InBlock.gif            SqlDataAdapter sda
=new SqlDataAdapter();
InBlock.gif            sda.SelectCommand
=new SqlCommand("select * from txtInsert",con);            
InBlock.gif            DataSet ds
=new DataSet();
InBlock.gif            sda.Fill(ds,
"emp");
InBlock.gif            
this.DgSource.DataSource=ds.Tables["emp"];            
InBlock.gif            
this.DataGridToExcel(this.DgSource,ds);
InBlock.gif            con.Close();
ExpandedBlockEnd.gif        }

转载于:https://www.cnblogs.com/mc-dragon/archive/2007/01/23/628144.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值