同桌上课用手指进去了好爽_欧美丰满熟妇xxⅹⅹ性大i_成人av天天日天天拍拍_猛男gay帅男gay男男同志_欧美va天堂在线观看_人妻无码av中文系列三里桃花_亚欧免费无码在线观看_久久久精品国产亚洲av水_日韩在线免费看污污污_2021无码专区人妻系列日韩

首頁 優(yōu)化推廣 C# 將數(shù)據(jù)導(dǎo)出到Execl匯總 .

C# 將數(shù)據(jù)導(dǎo)出到Execl匯總 .

來源: | 時(shí)間:2013/6/28 23:16:22 |

一、asp.net中導(dǎo)出Execl的方法:

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

此法將html中所有的內(nèi)容,如按鈕,表格,圖片等全部輸出到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屬性,它默認(rèn)的屬性為text/html,這時(shí)將輸出為超文本,即我們常見的網(wǎng)頁格式到客戶端,如果改為ms-excel將將輸出excel格式,也就是說以電子表格的格式輸出到客戶端,這時(shí)瀏覽器將提示你下載保存。ContentType的屬性還包括:image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword 。同理,我們也可以輸出(導(dǎo)出)圖片、word文檔等。下面的方法,也均用了這個(gè)屬性。

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

上述方法雖然實(shí)現(xiàn)了導(dǎo)出的功能,但同時(shí)把按鈕、分頁框等html中的所有輸出信息導(dǎo)了進(jìn)去。而我們一般要導(dǎo)出的是數(shù)據(jù),DataGrid控件上的數(shù)據(jù)。
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用了分頁,它導(dǎo)出的是當(dāng)前頁的信息,也就是它導(dǎo)出的是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ù)據(jù)導(dǎo)出Execl

有了上邊的思路,就是將在導(dǎo)出的信息,輸出(Response)客戶端,這樣就可以導(dǎo)出了。那么把DataSet中的數(shù)據(jù)導(dǎo)出,也就是把DataSet中的表中的各行信息,以ms-excel的格式Response到http流,這樣就OK了。說明:參數(shù)ds應(yīng)為填充有數(shù)據(jù)表的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="";  
 
 //定義表對象與行對象,同時(shí)用DataSet對其值進(jìn)行初始化
 DataTable dt=ds.Tables[0];
 DataRow[] myRow=dt.Select();//可以類似dt.Select("id>10")之形式達(dá)到數(shù)據(jù)篩選目的
        int i=0;
        int cl=dt.Columns.Count;
   
 //取得數(shù)據(jù)表各列標(biāo)題,各標(biāo)題之間以t分割,最后一個(gè)列標(biāo)題后加回車符
 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ù)據(jù)信息
  
 //逐行處理數(shù)據(jù)  
 foreach(DataRow row in myRow)
 {    
 //當(dāng)前行數(shù)據(jù)寫入HTTP輸出流,并且置空ls_item以便下行數(shù)據(jù)    
 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(); 
 }

4、將dataview導(dǎo)出execl
若想實(shí)現(xiàn)更加富于變化或者行列不規(guī)則的execl導(dǎo)出時(shí),可用本法。
public void OutputExcel(DataView dv,string str)
{
   //dv為要輸出到Excel的數(shù)據(jù),str為標(biāo)題名稱
   GC.Collect();
   Application excel;// = new Application();
   int rowIndex=4;
   int colIndex=1;
 
   _Workbook xBk;
   _Worksheet xSt;
 
   excel= new ApplicationClass();
  
   xBk = excel.Workbooks.Add(true);
   
   xSt = (_Worksheet)xBk.ActiveSheet;
 
   //
   //取得標(biāo)題
   //
   foreach(DataColumn col in dv.Table.Columns)
   {
    colIndex++;
    excel.Cells[4,colIndex] = col.ColumnName;
    xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[4,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//設(shè)置標(biāo)題格式為居中對齊
   }
 
   //
   //取得表格中的數(shù)據(jù)
   //
   foreach(DataRowView row in dv)
   {
    rowIndex ++;
    colIndex = 1;
    foreach(DataColumn col in dv.Table.Columns)
    {
     colIndex ++;
     if(col.DataType == System.Type.GetType("System.DateTime"))
     {
      excel.Cells[rowIndex,colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
      xSt.get_Range(excel.Cells[rowIndex,col

服務(wù)熱線

153 8323 9821

功能和特性

價(jià)格和優(yōu)惠

網(wǎng)站和維護(hù)

推廣和優(yōu)化

微信服務(wù)號