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

前端之家收集整理的这篇文章主要介绍了在VB.NET中,将数据库里的数据导出到Excel中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

第一步:
打开VS开发工具,并且添加引用
然后选择

  • Microsoft Excel 12.0 object library and

  • Microsoft Excel 14.0 object library

123803lxdezep72lyp2s5l.jpg403/16/123803lxdezep72lyp2s5l.jpg">




123803rrjnbdr7z7njdjdw.jpg403/16/123803rrjnbdr7z7njdjdw.jpg">





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

123906y2nheccehfydrcwy.jpg403/16/123906y2nheccehfydrcwy.jpg">


第三步:
在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. Dim i,j As Integer

  12. '创建Excel对象

  13. Dim xlApp As Microsoft.Office.Interop.Excel.Application

  14. Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook

  15. Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet

  16. Dim misValue As Object = System.Reflection.Missing.Value

  17. xlApp = New Microsoft.Office.Interop.Excel.ApplicationClass

  18. xlWorkBook = xlApp.Workbooks.Add(misValue)

  19. ' 打开某一个表单

  20. xlWorkSheet = xlWorkBook.Sheets("sheet1")

  21. ' SQL查询

  22. ' xlWorkBook.Sheets.Select("A1:A2")


  23. Dim sql As String = "SELECT * FROM EMP"

  24. ' sqlAdapter

  25. Dim dscmd As New sqlDataAdapter(sql,cnn.ConnectionString)

  26. ' 定义数据集

  27. Dim ds As New DataSet

  28. dscmd.Fill(ds)

  29. 添加字段信息到Excel表的第一行

  30. xlWorkSheet.Cells(1,1).Value = "First Name"

  31. xlWorkSheet.Cells(1,2).Value = "Last Name"

  32. xlWorkSheet.Cells(1,3).Value = "Full Name"

  33. xlWorkSheet.Cells(1,4).Value = "Salary"

  34. ' 将数据导入到excel

  35. For i = 0 To ds.Tables(0).Rows.Count - 1

  36. 'Column

  37. For j = 0 To ds.Tables(0).Columns.Count - 1

  38. ' this i change to header line cells >>>

  39. xlWorkSheet.Cells(i + 3,j + 1) = _

  40. ds.Tables(0).Rows(i).Item(j)

  41. Next

  42. Next

  43. 'HardCode in Excel sheet

  44. ' this i change to footer line cells >>>

  45. xlWorkSheet.Cells(i + 3,7) = "Total"

  46. xlWorkSheet.Cells.Item(i + 3,8) = "=SUM(H2:H18)"

  47. ' 保存到Excel

  48. xlWorkSheet.SaveAs("D:\vbexcel.xlsx")

  49. xlWorkBook.Close()

  50. xlApp.Quit()

  51. releaSEObject(xlApp)

  52. releaSEObject(xlWorkBook)

  53. releaSEObject(xlWorkSheet)

  54. '弹出对话框显示保存后的路径

  55. MsgBox("You can find the file D:\vbexcel.xlsx")

  56. Catch ex As Exception


  57. End Try


  58. End Sub

  59. ' Function of Realease Object in Excel Sheet

  60. Private Sub releaSEObject(ByVal obj As Object)

  61. Try

  62. System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)

  63. obj = Nothing

  64. Catch ex As Exception

  65. obj = Nothing

  66. Finally

  67. GC.Collect()

  68. End Try

  69. End Sub

  70. End Class

复制代码

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

124258xfushd95qh4fr5rr.jpg403/16/124258xfushd95qh4fr5rr.jpg">


相关链接


猜你在找的VB相关文章