• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • 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

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    ASP.NET Excel Export

    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.

    #2
    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.

    Comment


      #3
      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.

      Comment


        #4
        Have a look here. I'm using this (albeit on 1.1) and it works a treat.

        http://forums.contractoruk.com/thread6725.html

        Comment


          #5
          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!

          Comment

          Working...
          X