Guest User

Untitled

a guest
Jan 17th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. Public Class ImportService
  2. Public Function ImportsFiles(ByVal files as list(Of String))
  3. Dim rowEffect As Integer = 0
  4. For j as Integer=0 to files.Count-1
  5. dim fileAddress=files(j)
  6. Dim app As New Application
  7. Dim Wbook As Workbook
  8.  
  9. Try
  10. Wbook = app.Workbooks.Open(fileAddress, [ReadOnly]:=True)
  11. For i As Integer = 1 To Wbook.Sheets.Count
  12. If Not Wbook.Sheets(i).Name.ToString.ToLower.Contains("partial read of load profile") Then
  13. Dim con As New System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & fileAddress & "; Extended Properties=""Excel 12.0 Xml;HDR=No;IMEX=1"";")
  14. Dim cmd As New OleDbCommand
  15. Dim s As String = "SELECT * FROM [" & Wbook.Sheets(i).Name & "$]"
  16. cmd.CommandText = s
  17. cmd.Connection = con
  18. Dim da As New OleDbDataAdapter
  19. da.SelectCommand = cmd
  20. Dim dt As New System.Data.DataTable
  21. da.Fill(dt)
  22. Dim err As New Dictionary(Of String, String)
  23. Dim fileDate As String = Wbook.Sheets(i).Name.ToString.Trim.Replace(" ", "/")
  24. rowEffect += InsertLps(Path.GetFileName(fileAddress), fileDate, dt.Rows, err)
  25. dt.Dispose()
  26. da.Dispose()
  27. con.Dispose()
  28. cmd.Dispose()
  29. End If
  30. ReleaseCOMObject(Wbook.Sheets(i))
  31. Next
  32. Wbook.Close(SaveChanges:=False)
  33. Catch ex As Exception
  34. summary.Add(fileAddress, ex.Message)
  35. Finally
  36. GC.Collect()
  37. GC.WaitForPendingFinalizers()
  38. GC.Collect()
  39. GC.WaitForPendingFinalizers()
  40. If app.Workbooks IsNot Nothing Then
  41. For Each wb In app.Workbooks
  42. For Each ws In wb.Worksheets
  43. ReleaseCOMObject(ws)
  44. Next
  45. wb.Close(False)
  46. ReleaseCOMObject(wb)
  47. Next
  48. End If
  49. app.Workbooks.Close()
  50. app.Quit()
  51. ReleaseCOMObject(app)
  52. GC.WaitForFullGCComplete()
  53. Next
  54. End Function
  55. End Class
  56.  
  57. Public Class ImportJob
  58. Implements IJob
  59. Public Sub Execute(context As IJobExecutionContext) Implements IJob.Execute
  60. Dim importHelpers As New ImportFilesHelpers
  61. Dim exts As New List(Of String)
  62. exts.Add(".xls")
  63. Dim files = importHelpers.GetFiles(ImportFilesHelpers.BaseAssress, exts)
  64. Dim import = New ImportService()
  65. import.ImportsFiles(files)
  66. End Sub
  67. End Class
  68.  
  69. Dim fileDate As String = Wbook.Sheets(i).Name.ToString.Trim.Replace(" ", "/")
Add Comment
Please, Sign In to add comment