Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have developed a application using ASP and SQL Server 2008...
Hindi data is stored in database and displayed same data in gridview..
Now, I want to export this data to excel file..

I have tried by using following code but its not working..

C#
DataTable table = (DataTable)GridView1.DataSource;
            if (table != null)
                if (table.Rows.Count > 0)
                {
                    String file_name;
                    if (TextBox9.Text.Trim() == String.Empty)
                        file_name = "All_Data";
                    else
                        file_name = DropDownList1.Text + "_" + TextBox9.Text.Trim();
                    HttpContext context = HttpContext.Current;
                    context.Response.Clear();

                    Response.ContentEncoding = System.Text.Encoding.UTF8;
                    Response.ClearContent();
                    string attach = "attachment;filename=" + file_name + ".xls";
                    Response.AddHeader("content-disposition", attach);
                    Response.ContentType = "application/ms-excel";

                    if (table != null)
                    {
                        foreach (DataColumn dc in table.Columns)
                        {
                            Response.Write(dc.ColumnName + "\t");
                            //sep = ";";
                        }
                        Response.Write(System.Environment.NewLine);
                        foreach (DataRow dr in table.Rows)
                        {
                            for (int i = 0; i < table.Columns.Count; i++)
                            {
                                Response.Write(Server.HtmlDecode(dr[i].ToString() + "\t"));
                            }
                            Response.Write("\n");
                        }
                        Response.End();
                    }
                }




Plz help....URGENT..........
Posted
Updated 8-Mar-14 23:52pm
v2
Comments
Kornfeld Eliyahu Peter 9-Mar-14 6:24am    
Can you elaborate on which way the code not working?
Mayuri Gandhi 9-Mar-14 12:46pm    
Hindi data is getting exported as symbols like 'हॉटेल'...
Bernhard Hiller 10-Mar-14 6:27am    
Are you sure you are creating that excel attachment correctly? I doubt that it works correctly even with US ASCII characters only!
Mayuri Gandhi 11-Mar-14 2:09am    
No, I am not sure.. I just tried with that code but its not working..
Can you attach your code here????
Bernhard Hiller 11-Mar-14 4:26am    
No, you are expected to try that first. Google Bing etc will help you.

1 solution

Refer

export-gridview-to-excel-and-wor-with-arabic-content/[^]

The link shows different language , but it should work for your case too.
 
Share this answer
 
Comments
Mayuri Gandhi 20-Mar-14 6:02am    
Thank u So much JoCodes.. Its working for me..
Now hindi data is getting exported.. but I have gridview with so many pages..
And when I export by this method, only first page is getting exported..
How to solve this??
Plz gelp..
And thanks once again..
JoCodes 20-Mar-14 8:15am    
Ok Try as below ,
Add these to Markup of Gridview
AllowPaging="true" OnPageIndexChanging="OnPageIndexChanging" Then add the Event Code in Codebehind for Pageindex changing like

protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGridView();//Bind Method for populating Gridview
}
Then Modify the Excel Export Code as Below
protected void ExportExcel()
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader(“content-disposition”, “attachment;filename=GridViewExport.xls”);
Response.Charset = “”;
Response.ContentType = “application/ms-excel”;
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
//Add these 2 lines of Code
GridView1.AllowPaging = false;
this.BindGrid();
GridView1.RenderControl(hw);
//style to format numbers to string
string style = @”<style> .textmode { mso-number-format:\@; } </style>”;
Response.Output.Write(“<meta http-equiv=\”Content-Type\” content=\”text/html; charset=utf-8\”>”);// add this line to fix characterset in arabic
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
. Try these and revert to me. Dont forget to Mark it as an Answer and Upvote if helped you.
Mayuri Gandhi 20-Mar-14 12:25pm    
Ya.. Its absolutely working..
Thank you so much..
JoCodes 20-Mar-14 13:20pm    
You are welcome Mayuri, Glad to hear ur issue is solved.
Mayuri Gandhi 23-Mar-14 13:30pm    
JoCodes, Plz help me in my next issue..

http://www.codeproject.com/Questions/748327/Add-checkbox-to-gridview-column-dynamically

I am sure u will definately help me..
So thanks in advance..

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



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