Hejexportera data till Excel
Jag skulle experotera mina 3 GridView till Excel, Jag har googlat och fått lite hjälp,
har lyckats men när jag trycker på knappen som kör exporteringen då min sida ändrar utseende helt,
fonten ändras, tabller blir mycket stora , (jätte fullt utseende)
här är koden jag har använt:
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) & "\MyExcel.xls"
sTemplate = Server.MapPath(Request.ApplicationPath) & "\MyTemplate.xls"
Dim CurrentCI As System.Globalization.CultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture
System.Threading.Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo("en-US")
oExcel.Visible = False : oExcel.DisplayAlerts = False
oBooks = oExcel.Workbooks
oBooks.Open(Server.MapPath(Request.ApplicationPath) & "\MyTemplate.xls") 'Load colorful template with chart
oBook = oBooks.Item(1)
oSheets = oBook.Worksheets
oSheet = CType(oSheets.Item(1), Excel.Worksheet)
oSheet.Name = "First Sheet"
DumpData(MinDataTable, oCells) //denna metod bara lägger till värdet i cellerna
oSheet.SaveAs(sFile)
oBook.Close()
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()
System.Threading.Thread.CurrentThread.CurrentCulture = CurrentCI
oCells = oSheet.Cells