<form id="hz9zz"></form>
  • <form id="hz9zz"></form>

      <nobr id="hz9zz"></nobr>

      <form id="hz9zz"></form>

    1. 明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

      ASP.NET中將數據輸出到Excel

      [摘要]近來,在開發ISO文件管理系統的時候,曾經遇到過要將ASPX直接輸出到EXCEL的需求,現將經驗所得與大家分享。 其實,利用ASP.NET輸出指定內容的WORD、EXCEL、TXT、HTM等類型的文檔很容易的。主要分為三步來完成! ∫弧⒍x文檔類型、字符編碼Response.Clear(); R...

        近來,在開發ISO文件管理系統的時候,曾經遇到過要將ASPX直接輸出到EXCEL的需求,現將經驗所得與大家分享。 其實,利用ASP.NET輸出指定內容的WORD、EXCEL、TXT、HTM等類型的文檔很容易的。主要分為三步來完成。

        一、定義文檔類型、字符編碼

      Response.Clear();
      Response.Buffer= true;
      Response.Charset="utf-8";

      //下面這行很重要, attachment 參數表示作為附件下載,您可以改成 online在線打開

      //filename=FileFlow.xls 指定輸出文件的名稱,注意其擴展名和指定文件類型相符,可以為:.doc .xls .txt .htm

      Response.AppendHeader("Content-Disposition","attachment;filename=FileFlow.xls");
      Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8");

      //Response.ContentType指定文件類型 可以為application/ms-excel application/ms-word application/ms-txt application/ms-html 或其他瀏覽器可直接支持文檔

      Response.ContentType = "application/ms-excel";
      this.EnableViewState = false;

        二、定義一個輸入流

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

        三、將目標數據綁定到輸入流輸出

      this.RenderControl(oHtmlTextWriter);

      //this 表示輸出本頁,你也可以綁定datagrid,或其他支持obj.RenderControl()屬性的控件

      Response.Write(oStringWriter.ToString());
      Response.End();

        總結:本例程在Microsoft Visual Studio .NET 2003平臺下測試通過,適用于C#和VB,當采用VB的時候將 this 關鍵字改成 me 。




      日韩精品一区二区三区高清