vb中如何将数据导出到excel
介绍
下面通过一步一步的介绍,如何通过VB.NET来读取数据,并且将数据导入到Excel中。
第一步:
打开VS开发工具,并且添加引用。
然后选择。
Microsoft Excel 12.0 object library and。
Microsoft Excel 14.0 object library。
第二步:
创建一个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
'创建连接
Dim cnn As DataAccess = New DataAccess(CONNECTION_STRING)
Dim i, j As Integer
'创建Excel对象
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)
' 打开某一个表单
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)
' 定义数据集
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"
' 将数据导入到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)
'弹出对话框显示保存后的路径
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
复制代码。
第四步:
看到如下导出结果。
2013-08-16
Set Rs4 = New ADODB.Recordset
Dim sSQL As String
Dim j As Integer
DT1 = Format(Trim(D1.Value), "yyyy-mm-dd")
DT2 = Format(Trim(D2.Value), "yyyy-mm-dd")
'sql语句
sSQL = " Select * from XS_TAB WHERE RQ >='" & DT1 & "' AND RQ<='" & DT2 & "'" ' Order by id "
Rs4.Open sSQL, mConn, adOpenKeyset, adLockOptimistic
j = Rs4.recordcount
MsgBox "表内记录数:" + CStr(Rs4.recordcount) + "条.", vbOKOnly, "信息提示"
Set DataGrid1.DataSource = Rs4 '显示在DataGrid1中
DataGrid1.Refresh
End Sub
'导出到d:\text2.xls中
Private Sub Command2_Click()
Dim i As Integer
Dim j As Integer
Dim xlApp As New Excel.Application
Dim xlBook As New Excel.Workbook
Dim xlSheet As New Excel.Worksheet
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlBook = xlApp.Workbooks.Add
On Error Resume Next
Set xlBook = xlApp.Workbooks.Open("d:\text2.xls")
Set xlSheet = xlBook.Worksheets(1)
For j = 0 To DataGrid1.Columns.Count - 1
xlSheet.Cells(1, j + 1) = DataGrid1.Columns.Item(j).Caption
Next j
xlSheet.Cells(6, 1) = "i"
adodc1.Recordset.MoveFirst
For i = 0 To adodc1.Recordset.recordcount - 1
'For j = 0 To DataGrid1.Columns.Count - 1 '这一行跟下一行效果一样
For j = 0 To adodc1.Recordset.fields.Count - 1
xlSheet.Cells(i + 1, j + 1) = adodc1.Recordset(j)
Next j
adodc1.Recordset.movenext
Next i
End Sub
2013-08-16
Dim xlBook As Excel.WorkBook
Dim xlSheet As Excel.Worksheet
Set xlApp = CreateObject("Excel.Application") '创建EXCEL对象
Set xlBook = xlApp.Workbooks.Open("文件名") '打开已经存在的EXCEL工件簿文件
xlApp.Visible = True '设置EXCEL对象可见(或不可见)
Set xlSheet = xlBook.Worksheets("表名") '设置活动工作表
xlSheet.Cells(row, col) =值 '给单元格(row,col)赋值
xlBook.Close (True) '关闭工作簿�0�2
xlApp.Quit '结束EXCEL对象
Set xlApp = Nothing '释放xlApp对象