Click here to Skip to main content
15,913,587 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm try to export data into excel and so many code are used but unicode data not get in proper way

What I have tried:

protected void djb_Click(object sender, EventArgs e)
    {
string strFinal = "0";
        try
        {
            string Str = "";
            DataTable DTKhata = new DataTable();
            DTKhata.Columns.Add(new DataColumn("mutationno", typeof(string)));
            DTKhata.Columns.Add(new DataColumn("khata", typeof(string)));
            DTKhata.Rows.Add("सद्क्ब क", "asdbk askdb ");
            DTKhata.Rows.Add("1", "1");
            DTKhata.Rows.Add("1", "1");
            DTKhata.Rows.Add("1", "1");
            DTKhata.Rows.Add("1", "1");
            DTKhata.Rows.Add("1", "1");
            int Cnt = DTKhata.Columns.Count;
            if (DTKhata.Rows.Count > 0)
            {
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.ClearContent();
                HttpContext.Current.Response.ClearHeaders();
                HttpContext.Current.Response.Buffer = true;
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=mv.xls");
                HttpContext.Current.Response.ContentType = "application/ms-excel";
                
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
                HttpContext.Current.Response.Charset = "utf-8";
                //HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");

                HttpContext.Current.Response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");
                HttpContext.Current.Response.Write("<Table border=1>");

                HttpContext.Current.Response.Write("<tr><th style='text-align:left;mso-number-format:\"\\@\";\'>जिला</th> <th style='text-align:left;mso-number-format:\"\\@\";\'>teh</th> </tr>");

                foreach (DataRow row in DTKhata.Rows)
                {
                    HttpContext.Current.Response.Write("<TR>");
                    for (int i = 0; i < Cnt; i++)
                    {
                        HttpContext.Current.Response.Write("<td style=\'text-align:left;mso-number-format:\"\\@\";\'>");
                        HttpContext.Current.Response.Write(row[i].ToString());
                        HttpContext.Current.Response.Write("</Td>");
                    }
                    HttpContext.Current.Response.Write("</TR>");
                }
                HttpContext.Current.Response.Write("</Table>");
                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.End();
            }


        }
        catch (Exception ex)
        {
            //HttpContext.Current.Response.End();
        }
        finally
        {
            HttpContext.Current.Response.End();
        }
}
Posted
Updated 12-Oct-18 2:02am

1 solution

You are not creating an Excel file. What you are creating is an HTML file with an XLS extension. Excel is able to display the file because it can understand the HTML structure.

To create true Excel files look at the OpenXML SDK or EPPlus. This libraries allow you to create true Excel documents.
 
Share this answer
 

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