In Deiner Funktion hast Du einige Variablen definiert. Ich weiss nicht, wozu die gut sind.
Ich habe die Funktion, soweit ich nachvollziehen kann, nachgebaut. Habe leider noch nicht intensiv getestet. Probiere einfach mal aus.
Export-Funktion
Public Sub ExportToExcel(ByVal saveDir As String, _
ByVal kameraName As String, _
ByVal importDir As String)
Dim xlApp As Microsoft.Office.Interop.Excel.Application = Nothing
Dim xlWb As Microsoft.Office.Interop.Excel.Workbook = Nothing
Dim xlWs As Microsoft.Office.Interop.Excel.Worksheet = Nothing
Try
' Speicherort vorbereiten
If String.IsNullOrEmpty(saveDir) Then _
Throw New Exception("Speicherort darf nicht leer sein.")
Dim dSaveDirKamera As New _
IO.DirectoryInfo(IO.Path.Combine(saveDir, kameraName))
If Not dSaveDirKamera.Exists Then dSaveDirKamera.Create()
' Ini-Datei
Dim fiIni As New _
IO.FileInfo(IO.Path.Combine(importDir, kameraName & _
"\camera.ini"))
If Not fiIni.Exists Then _
Throw New Exception("Ini-Datei nicht vorhanden.")
' Ini Auslesen
Dim cameraIni() As String = IO.File.ReadAllLines( _
fiIni.FullName, System.Text.Encoding.Default)
' Datei-Pfad vorbereiten
Dim fiXls As New IO.FileInfo(IO.Path.Combine( _
dSaveDirKamera.FullName, _
Now.ToString("yyyy-MM-dd_HH_mm_ss") & _
".xls"))
' Neue Excel-Instanz öffnen
xlApp = New Microsoft.Office.Interop.Excel.Application
xlWb = xlApp.Workbooks.Add
xlWs = xlWb.Worksheets(1)
xlWs.Name = kameraName
' Daten füllen
With xlWs.Cells(1, 1)
.Value = "Aimetis In/Out Counter:"
.Font.FontStyle = "lucida fax"
.Font.Underline = True
.Font.Bold = True
.Font.Size = 12
End With
With xlWs.Cells(3, 1)
.Value = "Datum:"
.Font.Bold = True
End With
With xlWs.Cells(3, 3)
.Value = "Count In:"
.Font.Bold = True
End With
With xlWs.Cells(3, 4)
.Value = "Count Out:"
.Font.Bold = True
End With
With xlWs.Cells(5, 1)
.value = "=HEUTE()"
End With
xlWs.Cells(5, 3).value = cameraIni(19).Substring(15)
xlWs.Cells(5, 4).value = cameraIni(29).Substring(16)
xlWb.SaveAs(fiXls.FullName)
xlWb.Close()
xlApp.Quit()
Catch ex As Exception
Throw ex
Finally
If xlWs IsNot Nothing Then xlWs = Nothing
If xlWb IsNot Nothing Then xlWb = Nothing
If xlApp IsNot Nothing Then xlApp.Quit()
Runtime.InteropServices.Marshal.ReleaseComObject(xlWs)
Runtime.InteropServices.Marshal.ReleaseComObject(xlWb)
Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
End Try
End Sub Aufrufen
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim exportPath As String = "C:\Export"
Dim importPath As String = "C:\Import"
For index As Integer = 0 To chkList.CheckedItems.Count - 1
Me.ExportToExcel(exportPath, _
chkList.CheckedItems(index).ToString.Trim, _
importPath)
Next
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Export")
End Try
End Sub |