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
Dim sFile As String, sTemplate As String
sFile = Server.MapPath(Request.ApplicationPath) & "RESULTS\" & CType(Qid, String) & " - " & CType(Session("ReportName"), String) & " RESULTS.xls"
sTemplate = Server.MapPath(Request.ApplicationPath) & _
"\HPTResults.xlt"
Dim rFile As String 'Results File
rFile = "\\bls2mbr25\RESULTS\" & CType(Qid, String) & " - " & CType(Session("ReportName"), String) & " RESULTS.xls"
oExcel.Visible = False : oExcel.DisplayAlerts = False
'Start a new workbook
oBooks = oExcel.Workbooks
oBooks.Open(Server.MapPath(Request.ApplicationPath) & _
"\HPTResults.xlt") 'Load template
oBook = oBooks.Item(1)
oSheets = oBook.Worksheets
oSheet = CType(oSheets.Item("rawdata"), Excel.Worksheet)
'Do all the data export here.
'Export Team Answers
Dim x As Integer
Dim y As Integer
Dim range As String
For x = 0 To Answers.GetUpperBound(0)
y = x + 2
range = "A" & y & ":D" & y
oSheet.Range(range).Value = Answers(x)
Next
'Export TeamLeader Answers
For x = 0 To Leader.GetUpperBound(0)
y = x + 2
range = "E" & y & ":G" & y
oSheet.Range(range).Value = Leader(x)
Next
oSheet.SaveAs(sFile)
oBook.Close()
'Quit Excel and thoroughly deallocate everything
oExcel.Quit()
ReleaseComObject(oSheet)
ReleaseComObject(oSheets) : ReleaseComObject(oBook)
ReleaseComObject(oBooks) : ReleaseComObject(oExcel)
oExcel = Nothing : oBooks = Nothing : oBook = Nothing
oSheets = Nothing : oSheet = Nothing
System.GC.Collect()
Label7.Text = rFile
Response.Redirect(rFile) 'Send the user to the file
End Sub