Click here to Skip to main content
15,896,730 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
pls find the code snippet

C#
DataGrid gv = new DataGrid();
            gv.ItemDataBound += new DataGridItemEventHandler(dataExportExcel_ItemDataBound);

            

            gv.DataSource = dt;
            gv.DataBind();
            string path = Server.MapPath("~/Sheets/");
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            using (StringWriter sw = new StringWriter())
            {
                using (HtmlTextWriter hw = new HtmlTextWriter(sw))
                {
                    StreamWriter writer = File.AppendText(path + "GridView.xls");
                    gv.RenderControl(hw);
                    StringBuilder sbResponseString = new StringBuilder();

            sbResponseString.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns: o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=windows-1252\"><!--[if gte mso 9]><xml><x:excelworkbook xmlns:x="#unknown"><x:excelworksheets><x:excelworksheet><x:name>"+ worksheetName +"</x:name><x:worksheetoptions><x:panes></x:panes></x:worksheetoptions></x:excelworksheet></x:excelworksheets></x:excelworkbook></xml><![endif]--></head> <body>");

            sbResponseString.Append(sw + "</body></html>");

            writer.WriteLine(sbResponseString.ToString());
                    writer.Close();
                }
            }
        }


        void dataExportExcel_ItemDataBound(object sender, DataGridItemEventArgs e)

        {

            if (e.Item.ItemType == ListItemType.Header)

            {

                //Header Text Format can be done as follows

                e.Item.Font.Bold = true;

 

                //Adding Filter/Sorting functionality for the Excel

                int cellIndex = 0;

                while (cellIndex < e.Item.Cells.Count)

                {

                    e.Item.Cells[cellIndex].Attributes.Add("x:autofilter", "all");

                    e.Item.Cells[cellIndex].Width = 200;

                    e.Item.Cells[cellIndex].HorizontalAlign = HorizontalAlign.Center;

                    cellIndex++;

                }

            }

 

            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

            {

                int cellIndex = 0;

                while (cellIndex < e.Item.Cells.Count)

                {

                    //Any Cell specific formatting should be done here

                    e.Item.Cells[cellIndex].HorizontalAlign = HorizontalAlign.Left;

                    cellIndex++;

                }
           }

        }

    }
}


appcmd.exe set config -section:system.webserver/serverruntime/uploadreadaheadsize: 1048576 /commit:apphost
Posted
Updated 2-Aug-12 8:13am
v3
Comments
barneyman 2-Aug-12 0:27am    
It might pay to be a little more forthcoming with your problem ... currently you're asking is to put considerably more effort into the solution, than you made in asking the question ...

What fails, how does it fail, what line does it fail on - you know - the groundwork?

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900