Click here to Skip to main content
15,941,690 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have listView at the form and some table in it with russian words. When I export data to Excel english words and numbers exports well, but russian words turned to some symbols, but not words. Help me! My code:

C#
string[] st = new string[listView1.Columns.Count];
            DirectoryInfo di = new DirectoryInfo(@"..");
            if (di.Exists == false)
                di.Create();
            StreamWriter sw = new StreamWriter(@".." + "1.xls", false);
            sw.AutoFlush = true;
            for (int col = 0; col < listView1.Columns.Count; col++)
            {
                sw.Write("\t" + listView1.Columns[col].Text.ToString());
            }

            int rowIndex = 1;
            int row = 0;
            string st1 = "";
            for (row = 0; row < listView1.Items.Count; row++)
            {
                if (rowIndex <= listView1.Items.Count)
                    rowIndex++;
                st1 = "\n";
                for (int col = 0; col < listView1.Columns.Count; col++)
                {
                    st1 = st1 + "\t" + listView1.Items[row].SubItems[col].Text.ToString();
                }
                sw.WriteLine(st1);
            }
            sw.Close();
            FileInfo fil = new FileInfo(@".." + "1.xls");
            if (fil.Exists == true)
                MessageBox.Show("Process Completed", "Export to Excel", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        catch (Exception ex)
        {

        }


What I have tried:

I tried to edit code like
<pre lang="c#"> StreamWriter sw = new StreamWriter(@".." + "1.xls", false, Encoding.UTF8);
and russian symbols is okay, but all data go to one column at Excel :(
Posted
Updated 15-Feb-17 22:31pm

1 solution

Can you please try with below code as sample? it works from me

I have data in dataview (C#) and I've created a html string at C# using Foreach loop and export the html string to Excel via
StringWriter()


I've done this using Datatable, but can do the same using dataview.


protected void btnExportToExcel_Click(object sender, EventArgs e)
       {
           Response.Clear();
           Response.Buffer = true;
           Response.AddHeader("content-disposition",
               "attachment;filename=SearchLog.xls");
           Response.Charset = "";
           Response.ContentType = "application/vnd.ms-excel";

           StringWriter sw = new StringWriter();

           if (Session["dtlog"] != null)
           {
               // string strSearchResult = Session["htmlResult"].ToString();
               DataTable dtSeachLog = Session["dtlog"] as DataTable;


               string strSearchResult = "No Record Found!";

               if (dtSeachLog != null && dtSeachLog.Rows.Count > 0)
               {
                   strSearchResult = "<div class='table - responsive'> <table border='1' class='table table-bordered table - hover'><thead><tr>";
                   strSearchResult = strSearchResult + "<th class='header'>User Name</th> <th class='header'>Trade Mark</th><th class='header'>Date</th><th class='header'>Us Class Description</th>";
                   strSearchResult = strSearchResult + "</tr></thead><tbody>";
                   foreach (DataRow dr in dtSeachLog.Rows)
                   {

                       strSearchResult = strSearchResult + "<tr><td>" + dr["FullName"].ToString() + " </ a ></td>";

                       strSearchResult = strSearchResult + "<td align='center'> " + dr["SearchText"].ToString().Replace("''","'") + "</td>";
                       strSearchResult = strSearchResult + "<td>" +Convert.ToDateTime(dr["SearchDate"]).ToShortDateString() + "</td>";
                       strSearchResult = strSearchResult + "<td align='center'>" + dr["UsClassDescription"].ToString() + "</td>";
                       strSearchResult = strSearchResult + "</tr>";
                   }
                   strSearchResult = strSearchResult + "</tbody></table></div>";
               }
                   sw.WriteLine(strSearchResult);
               //HtmlTextWriter hw = new HtmlTextWriter(sw);

               Response.Output.Write(sw.ToString());
               Response.Flush();
               Response.End();
           }
       }
 
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