加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 07:57:08 所属栏目:百科 来源:网络整理
导读:介绍 下面通过一步一步的介绍,如何通过VB.NET来读取数据,并且将数据导入到Excel中 第一步: 打开VS开发工具,并且添加引用 然后选择 Microsoft Excel 12.0 object library and Microsoft Excel 14.0 object library 650) this.width=650;" id="aimg_109" s

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

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

  • Microsoft Excel 12.0 object library and

  • Microsoft Excel 14.0 object library

123803lxdezep72lyp2s5l.jpg




123803rrjnbdr7z7njdjdw.jpg





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

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.jpg


相关链接


  • ListView通过virtual list虚拟列表快速显示某个文件夹的所有文件

  • Delphi7实现的一个简单的图片识别代码

  • C#中List<String>快速排序算法

  • C#以及F#的字符串高速排序算法代码

  • Delphi7通过TIdHTTPServer实现一个简单的web服务

  • Delphi7开发的wav语音播放的dll

  • Fastreport 3.1.5如何实现在报表上进行数据集遍历、修改?

  • Delphi7实现用插件的开发方式来减少重复代码

  • Delphi7调用三汇语音卡进行电话录音

  • Delphi7实现程序中文字的简体繁体语言的切换

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读