Solved

export into excel based on stored procedure

Posted on 2008-10-24
5
1,371 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

743 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

15 Experts available now in Live!

Get 1:1 Help Now