Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.IO
Imports System.Data.SqlClient
Imports System.Text
Public Class WebForm1
Inherits System.Web.UI.Page
#Region " Web 窗体设计器生成的代码 "
'该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
Protected WithEvents Btn_Import_Excel As System.Web.UI.WebControls.Button
Protected WithEvents BtnImportWord As System.Web.UI.WebControls.Button
'注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
CreateDataSet()
End Sub
Private Sub ExportDataGrid(ByVal FileType As String, ByVal FileName As String) '从DataGrid导出
Response.Charset = "GB2312"
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312")
Response.AppendHeader("Content-Disposition", "attachment;filename=" & HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8))
Response.ContentType = FileType
Me.EnableViewState = False
Dim tw As System.IO.StringWriter
tw = New System.IO.StringWriter
Dim hw As System.Web.UI.HtmlTextWriter
hw = New HtmlTextWriter(tw)
DataGrid1.RenderControl(hw)
Response.Write(tw.ToString())
Response.End()
End Sub
Private Sub CreateDataSet() '创建DataSet
Dim myDataSet As DataSet
myDataSet = New DataSet("aNewDataSet")
Dim table1 As DataTable
table1 = MakeTable("ID", "Name")
myDataSet.Tables.Add(table1)
DataGrid1.DataSource = myDataSet
DataGrid1.DataBind()
End Sub
Private Function MakeTable(ByVal c1Name As String, ByVal c2Name As String) As DataTable '创建表
Dim i As Integer
Dim myTable As New DataTable
Dim myColumn As DataColumn
' Add two DataColumns
myColumn = New DataColumn(c1Name, System.Type.GetType("System.Int32")) '创建字段和设置类型
myTable.Columns.Add(myColumn)
myColumn = New DataColumn(c2Name, System.Type.GetType("System.String"))
myTable.Columns.Add(myColumn)
Dim Dr As DataRow
For i = 1 To 10
Dr = myTable.NewRow()
Dr(0) = i
Dr(1) = "Name" + i.ToString()
myTable.Rows.Add(Dr)
Next i
MakeTable = myTable
End Function
Private Sub BtnImportWord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnImportWord.Click
Call ExportDataGrid("application/ms-word", "指数列表.doc") '导到Word
End Sub
Private Sub Btn_Import_Excel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Import_Excel.Click
Call ExportDataGrid("application/ms-excel", "指数列表.xls") '导到Excel
End Sub
End Class