/// <summary>
/// 导出合并的gridview
/// </summary>
/// <param name="dataSource">数据源</param>
/// <param name="startColoum">要合并起始列,初始值:0</param>
/// <param name="endColoum">要合并的结束列序号</param>
/// <param name="filename">导出的文件名</param>
/// <returns></returns>
public virtual ActionResult ExportMergedGridViewToExcel(object dataSource, int startColoum, int endColoum, string filename = "excel")
{
GridView gv = new GridView();
gv.DataSource = dataSource;
gv.DataBind();
MergeGridViewCell.MergeRow(gv, startColoum, endColoum);
Response.ClearContent();
Response.Clear();
Response.Charset = "UTF8";
Response.ContentEncoding = Encoding.UTF8;
if (Request.Browser.Browser.ToLower() != "firefox")
{
filename = HttpUtility.UrlEncode(Encoding.UTF8.GetBytes(filename));
}
Response.AddHeader("content-disposition", "attachment; filename=" + filename + ".xls"); Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
#region 解决数据第一个字符0导出被过滤掉的问题
string strStyle = "<style>td{mso-number-format:\"\\@\";}</style>";
sw.WriteLine(strStyle);
#endregion
HtmlTextWriter htw = new HtmlTextWriter(sw);
//gv.RenderControl(htw);
//Response.Output.Write("<meta http-equiv=\"content-type\" content=\"application/vnd.ms-excel; charset=utf-8\"/>" + sw.ToString());
if (gv.Rows.Count > 0)
{
gv.RenderControl(htw);
Response.Output.Write("<meta http-equiv=\"content-type\" content=\"application/vnd.ms-excel; charset=utf-8\"/>" + sw.ToString());
}
Response.Flush();
Response.End();
return Content("");
}