將 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
dim tw as new system.io.stringwriter()
dim hw as new system.web.ui.htmltextwriter(tw)
get the html for the control.
datagrid1.rendercontrol(hw)
write the html back to the browser.
response.write(tw.tostring())
end the response.
response.end()
end if
注意:請將程式碼中的 sqlserver 取代為您的 sql server 名稱。如果您無法存取 northwind 範例資料庫所在的 sql server,請將連線字串修改為使用 microsoft access 2002 範例 northwind 資料庫:
provider=microsoft.jet.oledb.4.0; data source=c:\program files\microsoft office\office10\samples\northwind.mdb
如果您選取這個方法,請將 aforementioned 程式碼修改為使用 oledbclient 命名空間 (而不使用 sqlclient 命名空間)。
在 [專案] 功能表上,按一下 [加入 html 網頁]。將網頁命名為 top.htm,然後按一下 [開啟]。
「設計」檢視中會出現 top.htm。
在 [檢視] 功能表上,按一下 [html 原始檔]。將 html 原始檔視窗的內容取代為下列程式碼:
<html>
<script language="vbscript">
sub button1_onclick
select case select1.selectedindex
case 0 use automation.
dim shtml
shtml = window.parent.frames("bottom").document.forms(0).children("datagrid1").outerhtml
dim oxl, obook
set oxl = createobject("excel.application")
set obook = oxl.workbooks.add
obook.htmlproject.htmlprojectitems("sheet1").text = shtml
obook.htmlproject.refreshdocument
oxl.visible = true
oxl.usercontrol = true
case 1 use mime type (in a new window).
window.open("bottom.aspx?bexcel=1")
case 2 use mime type (in the frame).
window.parent.frames("bottom").navigate "bottom.aspx?bexcel=1"
end select
end sub
</script>
<body>
export to excel using:
<select id="select1" size="1" name="select1">
<option value="0" selected>automation</option>
<option value="1">mime type (in a new window)</option>
<option value="2">mime type (in the frame)</option>
</select>
<input id="button1" type="button" value="go!" name="button1">
</body>
</html>
在 [專案] 功能表上,按一下 [加入 html 網頁]。將網頁命名為 frameset.htm,然後按一下 [開啟]。
「設計」檢視中會開啟 frameset.htm。
在 [檢視] 功能表上,按一下 [html 原始檔]。將 html 原始檔視窗的內容取代為下列程式碼:
<html>
<frameset rows="10%,90%">
<frame noresize="0" scrolling="no" name="top" src="top.htm">
<frame noresize="0" scrolling="yes" name="bottom" src="bottom.aspx">
</frameset>
</html>
在「方案總管」的 [frameset.htm] 上按滑鼠右鍵,然後按一下 [設定為起始頁]。
在 [建置] 功能表上,按一下 [建置方案]。
試試看!
在 [偵錯] 功能表上按一下 [啟動但不偵錯],執行應用程式。
web 瀏覽器開啟框架組後,框架下方的 datagrid 顯示出 northwind 資料庫的資料。
在下拉清單中按一下 [automation],再按 [go]。
datagrid 內容會顯示在 microsoft excel 應用程式視窗的瀏覽器外側。
在下拉清單中按一下 [mime type (in a new window)],再按 [go]。
datagrid 內容會顯示在新 web 瀏覽器視窗的 excel 中。
注意:提示您開啟或儲存 excel 檔案時,請按一下 [開啟]。
在下拉清單中按一下 [mime type (in the frame)],再按 [go]。
datagrid 內容會顯示在 web 瀏覽器 excel 框架組的下框架中。
注意:提示您開啟或儲存 excel 檔案時,請按一下 [開啟]。
