#region
导出excel
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="page">请求的页面this</param>
/// <param name="dataTable">导出的数据源</param>
/// <param name="fileName">保存文件名称</param>
/// <returns>布尔值</returns>
public bool ExportExcel(Page page, DataTable dataTable, string fileName)
{
try
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel.numberformat:@";
page.EnableViewState = false;
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");//设置输出流为简体中文
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
//输出列名
for (int i = 0; i < dataTable.Columns.Count; i++)
HttpContext.Current.Response.Write(dataTable.Columns[i].ColumnName + "t");
HttpContext.Current.Response.Write("rn");
//输出数据
for (int i = 0; i < dataTable.Rows.Count; i++)
{
for (int j = 0; j < dataTable.Columns.Count; j++)
{
HttpContext.Current.Response.Write(dataTable.Rows[i][j].ToString() + "t");
}
HttpContext.Current.Response.Write("rn");
}
//输出当前缓存内容
//HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
return true;
}
catch
{
return false;
}
}
#endregion