Public Shared Sub Export(ByVal Filename As String, ByVal dt As DataTable, ByVal MRSMaster As String)
Dim RowNbr As Int32 = 0
Dim AllRecords(dt.Rows.Count) As Object
Try
'get MatRec into array
For Each Rw As DataRow In dt.Rows
AllRecords(RowNbr) = Rw.ItemArray()
RowNbr += 1
Next Rw
Dim oExcel As New Excel.Application
Dim oBooks As Excel.Workbooks, oBook As Excel.Workbook
Dim oSheets As Excel.Sheets, oSheet As Excel.Worksheet
Dim oCells As Excel.Range
'oExcel.Visible = False : oExcel.DisplayAlerts = False
'Open MRS template
oBook = oExcel.Workbooks.Open(MRSMaster)
oSheets = oBook.Worksheets
oSheet = CType(oSheets.Item(1), Excel.Worksheet)
oExcel.AddIns("Analysis ToolPak").Installed = True
'Do all the data export here.
Dim x As Integer
Dim y As Integer
Dim range As String
For x = 0 To AllRecords.GetUpperBound(0)
'************** HERE ***************
range = "A" & x + 2 & ":Q" & x + 2
oSheet.Range(range).Value = AllRecords(x)
Next
Select Case Right(MRSMaster, 13)
Case "antMaster.xls"
oSheet.Columns(17).delete()
oSheet.Columns(13).delete()
oSheet.Columns(2).delete()
oSheet.Columns(1).delete()
Case "MRSMaster.xls"
oSheet.Columns(14).delete()
oSheet.Columns(5).delete()
oSheet.Columns(2).delete()
oSheet.Columns(1).delete()
End Select
Dim MyDateTime As Date = Today
Dim MyStr As String
MyStr = Format(MyDateTime, "dd-mm-yy")
oSheet.Name = "PWBExport " & MyStr
oBook.SaveAs(Filename)
oBook.Close()
oExcel.DisplayAlerts = True
'Quit Excel and thoroughly deallocate everything
oExcel.Quit()
oExcel = Nothing : oBooks = Nothing : oBook = Nothing
oSheets = Nothing : oSheet = Nothing
AllRecords = Nothing
Catch ex As Exception
EH.Log(ex)
End Try
End Sub