Dim f As New Functions
Dim ds As DataSet = f.EmpleadosEnActivo
Dim dt As DataTable = ds.Tables(0)
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) & "\Excels\MyTemplate.xlsx"
sTemplate = Server.MapPath(Request.ApplicationPath) & "\Excels\MyTemplate1.xltx"
oExcel.Visible = False
oExcel.DisplayAlerts = False
oBooks = oExcel.Workbooks
oBooks.Open(Server.MapPath(Request.ApplicationPath) & "\Excels\MyTemplate1.xltx")
oBook = oBooks.Item(1)
oSheets = oBook.Worksheets
oSheet = CType(oSheets.Item(1), Excel.Worksheet)
oSheet.Name = "CALCULO EMPLEADOS"
oCells = oSheet.Cells
f.DumpData(dt, oCells)
oSheet.Columns.AutoFit()
oSheet.SaveAs(sFile)
oExcel.Quit()
ReleaseComObject(oCells)
ReleaseComObject(oSheet)
ReleaseComObject(oSheets)
ReleaseComObject(oBook)
ReleaseComObject(oBooks)
ReleaseComObject(oExcel)
oExcel = Nothing
oBooks = Nothing
oBook = Nothing
oSheets = Nothing
oSheet = Nothing
oCells = Nothing
System.GC.Collect()
Response.Clear()
Response.ContentType = "application/ms-excel"
Response.AddHeader("Content-disposition", "attachment; filename=" & "RRHH.xlsx")
Response.WriteFile(".\Excels\MyTemplate.xlsx")
Response.Flush()
Response.Close()