在VB.NET中,将数据库里的数据导出到Excel中


介绍
下面通过一步一步的介绍,如何通过VB.NET来读取数据,并且将数据导入到Excel中

第一步:
打开VS开发工具,并且添加引用
然后选择
  • Microsoft Excel 12.0 object library and
  • Microsoft Excel 14.0 object library
 


 



第二步:
创建一个Excle在你的电脑中
 


第三步:
在VS中写入如下代码:
  1. Imports System.Data
  2. Imports System.Data.SqlClient
  3. Imports Excel = Microsoft.Office.Interop.Excel

  4. Public Class excel
  5. ‘添加按钮
  6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
  7.         Handles Button1.Click
  8.         Try
  9.             '创建连接
  10.             Dim cnn As DataAccess = New DataAccess(CONNECTION_STRING)
  11.             
  12.             Dim i, j As Integer
  13.             '创建Excel对象
  14.             Dim xlApp As Microsoft.Office.Interop.Excel.Application
  15.             Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
  16.             Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
  17.             Dim misValue As Object = System.Reflection.Missing.Value
  18.             xlApp = New Microsoft.Office.Interop.Excel.ApplicationClass
  19.             xlWorkBook = xlApp.Workbooks.Add(misValue)
  20.             ' 打开某一个表单
  21.             xlWorkSheet = xlWorkBook.Sheets("sheet1")
  22.             ' sql查询
  23.             '  xlWorkBook.Sheets.Select("A1:A2")

  24.             Dim sql As String = "SELECT * FROM EMP"
  25.             ' SqlAdapter
  26.             Dim dscmd As New SqlDataAdapter(sql, cnn.ConnectionString)
  27.             ' 定义数据集
  28.             Dim ds As New DataSet
  29.             dscmd.Fill(ds)
  30.            ‘添加字段信息到Excel表的第一行
  31.             xlWorkSheet.Cells(1, 1).Value = "First Name"
  32.             xlWorkSheet.Cells(1, 2).Value = "Last Name"
  33.             xlWorkSheet.Cells(1, 3).Value = "Full Name"
  34.             xlWorkSheet.Cells(1, 4).Value = "Salary"
  35.             ' 将数据导入到excel
  36.               For i = 0 To ds.Tables(0).Rows.Count - 1
  37.                 'Column
  38.                 For j = 0 To ds.Tables(0).Columns.Count - 1
  39.                     ' this i change to header line cells >>>
  40.                     xlWorkSheet.Cells(i + 3, j + 1) = _
  41.                     ds.Tables(0).Rows(i).Item(j)
  42.                 Next
  43.             Next
  44.             'HardCode in Excel sheet
  45.             ' this i change to footer line cells  >>>
  46.            xlWorkSheet.Cells(i + 3, 7) = "Total"
  47.             xlWorkSheet.Cells.Item(i + 3, 8) = "=SUM(H2:H18)"
  48.             ' 保存到Excel
  49.             xlWorkSheet.SaveAs("D:\vbexcel.xlsx")
  50.             xlWorkBook.Close()
  51.             xlApp.Quit()
  52.             releaseObject(xlApp)
  53.             releaseObject(xlWorkBook)
  54.             releaseObject(xlWorkSheet)
  55.             '弹出对话框显示保存后的路径
  56.             MsgBox("You can find the file D:\vbexcel.xlsx")
  57.         Catch ex As Exception

  58.         End Try

  59.     End Sub
  60.     ' Function of Realease Object in Excel Sheet
  61.     Private Sub releaseObject(ByVal obj As Object)
  62.         Try
  63.             System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
  64.             obj = Nothing
  65.         Catch ex As Exception
  66.             obj = Nothing
  67.         Finally
  68.             GC.Collect()
  69.         End Try
  70.     End Sub
  71. End Class
复制代码

第四步:
看到如下导出结果
 



  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值