Solved

export into excel based on stored procedure

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
convert in derived column 7 30
SQL SELECT query help 7 40
SQL View nearest date 5 36
How to construct an if else statement from existing code 6 21
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how the fundamental information of how to create a table.

773 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