arsolati
یک شنبه 18 مرداد 1388, 11:57 صبح
سلام
من می خواستم از GridView ام خروجی Excel بگیرم.
تونستم این کار رو بکنم ولی مشکل من با عبارات فارسی داخل GridView هست که بعد از تبدیل به خروجی Excel یونیکدش به هم می ریزه و مطمئن شدم که اشکال از StringWriter هست که با UTF-16 کار می کنه و پراپرتی هاش هم فقط get دارن و set ندارن که بشه به UTF-8 تبدیل بشه..من کدم رو گذاشتم..
لطفا راهنمایی می کنین که چه جوری می شه این مشکل رو حل کرد؟؟
public
staticvoid Export(string fileName, GridView gv)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader(
"content-disposition", string.Format("attachment; filename={0}", fileName));
HttpContext.Current.Response.ContentType = "application/ms-excel";
using (StringWriter sw = newStringWriter())
{
using (HtmlTextWriter htw = newHtmlTextWriter(sw))
{
// Create a form to contain the grid
Table table = newTable();
// add the header row to the table
if (gv.HeaderRow != null)
{
GridViewExportUtil.PrepareControlForExport(gv.Head erRow);
table.Rows.Add(gv.HeaderRow);
}
// add each of the data rows to the table
foreach (GridViewRow row in gv.Rows)
{
GridViewExportUtil.PrepareControlForExport(row);
table.Rows.Add(row);
}
// add the footer row to the table
if (gv.FooterRow != null)
{
GridViewExportUtil.PrepareControlForExport(gv.Foot erRow);
table.Rows.Add(gv.FooterRow);
}
// 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();
}
}
}
///<summary>
/// Replace any of the contained controls with literals
///</summary>
///<param name="control"></param>
privatestaticvoid PrepareControlForExport(Control control)
{
for (int i = 0; i < control.Controls.Count; i++)
{
Control current = control.Controls[i];
if (current isLinkButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, newLiteralControl((current asLinkButton).Text));
}
elseif (current isImageButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, newLiteralControl((current asImageButton).AlternateText));
}
elseif (current isHyperLink)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, newLiteralControl((current asHyperLink).Text));
}
elseif (current isDropDownList)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, newLiteralControl((current asDropDownList).SelectedItem.Text));
}
elseif (current isCheckBox)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, newLiteralControl((current asCheckBox).Checked ? "True" : "False"));
}
if (current.HasControls())
{
GridViewExportUtil.PrepareControlForExport(current );
}
}
}
من می خواستم از GridView ام خروجی Excel بگیرم.
تونستم این کار رو بکنم ولی مشکل من با عبارات فارسی داخل GridView هست که بعد از تبدیل به خروجی Excel یونیکدش به هم می ریزه و مطمئن شدم که اشکال از StringWriter هست که با UTF-16 کار می کنه و پراپرتی هاش هم فقط get دارن و set ندارن که بشه به UTF-8 تبدیل بشه..من کدم رو گذاشتم..
لطفا راهنمایی می کنین که چه جوری می شه این مشکل رو حل کرد؟؟
public
staticvoid Export(string fileName, GridView gv)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader(
"content-disposition", string.Format("attachment; filename={0}", fileName));
HttpContext.Current.Response.ContentType = "application/ms-excel";
using (StringWriter sw = newStringWriter())
{
using (HtmlTextWriter htw = newHtmlTextWriter(sw))
{
// Create a form to contain the grid
Table table = newTable();
// add the header row to the table
if (gv.HeaderRow != null)
{
GridViewExportUtil.PrepareControlForExport(gv.Head erRow);
table.Rows.Add(gv.HeaderRow);
}
// add each of the data rows to the table
foreach (GridViewRow row in gv.Rows)
{
GridViewExportUtil.PrepareControlForExport(row);
table.Rows.Add(row);
}
// add the footer row to the table
if (gv.FooterRow != null)
{
GridViewExportUtil.PrepareControlForExport(gv.Foot erRow);
table.Rows.Add(gv.FooterRow);
}
// 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();
}
}
}
///<summary>
/// Replace any of the contained controls with literals
///</summary>
///<param name="control"></param>
privatestaticvoid PrepareControlForExport(Control control)
{
for (int i = 0; i < control.Controls.Count; i++)
{
Control current = control.Controls[i];
if (current isLinkButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, newLiteralControl((current asLinkButton).Text));
}
elseif (current isImageButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, newLiteralControl((current asImageButton).AlternateText));
}
elseif (current isHyperLink)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, newLiteralControl((current asHyperLink).Text));
}
elseif (current isDropDownList)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, newLiteralControl((current asDropDownList).SelectedItem.Text));
}
elseif (current isCheckBox)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, newLiteralControl((current asCheckBox).Checked ? "True" : "False"));
}
if (current.HasControls())
{
GridViewExportUtil.PrepareControlForExport(current );
}
}
}