vb.net导出到excel

前端之家收集整理的这篇文章主要介绍了vb.net导出到excel前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
网了查了一堆资料,自己也写出了到出到excel的方法.记下来.
  1. Try
  2.  
  3. Dim errMsg As String = ""
  4. Dim CheckedCount As Integer = Me.GpDetailListView.CheckedItems.Count
  5. If CheckedCount < 1 Then
  6. MsgBox("请选择要导出的项!",MsgBoxStyle.Information,"提示")
  7. Return
  8. End If
  9. Console.WriteLine("选择数量:" & CheckedCount.ToString)
  10.  
  11. Me.ExportButton.Text = "正在导出..."
  12. Me.ExportButton.Enabled = False
  13. Me.PictureBox1.Visible = True
  14.  
  15. If Not My.Computer.FileSystem.DirectoryExists("ExportedExcel") Then
  16. My.Computer.FileSystem.CreateDirectory("ExportedExcel")
  17. End If
  18.  
  19. Dim strNow As String = Format(Now(),"yyyy_MM_dd_HHmmss").ToString
  20. Dim strFileName As String = My.Computer.FileSystem.CurrentDirectory & "\ExportedExcel\ExportedGP" & strNow & ".xlsx"
  21. 'System.IO.File.Create(strFileName)
  22.  
  23. Dim excelApp = New Excel.Application ' CreateObject("Excel.Application")
  24. excelApp.Visible = False
  25. excelApp.DisplayAlerts = False
  26. Dim excelWorkbook As Excel.Workbook
  27. Dim misValue As Object = System.Reflection.Missing.Value
  28. excelWorkbook = excelApp.Workbooks.Add(misValue)
  29. Dim excelSheet As Excel.Worksheet = excelWorkbook.Worksheets(1)
  30. excelSheet.Name = "工票信息"
  31. Dim excelRange As Excel.Range
  32. excelSheet.Cells(1,1) = "员工"
  33. excelSheet.Cells(1,2) = "产品类别"
  34. excelSheet.Cells(1,3) = "工位"
  35. excelSheet.Cells(1,4) = "数量"
  36. excelRange = excelSheet.Cells(1,1)
  37. excelRange.Font.FontStyle = FontStyle.Bold
  38. excelRange.Font.Size = 12
  39. excelRange = excelSheet.Cells(1,2)
  40. excelRange.Font.FontStyle = FontStyle.Bold
  41. excelRange.Font.Size = 12
  42. excelRange = excelSheet.Cells(1,3)
  43. excelRange.Font.FontStyle = FontStyle.Bold
  44. excelRange.Font.Size = 12
  45. excelRange = excelSheet.Cells(1,4)
  46. excelRange.Font.FontStyle = FontStyle.Bold
  47. excelRange.Font.Size = 12
  48. excelSheet.
  49.  
  50. For i As Integer = 0 To CheckedCount - 1
  51. Dim CheckItemId As String = Me.GpDetailListView.CheckedItems(i).SubItems.Item(4).Text
  52. For j As Integer = 0 To Me.dtGpInfo.Rows.Count - 1
  53. Dim dtRowId As String = Me.dtGpInfo.Rows(j).Item("id").ToString
  54. If CheckItemId = dtRowId Then
  55. Dim cnName As String = Me.dtGpInfo.Rows(j).Item("cnName").ToString
  56. Dim prdType As String = Me.dtGpInfo.Rows(j).Item("prdType").ToString
  57. Dim posName As String = Me.dtGpInfo.Rows(j).Item("posName").ToString
  58. Dim count As String = Me.dtGpInfo.Rows(j).Item("count").ToString
  59.  
  60. excelSheet.Cells(j + 2,1) = cnName
  61. excelSheet.Cells(j + 2,2) = prdType
  62. excelSheet.Cells(j + 2,3) = posName
  63. excelSheet.Cells(j + 2,4) = count
  64. End If
  65. Next
  66. Next
  67. excelWorkbook.SaveAs(strFileName)
  68. excelApp.Quit()
  69. Me.ExportButton.Text = "导出到Excel"
  70. Me.ExportButton.Enabled = True
  71. Me.PictureBox1.Visible = False
  72. MsgBox("导出成功:" & strFileName,MsgBoxStyle.MsgBoxSetForeground,"导出成功")
  73. Catch ex As Exception
  74. MsgBox(ex.Message,MsgBoxStyle.Critical,"导出异常")
  75. Me.ExportButton.Text = "导出到Excel"
  76. Me.ExportButton.Enabled = True
  77. Me.PictureBox1.Visible = False
  78. Return
  79. End Try

猜你在找的VB相关文章