webform空间在html输出数据库,如何:在 ASP 上的数据网格中导出数据。 Microsoft Excel 的 NET WebForm...

启动 Visual Studio .NET。 在“文件”菜单上,指向“新建”,然后单击“项目”。

在 "项目类型" 窗格中,单击 "Visual Basic 项目"。 在 "模板" 下,单击 "ASP.NET Web 应用程序"。 将应用程序命名为 ExcelExport,然后单击 "确定"。

WebForm1 在设计视图中显示。

在 "解决方案资源管理器" 中,右键单击 "WebForm1",然后单击 "重命名"。 将文件的名称更改为 ".aspx"。

在 "视图" 菜单上,单击 "HTML 源文件",将以下数据网格添加到和 标记

BorderColor="#999999" BorderWidth="1px" BorderStyle="None" Width="100%" Height="100%" Font-Size="X-Small"

Font-Names="Verdana">

BorderColor="Black" BackColor="#000084">

在 "视图" 菜单上,单击 "设计" 以返回到 "设计视图"。

数据网格将显示在 WebForm 上。

在 "视图" 菜单上,单击 "代码" 以显示 Web 表单背后的代码。 将以下代码添加到 Page_Load 事件处理程序:

注释在运行此代码之前 "和" 密码 = 强密码 "> 更改为正确的值。 确保用户帐户具有对数据库执行此操作的正确权限。

' Create a connection and open it.

Dim objConn As New System.Data.SqlClient.SqlConnection("User ID=;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 数据库:

提供程序 = microsoft. 4.0;数据源 = C:\Program Files\Microsoft Office\Office10\Samples\Northwind.mdb

如果选择此方法,请将上述代码修改为使用 OleDbClient 命名空间,而不是 SqlClient 命名空间。

在 "项目" 菜单上,单击 "添加 HTML 页"。 将页面命名为 ".htm",然后单击 "打开"。

在设计视图中显示上 .htm。

在 "视图" 菜单上,单击 "HTML 源"。 将 HTML 源窗口的内容替换为以下代码:

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

Export to Excel Using:

Automation

MIME Type (In a New Window)

MIME Type (In the Frame)

在 "项目" 菜单上,单击 "添加 HTML 页"。 将页面框架命名为 .htm,然后单击 "打开"。

在设计视图中打开 Frameset。

在 "视图" 菜单上,单击 "HTML 源"。 将 HTML 源窗口的内容替换为以下代码:

在 "解决方案资源管理器" 中,右键单击 "Frameset",然后单击 "设为起始页"。

在"构建"菜单上,单击"构建解决方案"。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值