• Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

ASP.NET Excel Export

Durbs

Contractor Among Contractors
Joined
Sep 15, 2005
Messages
1,316
Likes received
0
Thanks received
0
This is doing my nut in. I've got a simple function that i've used countless times in VS 2003 .net 1.1 projects to export a dataset to Excel.

When i use this function in Visual Web Developer .net 2.0 projects though it exports the whole HTML page to excel, not just the supplied dataset.

The function is:

Code:
Sub ConvertToExcel(ByVal ds As DataSet, ByVal response As HttpResponse)

        response.Clear()
        response.Charset = ""
        'set the response mime type for excel
        response.ContentType = "application/vnd.ms-excel"
        'create a string writer
        Dim stringWrite As New System.IO.StringWriter
        'create an htmltextwriter which uses the stringwriter
        Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
        'instantiate a datagrid
        Dim dg As New DataGrid
        'set the datagrid datasource to the dataset passed in
        dg.DataSource = ds.Tables(0)
        'bind the datagrid
        dg.DataBind()
        'tell the datagrid to render itself to our htmltextwriter
        dg.RenderControl(htmlWrite)
        'all that's left is to output the html
        response.Write(stringWrite.ToString)
        HttpContext.Current.ApplicationInstance.CompleteRequest()

    End Sub

And is called from the page like: ConvertToExcel(YourDatasetName, Response)
tied to a buttons onclick event (plus code to generate dataset 'YourDatasetName').

Anyone know why this doesnt just export the dataset (it'll also export any text or images on the page that contains the button) or does anyone have a decent alternative .net 2.0 VB example as simple as this one?

Ta.
 
Yes, i am using a master page in this instance, hadnt thought of that one, will give it a go with a non master page'd site and see what happens.
 
:( Nah, still the same. Cant understand it, it just ignores the data sent to the function and writes the entire page to Excel.

Going to have to look into the XML route instead for this one i think.
 
Will have a play with that. Used that carlosag site before for his charting component which is fairly basic but does a nice job and is also free. The guy obviously knows his stuff!
 

Advertisers

Back
Top