五月综合缴情婷婷六月,色94色欧美sute亚洲线路二,日韩制服国产精品一区,色噜噜一区二区三区,香港三级午夜理伦三级三

您現(xiàn)在的位置: 365建站網(wǎng) > 365文章 > GridView數(shù)據(jù)導(dǎo)入到Excel表中

GridView數(shù)據(jù)導(dǎo)入到Excel表中

文章來源:365jz.com     點(diǎn)擊數(shù):772    更新時間:2009-09-18 10:25   參與評論

(1)StringWriter 不能用,因為沒有引入命名空間System.IO

(2)Encoding不能用,因為沒有引入命名空間System.Text

(3)在用戶控件上使用的GridView不能重寫VerifyRenderingInServerForm函數(shù)。

 

一、GridView數(shù)據(jù)導(dǎo)出到Excel文件中

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GridView1.DataSource = getDataSet();
                GridView1.DataBind();
            }
        }

        //filename為Excel的名字,ToExcelGrid就是數(shù)據(jù)源,在此為DataView數(shù)據(jù)源;
        private void ExportExcelFromDataGrid(string filename, System.Web.UI.WebControls.GridView ToExcelGrid)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "utf-8";
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(filename));
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");//設(shè)置輸出流為簡體中文  
            Response.ContentType = "application/ms-excel";//設(shè)置輸出文件類型為excel文件。    

            this.EnableViewState = false;
            System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            ToExcelGrid.RenderControl(oHtmlTextWriter);
            Response.Write(oStringWriter.ToString());
            Response.End();
        }

        //如果沒有下面方法會報錯類型“GridView”的控件“GridView1”必須放在具有 runat=server 的窗體標(biāo)記內(nèi)ITPUB個人空間 cB6w"i'J/n

        public override void VerifyRenderingInServerForm(Control control)
        {
            // Confirms that an HtmlForm control is rendered for
            //里邊不寫任何的東西
        }
 protected void btnWrite_Click(object sender, EventArgs e)
        {
            ExportExcelFromDataGrid("TestExcel.xls", this.GridView1);
        }

}  

如對本文有疑問,請?zhí)峤坏浇涣髡搲瑥V大熱心網(wǎng)友會為你解答??! 點(diǎn)擊進(jìn)入論壇

發(fā)表評論 (772人查看0條評論)
請自覺遵守互聯(lián)網(wǎng)相關(guān)的政策法規(guī),嚴(yán)禁發(fā)布色情、暴力、反動的言論。
昵稱:
最新評論
------分隔線----------------------------

其它欄目

· 建站教程
· 365學(xué)習(xí)

業(yè)務(wù)咨詢

· 技術(shù)支持
· 服務(wù)時間:9:00-18:00
365建站網(wǎng)二維碼

Powered by 365建站網(wǎng) RSS地圖 HTML地圖

copyright © 2013-2024 版權(quán)所有 鄂ICP備17013400號