Vb.net 将数据表导出到Excel(VB窗口应用程序)
这就是我尝试过的。它已成功导出,但没有数据。空excel。我想我需要添加一些代码来添加行和列,但我不知道如何添加。我是新来的Vb.net 将数据表导出到Excel(VB窗口应用程序),vb.net,winforms,Vb.net,Winforms,这就是我尝试过的。它已成功导出,但没有数据。空excel。我想我需要添加一些代码来添加行和列,但我不知道如何添加。我是新来的 Try 'Exporting to Excel. Dim folderPath As String = dtJelo.Rows(0)("jelly").ToString.Trim Dim filename As String = "JellyDetails" & current_yyyy & cu
Try
'Exporting to Excel.
Dim folderPath As String = dtJelo.Rows(0)("jelly").ToString.Trim
Dim filename As String = "JellyDetails" & current_yyyy & current_mon & ".xlsx"
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
'addrow
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
xlWorkSheet.SaveAs("C:\Users\spongebob\Desktop\" & filename & ".xlsx")
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
MsgBox("Excel file created")
Catch ex As Exception
End Try
我不太清楚您在这里想做什么,但这里有一个创建excel文件并用数组中的一些数据填充单元格的示例 数组示例--> 下面是您尝试使用的代码的链接 您缺少将年份和日期作为字符串返回以命名文件的函数。此外,您使用的原始代码中缺少方法
releaseObject()
测试代码
Imports System
Imports System.Windows.Forms
Imports Microsoft.Office.Interop
Namespace WinFormTestApp
Partial Public Class Form1
Inherits Form
Public Sub New()
Call LoadData()
End Sub
Private Sub LoadData()
Try
'Exporting to Excel.
'Dont know where this dtJelo comes from, so I just assigned a folder variable below.
'Dim folderPath As String = dtJelo.Rows(0)("jelly").ToString.Trim
Dim folderPath As String = "C:\temp\"
Dim filename As String = "JellyDetails" & current_yyyy() & current_mon() & ".xlsx"
Dim xlApp As Excel.Application = New Microsoft.Office.Interop.Excel.Application()
Dim xlWorkBook As Excel.Workbook = xlApp.Workbooks.Add
Dim xlWorkSheet As Excel.Worksheet = CType(xlWorkBook.Worksheets(1), Excel.Worksheet)
'Create an array to populate Excel with
Dim myArray(5) As String
myArray(0) = "This is line number 1"
myArray(1) = "This is line number 2"
myArray(2) = "This is line number 3"
myArray(3) = "This is line number 4"
myArray(4) = "This is line number 5"
myArray(5) = "This is line number 6"
'This is where they are adding the code.
xlWorkSheet.Cells(1, 1) = "Column Header Text"
For xx As Integer = 0 To UBound(myArray)
'Add 2 to xx so that you do not overwrite the Column Header
xlWorkSheet.Cells(xx + 2, 1) = myArray(xx)
Next
xlWorkBook.SaveAs($"{folderPath}{filename}", Excel.XlFileFormat.xlWorkbookDefault)
xlWorkBook.Close(True)
xlApp.Quit()
releaseObject(xlWorkSheet)
releaseObject(xlWorkBook)
releaseObject(xlApp)
'MsgBox("Excel file created")
MessageBox.Show($"Excel file created , you can find the file at {folderPath}{filename}")
Catch ex As Exception
End Try
End Sub
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
Private Function current_mon() As String
Try
Return String.Format(Now.Month.ToString, "00")
Catch ex As Exception
Return Nothing
End Try
End Function
Private Function current_yyyy() As String
Try
Return Now.Year.ToString
Catch ex As Exception
Return Nothing
End Try
End Function
End Class
End Namespace
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim xlApp As Excel.Application = New Microsoft.Office.Interop.Excel.Application()
If xlApp Is Nothing Then
MessageBox.Show("Excel is not properly installed!!")
Return
End If
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
xlWorkSheet.Cells(1, 1) = "Sheet 1 content"
xlWorkBook.SaveAs("d:\csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, _
Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue)
xlWorkBook.Close(True, misValue, misValue)
xlApp.Quit()
releaseObject(xlWorkSheet)
releaseObject(xlWorkBook)
releaseObject(xlApp)
MessageBox.Show("Excel file created, you can find the file d:\csharp-Excel.xls")
End Sub
'YOU WERE MISSING THIS FUNCTION IN YOUR EXAMPLE
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
他们的代码
Imports System
Imports System.Windows.Forms
Imports Microsoft.Office.Interop
Namespace WinFormTestApp
Partial Public Class Form1
Inherits Form
Public Sub New()
Call LoadData()
End Sub
Private Sub LoadData()
Try
'Exporting to Excel.
'Dont know where this dtJelo comes from, so I just assigned a folder variable below.
'Dim folderPath As String = dtJelo.Rows(0)("jelly").ToString.Trim
Dim folderPath As String = "C:\temp\"
Dim filename As String = "JellyDetails" & current_yyyy() & current_mon() & ".xlsx"
Dim xlApp As Excel.Application = New Microsoft.Office.Interop.Excel.Application()
Dim xlWorkBook As Excel.Workbook = xlApp.Workbooks.Add
Dim xlWorkSheet As Excel.Worksheet = CType(xlWorkBook.Worksheets(1), Excel.Worksheet)
'Create an array to populate Excel with
Dim myArray(5) As String
myArray(0) = "This is line number 1"
myArray(1) = "This is line number 2"
myArray(2) = "This is line number 3"
myArray(3) = "This is line number 4"
myArray(4) = "This is line number 5"
myArray(5) = "This is line number 6"
'This is where they are adding the code.
xlWorkSheet.Cells(1, 1) = "Column Header Text"
For xx As Integer = 0 To UBound(myArray)
'Add 2 to xx so that you do not overwrite the Column Header
xlWorkSheet.Cells(xx + 2, 1) = myArray(xx)
Next
xlWorkBook.SaveAs($"{folderPath}{filename}", Excel.XlFileFormat.xlWorkbookDefault)
xlWorkBook.Close(True)
xlApp.Quit()
releaseObject(xlWorkSheet)
releaseObject(xlWorkBook)
releaseObject(xlApp)
'MsgBox("Excel file created")
MessageBox.Show($"Excel file created , you can find the file at {folderPath}{filename}")
Catch ex As Exception
End Try
End Sub
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
Private Function current_mon() As String
Try
Return String.Format(Now.Month.ToString, "00")
Catch ex As Exception
Return Nothing
End Try
End Function
Private Function current_yyyy() As String
Try
Return Now.Year.ToString
Catch ex As Exception
Return Nothing
End Try
End Function
End Class
End Namespace
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim xlApp As Excel.Application = New Microsoft.Office.Interop.Excel.Application()
If xlApp Is Nothing Then
MessageBox.Show("Excel is not properly installed!!")
Return
End If
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
xlWorkSheet.Cells(1, 1) = "Sheet 1 content"
xlWorkBook.SaveAs("d:\csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, _
Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue)
xlWorkBook.Close(True, misValue, misValue)
xlApp.Quit()
releaseObject(xlWorkSheet)
releaseObject(xlWorkBook)
releaseObject(xlApp)
MessageBox.Show("Excel file created, you can find the file d:\csharp-Excel.xls")
End Sub
'YOU WERE MISSING THIS FUNCTION IN YOUR EXAMPLE
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
在此,我建议仅针对您的addrow,您可以将范围“A1”更改为其他范围:
'addrow (you can create function for this, with datatable parameter and range parameter to begin)
Dim IncludeLabel As Boolean = True
For xx = 0 To dtJelo.Rows.Count - 1
For yy = 0 To dtJelo.Columns.Count - 1
If IncludeLabel = True Then
If xx = 0 Then
xlWorkSheet.range("a1").Offset(xx,yy).value=dtJelo.Columns(yy).ColumnName
End If
xlWorkSheet.range("a1").Offset(xx+1,yy).value=dtJelo.Rows(xx)(yy)
Else
xlWorkSheet.range("a1").Offset(xx,yy).value=dtJelo.Rows(xx)(yy)
End If
Next
Next
只是在另一个问题中看到了这一点,它可能会使任务更容易。CloseXML作为Nuget包提供。