Jul 04 2003

ASP export to excel

Posted by admin under ASP

This is an easy, not much to say about, way of exporting data from a webpage to Excel. It makes use of the fact that Excel can read HTML tables and I wont explain it a lot more cause it's pretty straight forward:


Function GenHtmlTable()
	Dim sRet 
	sRet = ""
	sRet = "<table border=1 class=mysmalltext><tr bgcolor=""#d8ecea"">"
	sRet = sRet & "<td>Name</td>"	
	sRet = sRet & "<td>Country</td>"	
	sRet = sRet & "</tr>"	
	
	'Beginning rows...Might read from Recordset - here it's hardcoded
	sRet = sRet & "<tr>"	
	sRet = sRet & "<td>John Doe</td>"	
	sRet = sRet & "<td>US</td>"	
	sRet = sRet & "</tr>"	

	sRet = sRet & "<tr>"	
	sRet = sRet & "<td>Stefan Holmberg</td>"	
	sRet = sRet & "<td>Sweden</td>"	
	sRet = sRet & "</tr>"	

	sRet = sRet & "</table>"	
                GenHtmlTable = sRet
End Function






Response.Clear() Response.Buffer = True Response.AddHeader "Content-Disposition", "attachment;filename=export.xls" Response.ContentType = "application/vnd.ms-excel" Response.Write GenHtmlTable() Response.End()