The data in the DataGrid is imported into Word and Excel

Source: Internet
Author: User
Tags tostring
datagrid|excel|word| Data 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 the code generated by the Web Forms Designer

' This call is required by the Web Forms Designer.
<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

' NOTE: The following placeholder declarations are required by the Web Forms Designer.
' Do not delete or move it.
Private Designerplaceholderdeclaration as System.Object

Private Sub Page_Init (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Mybase.init
' CodeGen: This method call is required by the Web Forms Designer
' Do not modify it using the Code Editor.
InitializeComponent ()
End Sub

#End Region

Private Sub Page_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load
' The user code to place the initialization page here
CreateDataSet ()
End Sub


Private Sub Exportdatagrid (ByVal FileType As String, ByVal FileName as String) ' derived from the 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 () ' Create 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 ' CREATE table
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")) ' Create field and set type
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", "index list. doc") ' leads to Word
End Sub

Private Sub Btn_import_excel_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles BTN_IMPORT_EXCEL.C Lick
Call Exportdatagrid ("Application/ms-excel", "index list. xls") ' leads to excel
End Sub
End Class





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.