ASP.NET 匯出Excel表方法匯總(包含錯誤資訊調試方法)

來源:互聯網
上載者:User
文章目錄
  • 一、匯出Excel表方法
  • 二、錯誤資訊調試

ASP.NET在資料表查看的同時,大部分使用者希望能夠把資料表資訊能夠匯出為Excel並進行儲存。

本文主要介紹幾種匯出Excel表格的方法以及在調試過程中,錯誤資訊的調試方法。

一、匯出Excel表方法

在asp.net中匯出Execl有兩種方法,一種是將匯出的檔案存放在伺服器某個檔案夾下面,然後將檔案地址輸出在瀏覽器上;一種是將檔案直接將檔案輸出資料流寫給瀏覽器。在Response輸出時,t分隔的資料,匯出execl時,等價於分列,n等價於換行。

有位網友寫過:《C# 將資料匯出到Excel匯總》 http://blog.csdn.net/bat800/archive/2007/07/17/1694537.aspx

內容已經比較詳細了。本文對其進行一定的更正,大家可以直接拷貝代碼進行測試。

在此簡單的介紹一種方法,代碼如下:

public void DGToExcel(System.Web.UI.Control ctl)<br /> {<br /> HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");<br /> HttpContext.Current.Response.Charset = "UTF-8";<br /> HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;<br /> HttpContext.Current.Response.ContentType = "application/ms-excel";<br /> ctl.Page.EnableViewState = false;<br /> System.IO.StringWriter tw = new System.IO.StringWriter();<br /> System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);<br /> ctl.RenderControl(hw);<br /> HttpContext.Current.Response.Write(tw.ToString());<br /> HttpContext.Current.Response.End();<br /> }

你可以按照DGToExcel(this.GridView1);進行操作即可。

但是上面的方法雖然實現了匯出的功能,但同時把按鈕、分頁框等html中的所有輸出資訊導了進去。而我們一般要匯出的是資料,DataGrid控制項上的資料,而且如果你的DataGrid用了分頁,它匯出的是當前頁的資訊,也就是它匯出的是DataGrid中顯示的資訊。而不是你select語句的全部資訊。下面一個方法可以協助大家解決這個問題:

public void CreateExcel(DataSet ds, string FileName)<br /> {<br /> HttpResponse resp;<br /> resp = Page.Response;<br /> resp.ContentEncoding = System.Text.Encoding.GetEncoding("GBK");<br /> resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);<br /> string colHeaders = "", ls_item = "";</p><p> //定義表對象與行對象,同時用DataSet對其值進行初始化<br /> System.Data.DataTable dt = ds.Tables[0];<br /> DataRow[] myRow = dt.Select();//可以類似dt.Select("id>10")之形式達到資料篩選目的<br /> int i = 0;<br /> int cl = dt.Columns.Count;</p><p> //取得資料表各欄位標題,各標題之間以t分割,最後一個欄位標題後加斷行符號符<br /> for (i = 0; i < cl; i++)<br /> {<br /> if (i == (cl - 1))//最後一列,加/n<br /> {<br /> colHeaders += dt.Columns[i].Caption.ToString() + "/n";<br /> }<br /> else<br /> {<br /> colHeaders += dt.Columns[i].Caption.ToString() + "/t";<br /> }</p><p> }<br /> resp.Write(colHeaders);<br /> //向HTTP輸出資料流中寫入取得的資料資訊 </p><p> //逐行處理資料<br /> foreach (DataRow row in myRow)<br /> {<br /> //當前行資料寫入HTTP輸出資料流,並且置空ls_item以便下行資料<br /> for (i = 0; i < cl; i++)<br /> {<br /> if (i == (cl - 1))//最後一列,加/n<br /> {<br /> ls_item += row[i].ToString() + "/n";<br /> }<br /> else<br /> {<br /> ls_item += row[i].ToString() + "/t";<br /> }</p><p> }<br /> resp.Write(ls_item);<br /> ls_item = "";</p><p> }<br /> resp.End();<br /> }

你可以按照CreateExcel(ds, "ex.xls");進行操作。

二、錯誤資訊調試

提示【GridView”的控制項“GridView1”必須放在具有 runat=server 的表單標記內】

解決方案有:

(1) 確認你的GridView”的控制項在Form裡面,並且Form中runat="server"

(2) 在後台代碼重寫以下方法:

public override void VerifyRenderingInServerForm(Control control)<br /> {</p><p> }

提示【只能在執行 Render() 的過程中調用 RegisterForEventValidation】

解決方案有:

(1) 修改web.config(不推薦)<pages enableEventValidation ="false" ></pages>

(2) 修改頁面代碼:

<%@ Page Language="C#" EnableEventValidation = "false" AutoEventWireup="true"</p><p> CodeFile="ExportGridView.aspx.cs" Inherits="ExportGridView" %><br />  

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.