匯出Excel 的幾種方法。

來源:互聯網
上載者:User
一、asp.net中匯出Execl的方法:

在asp.net中匯出Execl有兩種方法,一種是將匯出的檔案存放在伺服器某個檔案夾下面,然後將檔案地址輸出在瀏覽器上;一種是將檔案直接將檔案輸出資料流寫給瀏覽器。在Response輸出時,t分隔的資料,匯出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控制項中的資料匯出Execl

上述方法雖然實現了匯出的功能,但同時把按鈕、分頁框等html中的所有輸出資訊導了進去。而我們一般要匯出的是資料,DataGrid控制項上的資料。
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中的資料匯出Execl

有了上邊的思路,就是將在匯出的資訊,輸出(Response)用戶端,這樣就可以匯出了。那麼把DataSet中的資料匯出,也就是把DataSet中的表中的各行資訊,以ms-excel的格式Response到http流,這樣就OK了。說明:參數ds應為填充有資料表的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")之形式達到資料篩選目的
        int i=0;
        int cl=dt.Columns.Count;
   
//取得資料表各欄位標題,各標題之間以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輸出資料流中寫入取得的資料資訊
 
//逐行處理資料 
foreach(DataRow row in myRow)
{   
//當前行資料寫入HTTP輸出資料流,並且置空ls_item以便下行資料   
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();
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.