[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how to Export Gridview and Treeview into Exel by Custom Webpart in wss 3.0??

Posted on 2010-01-07
7
Medium Priority
?
644 Views
Last Modified: 2012-05-08
Hii All Experts,
 i am using wss 3.0 and i made some custom webpart to display sharepoint list. And i used Gridview and Treeview control in web part. Now user need export button to export to Exel sheet. i tried by some code but i got error of .css file.
So i am looking for sample code for webpart in c# and for sharepoint. i tried a lot but i got same error "missing css file."..

Thank You.
0
Comment
Question by:guessdip
  • 4
  • 3
7 Comments
 
LVL 21

Expert Comment

by:66866
ID: 26208981
Are you looking to export from the Gridview or the Treeview?
0
 

Author Comment

by:guessdip
ID: 26212192
Hii, i am looking for both, Gridview to Exel and Treeview to Exel?? First i will try Gridview to Exel.
0
 

Author Comment

by:guessdip
ID: 26352616
I got solution from by myself..
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 21

Expert Comment

by:66866
ID: 26352827
what was your solution?
0
 

Author Comment

by:guessdip
ID: 26353202
i am attaching code..
  public static void GridViewExportUtil(string strFileName, GridView gv)
        {

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.AddHeader(
                "content-disposition", string.Format("attachment; filename={0}", strFileName));
            HttpContext.Current.Response.ContentType = "application/ms-excel";

            using (StringWriter sw = new StringWriter())
            {
                using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                {
                    //  Create a table to contain the grid
                    Table table = new Table();

                    //  include the gridline settings
                    table.GridLines = gv.GridLines;

                    //  add the header row to the table
                    if (gv.HeaderRow != null)
                    {
                       PrepareControlForExport(gv.HeaderRow);
                        table.Rows.Add(gv.HeaderRow);
                    }

                    //  add each of the data rows to the table
                    foreach (GridViewRow row in gv.Rows)
                    {
                        PrepareControlForExport(row);
                        table.Rows.Add(row);
                    }

                    //  add the footer row to the table
                    if (gv.FooterRow != null)
                    {
                        PrepareControlForExport(gv.FooterRow);
                        table.Rows.Add(gv.FooterRow);
                    }

                    //  render the table into the htmlwriter
                    table.RenderControl(htw);

                    //  render the htmlwriter into the response
                    HttpContext.Current.Response.Write(sw.ToString());
                    HttpContext.Current.Response.End();
                }
            }
        }

        private static void PrepareControlForExport(Control control)
        {
            for (int i = 0; i < control.Controls.Count; i++)
            {
                Control current = control.Controls[i];
                if (current is LinkButton)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
                }
                else if (current is ImageButton)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
                }
                else if (current is HyperLink)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
                }
                else if (current is DropDownList)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
                }
                else if (current is CheckBox)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
                }

                if (current.HasControls())
                {
                    PrepareControlForExport(current);
                }
            }


        }

Open in new window

0
 
LVL 21

Accepted Solution

by:
66866 earned 2000 total points
ID: 26353422
that's what I was about to send :-)
anyway glad everything's working for you
0
 

Author Comment

by:guessdip
ID: 26372834
Hii, Please can you take a look, i have problem with export to Excel::

http://www.experts-exchange.com/Programming/Languages/.NET/Q_25074674.html
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In case you ever have to remove a faulty web part from a page , add the following to the end of the page url ?contents=1
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Loops Section Overview
Suggested Courses

834 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