' Set up a query Dim ta As New SomeTableAdapter Dim results As SomeDataTable = ta.GetData() ' Set up the Response headers to describe the content being delivered Response.Clear() Response.AddHeader("content-disposition", "attachment;filename=" & filename) Response.Charset = "" 'If you want the option to open the Excel file without saving then comment out the line below 'Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.ContentType = "application/vnd.xls" 'Register Aspose Liscence Dim cellslicense As Aspose.Cells.License = New Aspose.Cells.License() cellslicense.SetLicense("Aspose.Total.lic") Dim wb As New Aspose.Cells.Workbook ' Import Query results into the Workbook wb.Worksheets(0).Cells.ImportDataTable(results, True, "A1") ' Save workbook to a memory stream then output the memory stream to the HTTP Response object Dim stream As System.IO.MemoryStream stream = wb.SaveToStream() stream.WriteTo(Response.OutputStream) Response.End()
Tuesday, August 26, 2008
How to output a query as an Aspose workbook download.
Being an 'on the job'er rather than a 'cetified'er, I'm often tasked with figuring things out by doing them. Now now, stop your wailing and gnashing of teeth. Unlike the usual fly by night programmer I spend a lot of time researching before I do. That means learning things about what I'm using that are extraneous to the job at hand.
I do like to get thanked for this. The only way to do that is advertise, so every time I figure these things out I pass them around. It annoys my manager somewhat, but I'll be damned if I'm going to be invisible anymore.
So anyway, here's the first thing I figured out that was actually hard. It was my intro to the Response object and .NET streams.
How to output a query as an Aspose workbook verbatim as a download. For those of you that don't use Aspose, it is a suite of document editing dll's for ASP.NET. We use it for handling excel files and pdf's etc etc etc.
Here's the code.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment