DataGrid中的資料匯入到Word和Excel

來源:互聯網
上載者:User
datagrid|excel|word|資料 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 Form設計器產生的程式碼 "

'該調用是 Web Form設計器所必需的。
<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 Form設計器所必需的。
'不要刪除或移動它。
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法調用是 Web Form設計器所必需的
'不要使用代碼編輯器修改它。
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





相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。