校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁(yè) > 知識(shí)庫(kù) > ASP.NET導(dǎo)出數(shù)據(jù)到Excel的實(shí)現(xiàn)方法

ASP.NET導(dǎo)出數(shù)據(jù)到Excel的實(shí)現(xiàn)方法

熱門標(biāo)簽:鄭州400電話辦理 聯(lián)通 五常地圖標(biāo)注 凱立德導(dǎo)航官網(wǎng)地圖標(biāo)注 地圖標(biāo)注和認(rèn)領(lǐng) 戶外地圖標(biāo)注軟件手機(jī)哪個(gè)好用 長(zhǎng)春呼叫中心外呼系統(tǒng)哪家好 電銷語(yǔ)音自動(dòng)機(jī)器人 智能電話營(yíng)銷外呼系統(tǒng) 萊蕪?fù)夂綦婁N機(jī)器人價(jià)格
網(wǎng)上好些代碼的原理大致與此類似,同樣都存在一個(gè)問(wèn)題,就是:
  類型“GridView”的控件“ctl00_center_GridView1”必須放在具有 runat=server 的窗體標(biāo)記內(nèi)。 說(shuō)明: 執(zhí)行當(dāng)前 Web 請(qǐng)求期間,出現(xiàn)未處理的異常。請(qǐng)檢查堆棧跟蹤信息,以了解有關(guān)該錯(cuò)誤以及代碼中導(dǎo)致錯(cuò)誤的出處的詳細(xì)信息。 異常詳細(xì)信息:System.Web.HttpException: 類型“GridView”的控件“ctl00_center_GridView1”必須放在具有 runat=server 的窗體標(biāo)記內(nèi)。
  這段錯(cuò)誤描述是我在注釋了這段程序是報(bào)的錯(cuò),
復(fù)制代碼 代碼如下:

//publicoverridevoidVerifyRenderingInServerForm(Controlcontrol)
//{
//  //base.VerifyRenderingInServerForm(control);
//}

  雖然這個(gè)方法里的內(nèi)容也被注釋了,也就是說(shuō)這是個(gè)空方法,但是如果沒(méi)有個(gè)方法,程序就會(huì)報(bào)上面那個(gè)錯(cuò)誤。最初見(jiàn)到這段錯(cuò)誤說(shuō)明是想到了以前做ajax程序時(shí)報(bào)的一個(gè)錯(cuò)誤很是類似。同樣是因?yàn)闆](méi)有重寫VerifyRenderingInServerForm方法所致。在此提醒使用的朋友注意,下面貼出導(dǎo)出到Excel的代碼
復(fù)制代碼 代碼如下:

usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Collections;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
usingSystem.IO;
///summary>
///ToExcleHelper的摘要說(shuō)明
////summary>
  publicclassExportHelper
  {
    publicstaticvoidExportToExcel(IListdataList,string[]fields,string[]headTexts,stringtitle)
    {
      GridViewgvw=newGridView();
      intColCount,i;
      //如果篩選的字段和對(duì)應(yīng)的列頭名稱個(gè)數(shù)相對(duì)的情況下只導(dǎo)出指定的字段
      if(fields.Length!=0fields.Length==headTexts.Length)
      {
        ColCount=fields.Length;
        gvw.AutoGenerateColumns=false;
        for(i=0;iColCount;i++)
        {
          BoundFieldbf=newBoundField();
          bf.DataField=fields[i];
          bf.HeaderText=headTexts[i];
          gvw.Columns.Add(bf);
        }
      }
      else
      {
        gvw.AutoGenerateColumns=true;
      }
      SetStype(gvw);
      gvw.DataSource=dataList;
      gvw.DataBind();
      ExportToExcel(gvw,title);
    }
    ///summary>
    ///導(dǎo)出數(shù)據(jù)到Excel
    ////summary>
    ///paramname="DataList">IListData/param>
    ///paramname="Fields">要導(dǎo)出的字段/param>
    ///paramname="HeadName">字段對(duì)應(yīng)顯示的名稱/param>
    publicstaticvoidExportToExcel(IListdataList,string[]fields,string[]headTexts)
    {
      ExportToExcel(dataList,fields,headTexts,string.Empty);
    }
    ///summary>
    ///設(shè)置樣式
    ////summary>
    ///paramname="gvw">/param>
    privatestaticvoidSetStype(GridViewgvw)
    {
      gvw.Font.Name="Verdana";
      gvw.BorderStyle=System.Web.UI.WebControls.BorderStyle.Solid;
      gvw.HeaderStyle.BackColor=System.Drawing.Color.LightCyan;
      gvw.HeaderStyle.ForeColor=System.Drawing.Color.Black;
      gvw.HeaderStyle.HorizontalAlign=System.Web.UI.WebControls.HorizontalAlign.Center;
      gvw.HeaderStyle.Wrap=false;
      gvw.HeaderStyle.Font.Bold=true;
      gvw.HeaderStyle.Font.Size=10;
      gvw.RowStyle.Font.Size=10;
    }
    ///summary>
    ///導(dǎo)出GridView中的數(shù)據(jù)到Excel
    ////summary>
    ///paramname="gvw">/param>
    ///paramname="DataList">/param>
    publicstaticvoidExportToExcel(GridViewgvw,stringtitle)
    {
      stringfileName;
      HttpContext.Current.Response.Buffer=true;
      HttpContext.Current.Response.ClearContent();
      HttpContext.Current.Response.ClearHeaders();
      fileName=string.Format("xhmd{0:yyMMddHHmm}.xls",DateTime.Now);
      HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+fileName);
      HttpContext.Current.Response.ContentType="application/vnd.ms-excel";
      StringWritertw=newSystem.IO.StringWriter();
      HtmlTextWriterhw=newSystem.Web.UI.HtmlTextWriter(tw);
      gvw.RenderControl(hw);
      if(!string.IsNullOrEmpty(title))
      {
        HttpContext.Current.Response.Write("b>center>fontsize=3face=Verdanacolor=#0000FF>"+title+"/font>/center>/b>");
      }
      HttpContext.Current.Response.Write(tw.ToString());
      HttpContext.Current.Response.Flush();
      HttpContext.Current.Response.Close();
      HttpContext.Current.Response.End();
      gvw.Dispose();
      tw.Dispose();
      hw.Dispose();
      gvw=null;
      tw=null;
      hw=null;
    }
    publicstaticvoidDataTable2Excel(System.Data.DataTabledtData)
    {
      System.Web.UI.WebControls.DataGriddgExport=null;
     //當(dāng)前對(duì)話
      System.Web.HttpContextcurContext=System.Web.HttpContext.Current;
      //IO用于導(dǎo)出并返回excel文件
      System.IO.StringWriterstrWriter=null;
      System.Web.UI.HtmlTextWriterhtmlWriter=null;
      if(dtData!=null)
     {
        //設(shè)置編碼和附件格式
       curContext.Response.ContentType="application/vnd.ms-excel";
       curContext.Response.ContentEncoding=System.Text.Encoding.UTF8;
       curContext.Response.Charset="";
        
        //導(dǎo)出excel文件
       strWriter=newSystem.IO.StringWriter();
       htmlWriter=newSystem.Web.UI.HtmlTextWriter(strWriter);
       //為了解決dgData中可能進(jìn)行了分頁(yè)的情況,需要重新定義一個(gè)無(wú)分頁(yè)的DataGrid
        dgExport=newSystem.Web.UI.WebControls.DataGrid();
        dgExport.DataSource=dtData.DefaultView;
        dgExport.AllowPaging=false;
        dgExport.DataBind();
        //返回客戶端
        dgExport.RenderControl(htmlWriter);  
        curContext.Response.Write(strWriter.ToString());
        curContext.Response.End();
      }
    }
  }
您可能感興趣的文章:
  • 直接在線預(yù)覽Word、Excel、TXT文件之ASP.NET
  • ASP.NET使用GridView導(dǎo)出Excel實(shí)現(xiàn)方法
  • asp.net導(dǎo)出excel數(shù)據(jù)的常見(jiàn)方法匯總
  • Asp.net導(dǎo)出Excel/Csv文本格式數(shù)據(jù)的方法
  • Asp.Net使用Npoi導(dǎo)入導(dǎo)出Excel的方法
  • asp.net導(dǎo)出Excel亂碼的原因及解決方法
  • asp.net使用npoi讀取excel模板并導(dǎo)出下載詳解
  • .Net中導(dǎo)出數(shù)據(jù)到Excel(asp.net和winform程序中)
  • asp.net生成Excel并導(dǎo)出下載五種實(shí)現(xiàn)方法
  • ASP.NET導(dǎo)出Excel打開(kāi)時(shí)提示:與文件擴(kuò)展名指定文件不一致解決方法
  • asp.net中如何批量導(dǎo)出access某表內(nèi)容到word文檔
  • asp.net 按指定模板導(dǎo)出word,pdf實(shí)例代碼
  • asp.net+Ligerui實(shí)現(xiàn)grid導(dǎo)出Excel和Word的方法

標(biāo)簽:岳陽(yáng) 衢州 西寧 紅河 福州 湖州 西藏 宣城

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《ASP.NET導(dǎo)出數(shù)據(jù)到Excel的實(shí)現(xiàn)方法》,本文關(guān)鍵詞  ASP.NET,導(dǎo)出,數(shù)據(jù),到,Excel,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《ASP.NET導(dǎo)出數(shù)據(jù)到Excel的實(shí)現(xiàn)方法》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于ASP.NET導(dǎo)出數(shù)據(jù)到Excel的實(shí)現(xiàn)方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 安泽县| 镇宁| 乌拉特中旗| 屯昌县| 青河县| 光山县| 绥芬河市| 郑州市| 徐闻县| 徐汇区| 祁门县| 宜昌市| 固安县| 特克斯县| 达州市| 安仁县| 禄丰县| 大英县| 定日县| 德州市| 镇巴县| 马山县| 英超| 泸定县| 桦南县| 海门市| 冀州市| 应用必备| 徐州市| 鹤峰县| 馆陶县| 莱州市| 福州市| 永泰县| 阿克陶县| 蓬溪县| 衢州市| 合作市| 桦南县| 桑植县| 灵石县|