這里有段VB6.0的,你可以參考。
為科爾沁左翼等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及科爾沁左翼網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、科爾沁左翼網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
注意添加引用。
Private Sub Command6_Click()
Dim i, j As Integer
Dim xlApplication As Excel.Application, xlWorkbook As Excel.Workbook, xlSheet
Dim xlApp As Excel.Application
On Error Resume Next
Set xlApplication = GetObject(, "Excel.Application")
Set xlApp = CreateObject("Excel.Application")
If MsgBox("確認(rèn)將文件信息導(dǎo)出到EXCEL中??", vbExclamation + vbYesNo, "警告") = vbYes Then
If Err.Number 0 Then Set xlApplication = CreateObject("Excel.Application")
Set xlWorkbook = xlApplication.Workbooks.Add
Set xlSheet = xlWorkbook.ActiveSheet
xlSheet.Cells(1, 2) = lblcl.Caption
xlSheet.Range("A1:E1").MergeCells = True
xlSheet.Range("A1:E1").HorizontalAlignment = xlCenter
xlSheet.Cells(2, 2).ColumnWidth = 18
For i = 1 To DataGrid1.Columns.Count
xlSheet.Cells(2, 1) = "編號(hào)"
xlSheet.Cells(2, i + 1) = DataGrid1.Columns(i).Caption
For j = 0 To DataGrid1.VisibleRows - 1
xlSheet.Cells(j + 3, 1) = j + 1
xlSheet.Cells(j + 3, i + 1) = DataGrid1.Columns(i).CellText(DataGrid1.RowBookmark(j))
Next j
Next i
xlApplication.Visible = True
Set xlSheet = Nothing
Set xlWorkbook = Nothing
Set xlApplication = Nothing
'xlApp.Range("A2:L2").Columns.Interior.ColorIndex = 40
'xlApp.Range("A2:L2").Borders.LineStyle = xlContinuous
'xlApp.Visible = True
'xlApp.Range(xlSheet.Cells(2 + PartsRs.RecordCount + 1, 1), xlSheet.Cells(2 + PartsRs.RecordCount + 1, 8)).Columns.Interior.ColorIndex = 40
'xlApp.Range(xlSheet.Cells(2 + PartsRs.RecordCount + 1, 1), xlSheet.Cells(2 + PartsRs.RecordCount + 1, 8)).Borders.LineStyle = xlContinuous
Else
MsgBox "無信息可供您導(dǎo)出,請(qǐng)確認(rèn)!", vbExclamation + vbOKOnly, "警告"
End If
End Sub
不好意思 現(xiàn)在才看到;Private Sub 導(dǎo)出EXCEL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim xlApp, xlBook, xlSheet As Object
xlapp = CreateObject("Excel.Application")
xlbook = xlapp.Workbooks.Add
xlsheet = xlbook.Worksheets(1)
'Dim xlapp As New Excel.Application
'Dim xlbook As Excel.Workbook
'Dim xlsheet As Excel.Worksheet
Dim rowindex, colindex As Integer rowindex = 1 '行
colindex = 0 '列
'xlbook = xlapp.Workbooks.Open("c:\EXCEL.xls") '打開EXCEL文件
xlsheet = xlapp.Worksheets("sheet1") '打開sheet1那頁 Dim a As New DataSet
Dim table As New System.Data.DataTable table = DataSet11.Tables("INVMATLISTA") '填充表 Dim row As DataRow '定義row為表格的行
Dim col As DataColumn '定義col為表格的列 '把表格的每一列寫到EXCEL去
For Each col In table.Columns
colindex = colindex + 1
xlapp.Cells(1, colindex) = col.ColumnName Next
'把表格的每一行寫到EXCEL去
For Each row In table.Rows
rowindex = rowindex + 1
colindex = 0
For Each col In table.Columns
colindex = colindex + 1
xlapp.Cells(rowindex, colindex) = row(col.ColumnName)
Next
Next
xlapp.Visible = True
End Sub 前提要先引用一個(gè)Microsoft.Office.Interop.Excel.dll然后在最上面先輸入Imports Microsoft.Office.Interop; 就可以了
介紹
下面通過一步一步的介紹,如何通過VB.NET來讀取數(shù)據(jù),并且將數(shù)據(jù)導(dǎo)入到Excel中。
第一步:
打開VS開發(fā)工具,并且添加引用。
然后選擇。
Microsoft Excel 12.0 object library and。
Microsoft Excel 14.0 object library。
第二步:
創(chuàng)建一個(gè)Excle在你的電腦中。
第三步:
在VS中寫入如下代碼:
Imports System.Data
Imports System.Data.SqlClient
Imports Excel = Microsoft.Office.Interop.Excel。
Public Class excel
‘添加按鈕
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
? Handles Button1.Click
Try
?? ?'創(chuàng)建連接
?? ?Dim cnn As DataAccess = New DataAccess(CONNECTION_STRING)
?? ?
?? ?Dim i, j As Integer
?? ?'創(chuàng)建Excel對(duì)象
?? ?Dim xlApp As Microsoft.Office.Interop.Excel.Application
?? ?Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
?? ?Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
?? ?Dim misValue As Object = System.Reflection.Missing.Value
?? ?xlApp = New Microsoft.Office.Interop.Excel.ApplicationClass
?? ?xlWorkBook = xlApp.Workbooks.Add(misValue)
?? ?' 打開某一個(gè)表單
?? ?xlWorkSheet = xlWorkBook.Sheets("sheet1")
?? ?' sql查詢
?? ?'??xlWorkBook.Sheets.Select("A1:A2")
?? ?Dim sql As String = "SELECT * FROM EMP"
?? ?' SqlAdapter
?? ?Dim dscmd As New SqlDataAdapter(sql, cnn.ConnectionString)
?? ?' 定義數(shù)據(jù)集
?? ?Dim ds As New DataSet
?? ?dscmd.Fill(ds)
?? ? ‘添加字段信息到Excel表的第一行
?? ?xlWorkSheet.Cells(1, 1).Value = "First Name"
?? ?xlWorkSheet.Cells(1, 2).Value = "Last Name"
?? ?xlWorkSheet.Cells(1, 3).Value = "Full Name"
?? ?xlWorkSheet.Cells(1, 4).Value = "Salary"
?? ?' 將數(shù)據(jù)導(dǎo)入到excel
?? ???For i = 0 To ds.Tables(0).Rows.Count - 1
?? ?? ? 'Column
?? ?? ? For j = 0 To ds.Tables(0).Columns.Count - 1
?? ?? ?? ???' this i change to header line cells
?? ?? ?? ???xlWorkSheet.Cells(i + 3, j + 1) = _
?? ?? ?? ???ds.Tables(0).Rows(i).Item(j)
?? ?? ? Next
?? ?Next
?? ?'HardCode in Excel sheet
?? ?' this i change to footer line cells??
???xlWorkSheet.Cells(i + 3, 7) = "Total"
?? ?xlWorkSheet.Cells.Item(i + 3, 8) = "=SUM(H2:H18)"
?? ?' 保存到Excel
?? ?xlWorkSheet.SaveAs("D:\vbexcel.xlsx")
?? ?xlWorkBook.Close()
?? ?xlApp.Quit()
?? ?releaseObject(xlApp)
?? ?releaseObject(xlWorkBook)
?? ?releaseObject(xlWorkSheet)
?? ?'彈出對(duì)話框顯示保存后的路徑
?? ?MsgBox("You can find the file D:\vbexcel.xlsx")
Catch ex As Exception
End Try
End Sub
' Function of Realease Object in Excel Sheet
Private Sub releaseObject(ByVal obj As Object)
Try
?? ?System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
?? ?obj = Nothing
Catch ex As Exception
?? ?obj = Nothing
Finally
?? ?GC.Collect()
End Try
End Sub
End Class
復(fù)制代碼。
第四步:
看到如下導(dǎo)出結(jié)果。