精品丰满熟女一区二区三区_五月天亚洲欧美综合网_亚洲青青青在线观看_国产一区二区精选

  • <menu id="29e66"></menu>

    <bdo id="29e66"><mark id="29e66"><legend id="29e66"></legend></mark></bdo>

  • <pre id="29e66"><tt id="29e66"><rt id="29e66"></rt></tt></pre>

      <label id="29e66"></label><address id="29e66"><mark id="29e66"><strike id="29e66"></strike></mark></address>
      學習啦 > 學習電腦 > 工具軟件 > 辦公軟件學習 > Excel教程 > Excel2007教程 > asp.net如何導出excel2007

      asp.net如何導出excel2007

      時間: 嘉銘873 分享

      asp.net如何導出excel2007

        asp.net如何導出到excel2007?下面讓學習啦小編為你帶來asp.net導出excel2007的方法。

        asp.net導出excel2007方法:

          在asp.net中導出Execl有兩種方法,一種是將導出的文件存放在服務器某個文件夾下面,然后將文件地址輸出在瀏覽器上;一種是將文件直接將文件輸出流寫給瀏覽器。在Response輸出時,t分隔的數(shù)據,導出execl時,等價于分列,n等價于換行。

        1、將整個html全部輸出execl

        此法將html中所有的內容,如按鈕,表格,圖片等全部輸出到Execl中。

        Response.Clear();

        Response.Buffer= true;

        Response.AppendHeader("Content-Disposition","attachment;filename="+DateTime.Now.ToString("yyyyMMdd")+".xls");

        Response.ContentEncoding=System.Text.Encoding.UTF8;

        Response.ContentType = "application/vnd.ms-excel";

        this.EnableViewState = false;

        這里我們利用了ContentType屬性,它默認的屬性為text/html,這時將輸出為超文本,即我們常見的網頁格式到客戶端,如果改為ms-excel將將輸出excel格式,也就是說以電子表格的格式輸出到客戶端,這時瀏覽器將提示你下載保存。ContentType的屬性還包括:image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword 。同理,我們也可以輸出(導出)圖片、word文檔等。下面的方法,也均用了這個屬性。

        2、將DataGrid控件中的數(shù)據導出Execl

        上述方法雖然實現(xiàn)了導出的功能,但同時把按鈕、分頁框等html中的所有輸出信息導了進去。而我們一般要導出的是數(shù)據,DataGrid控件上的數(shù)據。

        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();

        如果你的DataGrid用了分頁,它導出的是當前頁的信息,也就是它導出的是DataGrid中顯示的信息。而不是你select語句的全部信息。

        為方便使用,寫成方法如下:

        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();

        }

        用法:DGToExcel(datagrid1);

        3、將DataSet中的數(shù)據導出Execl

        有了上邊的思路,就是將在導出的信息,輸出(Response)客戶端,這樣就可以導出了。那么把DataSet中的數(shù)據導出,也就是把DataSet中的表中的各行信息,以ms-excel的格式Response到http流,這樣就OK了。說明:參數(shù)ds應為填充有數(shù)據表的DataSet,文件名是全名,包括后綴名,如execl2006.xls

        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")之形式達到數(shù)據篩選目的

        int i=0;

        int cl=dt.Columns.Count;

        //取得數(shù)據表各列標題,各標題之間以t分割,最后一個列標題后加回車符

        for(i=0;i<cl;i++)

        {

        if(i==(cl-1))//最后一列,加n

        {

        colHeaders +=dt.Columns[i].Caption.ToString() +"n";

        }

        else

        {

        colHeaders+=dt.Columns[i].Caption.ToString()+"t";

        }

        }

        resp.Write(colHeaders);

        //向HTTP輸出流中寫入取得的數(shù)據信息

        //逐行處理數(shù)據

        foreach(DataRow row in myRow)

        {

        //當前行數(shù)據寫入HTTP輸出流,并且置空ls_item以便下行數(shù)據

        for(i=0;i<cl;i++)

        {

        if(i==(cl-1))//最后一列,加n

        {

        ls_item +=row[i].ToString()+"n";

        }

        else

        {

        ls_item+=row[i].ToString()+"t";

        }

        }

        resp.Write(ls_item);

        ls_item="";

        }

        resp.End();

        }

      關于asp.net導出excel的相關文章推薦:

      1.將asp圖片導出Excel的方法

      2.怎么將ASP.NET導出Excel表格

      3.如何將asp中把數(shù)據導出為excel

      1448026