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

首頁 > 網站 > 建站經驗 > 正文

ASP.NET(C#)將數、據導出到Word或Excel

2019-11-02 14:19:06
字體:
來源:轉載
供稿:網友

   最簡單的方法是把頁面上所有的東西都導出

  在載入時調用,注意頁面里不能有其它控件,包括按鈕

  void converttoexcel()

  {

  Response.Clear();

  Response.Buffer = true;

  Response.Charset = "GB2312";

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

  Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

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

  this.Page.EnableViewState = false;

  System.IO.StringWriter oStringWriter = new System.IO.StringWriter();

  System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

  this.Page.RenderControl(oHtmlTextWriter);

  Response.Write(oStringWriter.ToString());

  Response.End();

  }

  ASP.NET(C#)將數據導出到WordExcel

  命名空間:

  using System.IO;

  using System.Text;

  將DataGrid的數據導出到Excel

  string excelname="excel文件名";

  HttpContext.Current.Response.Charset = "GB2312";

  HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;

  HttpContext.Current.Response.ContentType = "application/ms-excel";

  HttpContext.Current.Response.AppendHeader("Content-disposition", "attachment;filename=" + excelname + ".xls");

  dr1.Page.EnableViewState = false;

  StringWriter sw = new StringWriter();

  HtmlTextWriter tw = new HtmlTextWriter(sw);

  dr1.RenderControl(tw);

  HttpContext.Current.Response.Write(sw.ToString());

  HttpContext.Current.Response.End();

  將DataGrid的數據導出到Word

  string excelname="word文件名";

  HttpContext.Current.Response.Charset = "GB2312";

  HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;

  HttpContext.Current.Response.ContentType = "application/ms-winword";

  HttpContext.Current.Response.AppendHeader("Content-disposition", "attachment;filename=" + excelname + ".doc");

  dr1.Page.EnableViewState = false;

  StringWriter sw = new StringWriter();

  HtmlTextWriter tw = new HtmlTextWriter(sw);

  dr1.RenderControl(tw);

  HttpContext.Current.Response.Write(sw.ToString());

  HttpContext.Current.Response.End();

  ASP.NET 2.0,C#----利用GridView控件導出其他文件(導出Excel,導出Word文件)

  // 注意,在Visual Studio2005平臺下,如果使用GridView導出文件,

  //就必須重載VerifyRenderingInServerForm方法

  public override void VerifyRenderingInServerForm(Control control)

  {

  }

  ///

  /// 導出到文件的方法,

  ///

  /// Model=1:導出為Execl,Model=2:導出為Word

  private void toFiles(int Model)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 于田县| 禄劝| 曲阜市| 刚察县| 龙游县| 桓台县| 隆尧县| 龙胜| 汾阳市| 瓦房店市| 辽宁省| 闸北区| 大石桥市| 辽阳县| 太和县| 岳西县| 甘孜| 临澧县| 高雄市| 奉节县| 边坝县| 潜山县| 湖口县| 博罗县| 龙山县| 乌拉特中旗| 庆云县| 昭通市| 浪卡子县| 格尔木市| 游戏| 宁远县| 武陟县| 临汾市| 宝鸡市| 新源县| 平湖市| 杨浦区| 新源县| 黔西县| 广安市|