中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

C#導(dǎo)出數(shù)據(jù)到Excel或者Word中的代碼片段

2018-07-20    來源:open-open

容器云強(qiáng)勢上線!快速搭建集群,上萬Linux鏡像隨意使用
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();
        }
 
  
 
  
 
public void DataGridToExcel(DataGrid grdTemp,DataSet dsTemp)
        {
            grdTemp.AllowPaging=false;   //設(shè)置不能分頁
 
            grdTemp.DataSource=dsTemp;  //重新綁定數(shù)據(jù)源
            grdTemp.DataBind();
    
            //常規(guī)導(dǎo)出方法
 
            System.IO.StringWriter SW = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter HTW=new System.Web.UI.HtmlTextWriter(SW);
            grdTemp.RenderControl(HTW);
 
            //Page為要導(dǎo)出的對象,當(dāng)前是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)
            //進(jìn)行進(jìn)行編碼,以解決文件名亂碼的問題
            Response.Write(SW.ToString());
            Response.Flush();
            Response.Close();
        }
 
  
 
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();
        }

標(biāo)簽: isp

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:C#動態(tài)生成水印圖片的類

下一篇:C#對IIS進(jìn)行操作的代碼