别人的例子: Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '在此处放置初始化页的用户代码 BindGrid() End Sub Private Sub BindGrid() conn = New SqlConnection("Initial Catalog=Northwind;Data Source=127.0.0.1;uid=sa;pwd=sa") sql = New SqlCommand("select * from products", conn) conn.Open() Dim reader As SqlDataReader reader = sql.ExecuteReader() DataGrid1.DataSource = reader DataGrid1.DataBind() reader.Close() conn.Close() End Sub Private Sub WriteDataGrid2Excel() Dim xlsheet As New SpreadsheetClass() conn.Open() Dim reader As SqlDataReader reader = sql.ExecuteReader() Dim numbercols As Int16 numbercols = reader.FieldCount Dim row As Int16 = 1 Dim i As Int16 While reader.Read() For i = 0 To numbercols - 1 xlsheet.ActiveSheet.Cells(row, i + 1) = reader.GetValue(i).ToString() Next row += 1 End While reader.Close() conn.Close() 'xlsheet.ActiveSheet.Export() Try xlsheet.ActiveSheet.Export(Server.MapPath(".") + "\wx.xls", OWC.SheetExportActionEnum.ssExportActionNone) Catch e As Exception Dim wx As String wx = e.Message End Try Response.Redirect("wx.xls") End Sub Private Sub export2excel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles export2excel.Click Dim xlApp, xlBook, XlSheet xlApp = CreateObject("Excel.Application") xlBook = xlApp.Workbooks.add XlSheet = xlBook.Worksheets(1) xlApp.Caption = "test表" XlSheet.name = "test表" XlSheet.Rows("1:1").RowHeight = 36.6 XlSheet.Range("A1").FormulaR1C1 = "test" XlSheet.Range("A1").Font.Bold = True XlSheet.Range("A1").Font.ColorIndex = 3 XlSheet.Range("A1").Font.Size = 16 xlApp.Visible = True 'xlApp = Nothing 'xlBook = Nothing 'XlSheet = Nothing End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click WriteDataGrid2Excel() End Sub End Class
'在此处放置初始化页的用户代码
BindGrid()
End Sub
Private Sub BindGrid()
conn = New SqlConnection("Initial Catalog=Northwind;Data Source=127.0.0.1;uid=sa;pwd=sa")
sql = New SqlCommand("select * from products", conn)
conn.Open()
Dim reader As SqlDataReader
reader = sql.ExecuteReader()
DataGrid1.DataSource = reader
DataGrid1.DataBind()
reader.Close()
conn.Close()
End Sub
Private Sub WriteDataGrid2Excel()
Dim xlsheet As New SpreadsheetClass()
conn.Open()
Dim reader As SqlDataReader
reader = sql.ExecuteReader()
Dim numbercols As Int16
numbercols = reader.FieldCount
Dim row As Int16 = 1
Dim i As Int16
While reader.Read()
For i = 0 To numbercols - 1
xlsheet.ActiveSheet.Cells(row, i + 1) = reader.GetValue(i).ToString()
Next
row += 1
End While
reader.Close()
conn.Close()
'xlsheet.ActiveSheet.Export()
Try
xlsheet.ActiveSheet.Export(Server.MapPath(".") + "\wx.xls", OWC.SheetExportActionEnum.ssExportActionNone)
Catch e As Exception
Dim wx As String
wx = e.Message
End Try
Response.Redirect("wx.xls") End Sub Private Sub export2excel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles export2excel.Click
Dim xlApp, xlBook, XlSheet
xlApp = CreateObject("Excel.Application")
xlBook = xlApp.Workbooks.add
XlSheet = xlBook.Worksheets(1)
xlApp.Caption = "test表"
XlSheet.name = "test表" XlSheet.Rows("1:1").RowHeight = 36.6
XlSheet.Range("A1").FormulaR1C1 = "test"
XlSheet.Range("A1").Font.Bold = True
XlSheet.Range("A1").Font.ColorIndex = 3
XlSheet.Range("A1").Font.Size = 16 xlApp.Visible = True 'xlApp = Nothing
'xlBook = Nothing
'XlSheet = Nothing
End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
WriteDataGrid2Excel()
End Sub
End Class