html5中文学习网

您的位置: 首页 > 网络编程 > ASP.NET » 正文

ASP .NET WebForm 的 DataGrid 中的料 出至 Microsoft Excel_.NET教程_编程技术

[ ] 已经帮助:人解决问题
datagrid|excel|web
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 案,按一下 []。

EkxHTML5中文学习网 - HTML5先行者学习网
EkxHTML5中文学习网 - HTML5先行者学习网
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助