Я не совсем уверен, что вы пытаетесь сделать здесь, но вот пример создания файла Excel и заполнения ячеек некоторыми данными из массива.
Примеры массивов -> Массивы в Visual Basic
Итак, вот ссылка на код, который вы пытались использовать. Net-informations.com
Вам не хватало функций для возврата года и дня в виде строки для имени файла. Кроме того, вам не хватало метода 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