DataGrid数据导入excel或word

本文介绍了一种使用C#从数据库获取数据并将其填充到DataGrid中,随后将DataGrid内容导出为Excel文件的方法。具体步骤包括连接数据库、填充数据集、绑定DataGrid以及通过响应对象将内容输出为Excel格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以下的C#代码:
        private void Page_Load(object sender, System.EventArgs e)
        
{
            SqlConnection con
=new SqlConnection("server=.;database=pubs;uid=sa;pwd=;");
            con.Open();
            SqlDataAdapter sda
=new SqlDataAdapter();
            sda.SelectCommand
=new SqlCommand("select * from txtInsert",con);
            DataSet ds
=new DataSet();
            sda.Fill(ds,
"emp");
            
this.DgSource.DataSource=ds.Tables["emp"];
            
this.DgSource.DataBind();
            con.Close();
        }

        
/// <summary>
        
/// 导出Datagrid里所有数据到Office
        
/// </summary>
        
/// <param name="grdTemp">要导出的Datagrid</param>
        
/// <param name="dsTemp">Datagrid的数据源</param>

        public void DataGridToExcel(DataGrid grdTemp,DataSet dsTemp)
        
{
            grdTemp.AllowPaging
=false;   //设置不能分页

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

            System.IO.StringWriter SW 
= new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter HTW
=new System.Web.UI.HtmlTextWriter(SW);
            grdTemp.RenderControl(HTW);

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

        
Web 窗体设计器生成的代码

        
private void Button1_Click(object sender, System.EventArgs e)
        
{
            SqlConnection con
=new SqlConnection("server=.;database=pubs;uid=sa;pwd=;");
            con.Open();
            SqlDataAdapter sda
=new SqlDataAdapter();
            sda.SelectCommand
=new SqlCommand("select * from txtInsert",con);            
            DataSet ds
=new DataSet();
            sda.Fill(ds,
"emp");
            
this.DgSource.DataSource=ds.Tables["emp"];            
            
this.DataGridToExcel(this.DgSource,ds);
            con.Close();
        }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值