国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

asp.net里導出excel表方法匯總

2019-11-17 04:05:17
字體:
來源:轉載
供稿:網友
1、由dataset生成

public void CreateExcel(DataSet ds,string typeid,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="";
   int i=0;

   //定義表對象與行對像,同時用DataSet對其值進行初始化
   DataTable dt=ds.Tables[0];
   DataRow[] myRow=dt.Select("");  
   // typeid=="1"時導出為EXCEL格式文件;typeid=="2"時導出為xml格式文件
   if(typeid=="1")
   {
    //取得數據表各列標題,各標題之間以/t分割,最后一個列標題后加回車符
    for(i=0;i     colHeaders+=dt.Columns[i].Caption.ToString()+"/t";
    colHeaders +=dt.Columns[i].Caption.ToString() +"/n";    
    //向HTTP輸出流中寫入取得的數據信息
    resp.Write(colHeaders);  
    //逐行處理數據   
    foreach(DataRow row in myRow)
    {
     //在當前行中,逐列獲得數據,數據之間以/t分割,結束時加回車符/n
     for(i=0;i      ls_item +=row[i].ToString() + "/t";      
     ls_item += row[i].ToString() +"/n";
     //當前行數據寫入HTTP輸出流,并且置空ls_item以便下行數據     
     resp.Write(ls_item);
     ls_item="";
    }
   }
   else
   {
    if(typeid=="2")
    {  
     //從DataSet中直接導出XML數據并且寫到HTTP輸出流中
     resp.Write(ds.GetXml());
    }     
   }
   //寫緩沖區中的數據到HTTP頭文件中
   resp.End();


  }


2、由datagrid生成

public void ToExcel(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";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msWord
   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();
  }

用法:ToExcel(datagrid1);


3、這個用dataview

public void OutputExcel(DataView dv,string str)
{
   //
   // TODO: 在此處添加構造函數邏輯
   //
                           //dv為要輸出到Excel的數據,str為標題名稱
   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;

   //
   //取得標題
   //
   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;//設置標題格式為居中對齊
   }

   //
   //取得表格中的數據
   //
   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,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//設置日期型的字段格式為居中對齊
     }
     else
      if(col.DataType == System.Type.GetType("System.String"))
     {
      excel.Cells[rowIndex,colIndex] = "'"+row[col.ColumnName].ToString();
      xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//設置字符型的字段格式為居中對齊
     }
     else
     {
      excel.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString();
     }
    }
   }
   //
   //加載一個合計行
   //
   int rowSum = rowIndex + 1;
   int colSum = 2;
   excel.Cells[rowSum,2] = "合計";
   xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,2]).HorizontalAlignment = XlHAlign.xlHAlignCenter;
   //
   //設置選中的部分的顏色
   //
   xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Select();
   xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Interior.ColorIndex = 19;//設置為淺黃色,共計有56種
   //
   //取得整個報表的標題
   //
   excel.Cells[2,2] = str;
   //
   //設置整個報表的標題格式
   //
   xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Bold = true;
   xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Size = 22;
   //
   //設置報表表格為最適應寬度
   //
   xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Select();
   xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Columns.AutoFit();
   //
   //設置整個報表的標題為跨列居中
   //
   xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).Select();
   xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).HorizontalAlignment = XlHAlign.xlHAlignCenterAcrossSelection;
   //
   //繪制邊框
   //
   xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Borders.LineStyle = 1;
   xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,2]).Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick;//設置左邊線加粗
   xSt.get_Range(excel.Cells[4,2],excel.Cells[4,colIndex]).Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlThick;//設置上邊線加粗
   xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;//設置右邊線加粗
   xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThick;//設置下邊線加粗
   //
   //顯示效果
   //
   excel.Visible=true;

   //xSt.Export(Server.MapPath(".")+"//"+this.xlfile.Text+".xls",SheetExportActionEnum.ssExportActionNone,Microsoft.Office.Interop.OWC.SheetExportFormat.ssExportHTML);
   xBk.SaveCopyAs(Server.MapPath(".")+"//"+this.xlfile.Text+".xls");

   ds = null;
            xBk.Close(false, null,null);
    
            excel.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
            xBk = null;
            excel = null;
   xSt = null;
            GC.Collect();
   string path = Server.MapPath(this.xlfile.Text+".xls");

   System.IO.FileInfo file = new System.IO.FileInfo(path);
   Response.Clear();
   Response.Charset="GB2312";
   Response.ContentEncoding=System.Text.Encoding.UTF8;
   // 添加頭信息,為"文件下載/另存為"對話框指定默認文件名
   Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
   // 添加頭信息,指定文件大小,讓瀏覽器能夠顯示下載進度
   Response.AddHeader("Content-Length", file.Length.ToString());
    
   // 指定返回的是一個不能被客戶端讀取的流,必須被下載
   Response.ContentType = "application/ms-excel";
    
   // 把文件流發送到客戶端
   Response.WriteFile(file.FullName);
   // 停止頁面的執行
   
   Response.End();
}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 启东市| 嵊泗县| 长岛县| 寻甸| 襄樊市| 巴林左旗| 耒阳市| 肥乡县| 松原市| 周至县| 桂平市| 正定县| 东方市| 时尚| 师宗县| 昌乐县| 湖南省| 陈巴尔虎旗| 凤台县| 波密县| 西贡区| 宁波市| 临湘市| 通许县| 南宁市| 威信县| 荥阳市| 建始县| 蒙城县| 喀什市| 浠水县| 图们市| 雷州市| 永泰县| 宁河县| 筠连县| 西林县| 邵武市| 建德市| 新乐市| 香格里拉县|