Solved

asp.net vb.net export dataset to excel

Posted on 2009-04-15
4
2,602 Views
Last Modified: 2012-05-06
How can in asp.net export a dataset to excel.
0
Comment
Question by:rflorencio
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 14

Expert Comment

by:GiftsonDJohn
ID: 24146917
You have to bind the DataSet to DataGrid or GridView. Then you can use the following code to download as excel sheet.
DataGrid dg = new DataGrid();
                    dg.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
                    dg.HeaderStyle.Font.Bold = true;
 
                    dg.DataSource = ds.Tables[0];
                    dg.DataBind();
 
                    Response.Clear();
                    Response.Buffer = true;
                    Response.ContentType = "application/vnd.ms-excel";
                    Response.AddHeader("Content-Disposition", "attachment; filename=Consolidated Report.xls");
                    Response.Charset = "";
                    this.EnableViewState = false;
                    System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
                    System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
                    dg.RenderControl(oHtmlTextWriter);
                    Response.Write(oStringWriter.ToString());
                    Response.End();

Open in new window

0
 
LVL 14

Expert Comment

by:GiftsonDJohn
ID: 24146922
Here is the VB.net equivalent
Dim dg As New DataGrid()
dg.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
dg.HeaderStyle.Font.Bold = True
 
dg.DataSource = ds.Tables(0)
dg.DataBind()
 
Response.Clear()
Response.Buffer = True
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("Content-Disposition", "attachment; filename=Consolidated Report.xls")
Response.Charset = ""
Me.EnableViewState = False
Dim oStringWriter As New System.IO.StringWriter()
Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)
dg.RenderControl(oHtmlTextWriter)
Response.Write(oStringWriter.ToString())
Response.End()

Open in new window

0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 24146937
are you using VB2008? In this case, see how easy it is to generate a CSV or XML document from a dataset: http://www.emoreau.com/Entries/Articles/2009/04/Using-LINQ-and-XML-Literals-to-transform-a-DataTable-into-a-HTML-table.aspx
0
 
LVL 6

Accepted Solution

by:
badalpatel earned 500 total points
ID: 24147438
You dont need to bind your dataset to the datagrid.
You can directy export to excel.
Code is below

 Sub exportToexCEl()
        Dim sSQL As String
        sSQL = "select * from t_user where suserType='n'"
        Dim ds As DataSet = CommonMethods.GetDataset(sSQL)
        Dim dt As DataTable = ds.Tables(0)
        Dim attachment As String = "attachment; filename=userlist.xls"
        Response.ClearContent()
        Response.AddHeader("content-disposition", attachment)
        Response.ContentType = "application/vnd.ms-excel"
        Dim tab As String = ""
        For Each dc As DataColumn In dt.Columns
            Response.Write(tab + dc.ColumnName)
            tab = vbTab
        Next
        Response.Write(vbLf)
        Dim i As Integer
        For Each dr As DataRow In dt.Rows
            tab = ""
            For i = 0 To dt.Columns.Count - 1
                Response.Write(tab + dr(i).ToString().Replace(vbCrLf, " "))
                tab = vbTab
            Next
            Response.Write(vbLf)
        Next
        Response.End()
    End Sub

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question