Solved

export into excel based on stored procedure

Posted on 2008-10-24
5
1,372 Views
Last Modified: 2013-12-17
hi
i want to export data into excel by executing specific stored procedure, i dont want a grid view object on the page
0
Comment
Question by:mattibutt
  • 3
  • 2
5 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22803251
And, how would you want to do that?  Do you want to use Excel automation?  Write an Open XML Excel file?  

What version of C# and Office are you using?
0
 
LVL 11

Author Comment

by:mattibutt
ID: 22813112
thing is i have been using an example where i can export recrods based on Gridview component the problem is when i make the gridview object invisible then it doesnt work to simplify this task if i can just export it without showing the gridview object on the asp.net web page then my problem is sorted

 protected void ExportButton_Click(object sender, EventArgs e)

    {
 

        //Export the GridView to Excel
 

        PrepareGridViewForExport(Gridview1);
 

        ExportGridView();
 

    }
 
 
 

 private void ExportGridView()

    {
 

        string attachment = "attachment; filename=Contacts.xls";
 

        Response.ClearContent();
 

        Response.AddHeader("content-disposition", attachment);
 

        Response.ContentType = "application/ms-excel";
 

        StringWriter sw = new StringWriter();
 

        HtmlTextWriter htw = new HtmlTextWriter(sw);
 

        Gridview1.RenderControl(htw);
 

        Response.Write(sw.ToString());
 

        Response.End();
 

    }
 
 

private void PrepareGridViewForExport(Control gv)

    {
 

        LinkButton lb = new LinkButton();
 

        Literal l = new Literal();
 

        string name = String.Empty;
 

        for (int i = 0; i < gv.Controls.Count; i++)

        {
 

            if (gv.Controls[i].GetType() == typeof(LinkButton))

            {
 

                l.Text = (gv.Controls[i] as LinkButton).Text;
 

                gv.Controls.Remove(gv.Controls[i]);
 

                gv.Controls.AddAt(i, l);
 

            }
 

            else if (gv.Controls[i].GetType() == typeof(DropDownList))

            {
 

                l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;
 

                gv.Controls.Remove(gv.Controls[i]);
 

                gv.Controls.AddAt(i, l);
 

            }
 

            else if (gv.Controls[i].GetType() == typeof(CheckBox))

            {
 

                l.Text = (gv.Controls[i] as CheckBox).Checked ? "True" : "False";
 

                gv.Controls.Remove(gv.Controls[i]);
 

                gv.Controls.AddAt(i, l);
 

            }
 

            if (gv.Controls[i].HasControls())

            {
 

                PrepareGridViewForExport(gv.Controls[i]);
 

            }

        }

    }

Open in new window

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22813290
That shows that you are rendering a GridView to an HTML string, and then streaming that to the browser.  Since you don't want that, I would like to find out what you do really want.  Working with Excel automation (single threaded) is not very friendly with ASP.NET (multi-threaded), so I would like to find out which direction you would like to take.
0
 
LVL 11

Author Comment

by:mattibutt
ID: 22814291
multi-threaded would be fine
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 22814329
My friend, that could mean so many things, so I would have to suggest that you use OLE DB, and SQL statements to insert into an Excel worksheet.

Proof-of concept:

Reading and Writing Excel Spreadsheets Using ADO.NET C# DbProviderFactory
http://davidhayden.com/blog/dave/archive/2006/05/26/2973.aspx
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server - Slabs 9 38
TSQL DateADD update Question 4 29
how to install/upgrade the Blitz responder kit 8 25
How to use Full Text CONTAINS with Case in SQL 6 20
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

896 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now