日期:2009-09-07  浏览次数:20517 次

將 ASP .NET WebForm 的 DataGrid 中的資料 匯出至 Microsoft Excel
本文將逐步帶領您產生 ASP.NET WebForm 上的 DataGrid Web 伺服器控制項,然後將 DataGrid 內容匯出至 Microsoft Excel。


技術
本文將說明兩種匯出 DataGrid 資料的技術:
使用 Excel MIME 類型 (或內容類型)

您可以使用伺服器端的程式碼將 DataGrid 連結至資料,然後在用戶端電腦的 Excel 中開啟資料。如果要執行這項操作,請將 ContentType 設為 application/vnd.ms-Excel。用戶端收到新資料流後,資料會顯示在 Excel 中,如同在 Web 瀏覽器新網頁中開啟內容。
使用 Excel 自動化

您可以使用用戶端的程式碼,從 DataGrid 擷取成 HTML,然後「自動執行 Excel」以新的活頁簿顯示 HTML。「Excel 自動化」永遠會在 Excel 應用程式視窗的瀏覽器外側,顯示資料。「自動化」的一項優點是如果您想在匯出資料後修改活頁簿,可以由程式來控制 Excel。不過,由於 Excel 的指令碼是標示為不安全的,因此用戶端必須在 Web 瀏覽器上套用允許「自動化」的安全性設定。
逐步說明
啟動 Visual Studio .NET。在 [檔案] 功能表中,指向 [新增],再按一下 [專案]。
按一下 [專案類型] 窗格中的 [Visual Basic 專案]。按一下 [範本] 下的 [ASP.NET Web 應用程式]。將應用程式命名為 ExcelExport,然後按一下 [確定]。

「設計」檢視中會出現 WebForm1。
在「方案總管」的 [WebForm1.ASPx] 上按右滑鼠鍵,然後按一下 [重新命名]。將檔案名稱變更為 Bottom.ASPx。
在 [檢視] 功能表上按一下 [HTML 原始檔],將下列 DataGrid 新增至 <form> 和 </form> 標籤之間:
<ASP:datagrid id="DataGrid1" runat="server" GridLines="Vertical" CellPadding="3" BackColor="White"
BorderColor="#999999" BorderWidth="1px" BorderStyle="None" Width="100%" Height="100%" Font-Size="X-Small"
Font-Names="Verdana">
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#008A8C"></SelectedItemStyle>
<AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle>
<ItemStyle BorderWidth="2px" ForeColor="Black" BorderStyle="Solid" BorderColor="Black" BackColor="#EEEEEE"></ItemStyle>
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" BorderWidth="2px" ForeColor="White" BorderStyle="Solid"
BorderColor="Black" BackColor="#000084"></HeaderStyle>
<FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle>
<PagerStyle HorizontalAlign="Center" ForeColor="Black" BackColor="#999999" Mode="NumericPages"></PagerStyle>
</ASP:datagrid>

在 [檢視] 功能表上按一下 [設計],便可返回設計檢視。

DataGrid 出現在 WebForm 上。
在 [檢視] 功能表上按一下 [程式碼],如此便可顯示 Web Form 後面的程式碼。將下列程式碼新增至 Page_Load 事件處理常式:

注意您必須將 User ID <username> 和 password=<strong password> 變更為正確的值,才能執行這個程式碼。使用者帳戶必須有正確的權限,才能在資料庫上執行這個作業。
' Create a connection and open it.
Dim objConn As New System.Data.SqlClient.SqlConnection("User ID=<username>;Password=<strong password>;Initial Catalog=Northwind;Data Source=SQLServer;")
objConn.Open()

Dim strSQL As String
Dim objDataset As New DataSet()
Dim objAdapter As New System.Data.SqlClient.SqlDataAdapter()

' Get all the customers from the USA.
strSQL = "Select * from customers where country='USA'"
objAdapter.SelectCommand = New System.Data.SqlClient.SqlCommand(strSQL, objConn)
' Fill the dataset.
objAdapter.Fill(objDataset)
' Create a new view.
Dim oView As New DataView(objDataset.Tables(0))
' Set up the data grid and bind the data.
DataGrid1.DataSource = oView
DataGrid1.DataBind()

' Verify if the page is to be displayed in Excel.
If Request.QueryString("bExcel") = "1" Then
' Set the content type to Excel.
Response.ContentType = "application/vnd.ms-Excel"
' Remove the charset from the Content-Type header.
Response.Charset = ""
' Turn off the view state.
Me.EnableViewState = False <