Don’t Export Persian fields finely from ASP.NET to Excel (VS2010 – C#)
-
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
-
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
Format your code snippets
Failure is not an option; it's the default selection.
-
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
-
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
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.
-
if you want export fine,you used row[m][n],each other cell, you'll format fields to excel.
A completely useless and uninformative answer
Failure is not an option; it's the default selection.