Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Don’t Export Persian fields finely from ASP.NET to Excel (VS2010 – C#)

Don’t Export Persian fields finely from ASP.NET to Excel (VS2010 – C#)

Scheduled Pinned Locked Moved ASP.NET
csharpquestioncssasp-net
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    armm1388
    wrote on last edited by
    #1

    Hi I export my GrideView from asp.net to excel but the fields which contain Persian character don’t export finely (VS2010 – C#). HOW CAN I DO IT? My code is : HttpContext.Current.Response.Clear(); HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", "Exported")); HttpContext.Current.Response.Charset = ""; 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(); foreach (GridViewRow row in GridView1.Rows) { table.Rows.Add(row); } // 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(); } } Thanks very much

    N J 3 Replies Last reply
    0
    • A armm1388

      Hi I export my GrideView from asp.net to excel but the fields which contain Persian character don’t export finely (VS2010 – C#). HOW CAN I DO IT? My code is : HttpContext.Current.Response.Clear(); HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", "Exported")); HttpContext.Current.Response.Charset = ""; 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(); foreach (GridViewRow row in GridView1.Rows) { table.Rows.Add(row); } // 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(); } } Thanks very much

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      Format your code snippets


      Failure is not an option; it's the default selection.

      1 Reply Last reply
      0
      • A armm1388

        Hi I export my GrideView from asp.net to excel but the fields which contain Persian character don’t export finely (VS2010 – C#). HOW CAN I DO IT? My code is : HttpContext.Current.Response.Clear(); HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", "Exported")); HttpContext.Current.Response.Charset = ""; 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(); foreach (GridViewRow row in GridView1.Rows) { table.Rows.Add(row); } // 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(); } } Thanks very much

        J Offline
        J Offline
        Jack Li
        wrote on last edited by
        #3

        if you want export fine,you used row[m][n],each other cell, you'll format fields to excel.

        N 1 Reply Last reply
        0
        • A armm1388

          Hi I export my GrideView from asp.net to excel but the fields which contain Persian character don’t export finely (VS2010 – C#). HOW CAN I DO IT? My code is : HttpContext.Current.Response.Clear(); HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", "Exported")); HttpContext.Current.Response.Charset = ""; 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(); foreach (GridViewRow row in GridView1.Rows) { table.Rows.Add(row); } // 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(); } } Thanks very much

          N Offline
          N Offline
          Not Active
          wrote on last edited by
          #4

          You don't export the GridView, you export the DataSource of the GridView. Assuming you are using a DataTable as the DatSource

          StringBuilder sb = new StringBuilder();
          List rows = new List();
          foreach(DataColumn col in DataSource.Columns)
          {
          rows.Add(col.ColumnName);
          }

          sb.AppendLine(string.Join(",", rows.ToArray()));
          rows.Clear();
          foreach(DataRow row in DataSource.Rows)
          {
          foreach(object item in row.ItemArray)
          {
          rows.Add(item.ToString());
          }
          sb.AppendLine(string.Join(",", rows.ToArray()));
          rows.Clear();
          }

          Response.ContentType = "Application/x-msexcel";
          Response.AddHeader("content-disposition", "attachment;filename=export.csv");
          Response.Clear();
          using(StreamWriter writer = new StreamWriter(Response.OutputStream, Encoding.UTF8))
          {
          writer.Write(sb.ToString());
          }
          Response.End();


          Failure is not an option; it's the default selection.

          1 Reply Last reply
          0
          • J Jack Li

            if you want export fine,you used row[m][n],each other cell, you'll format fields to excel.

            N Offline
            N Offline
            Not Active
            wrote on last edited by
            #5

            A completely useless and uninformative answer


            Failure is not an option; it's the default selection.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups