#region 导出数据
private void DownLoadExcel(DataTable table,string fileName)
{
DataTable dt=table;
if(dt==null)
{
Jscript.Alert("对不起,没有要导出的数据");
return;
}
try
{
StringBuilder sb = new StringBuilder();
sb.Append("<table border=1 width=100%>");
for(int i=0;i<dt.Columns.Count;i++)
{
sb.Append("<td>").Append(dt.Columns[i].ColumnName).Append("</td>");
}
sb.Append("</tr>");
foreach(DataRow row in dt.Rows)
{
sb.Append("<tr>");
for(int i=0;i<dt.Columns.Count;i++)
{
sb.Append("<td>").Append(row[i].ToString()).Append("</td>");
}
sb.Append("</tr>");
}
sb.Append("</table>");
HttpResponse response = HttpContext.Current.Response;
//HttpContext.Current.Response.Charset ="UTF-8";
//HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
HttpContext.Current.Response.Charset ="GB2312";
HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.GetEncoding("GB2312");
response.AddHeader("base","target=_blank");
response.ContentType = "application/ms-Excel";
//设置导出报表的文件名
if(fileName.Trim()=="")
{
fileName= DateTime.Now.ToString(@"yyyyMMddhhmmss")+".xls";
}
else
{
fileName = HttpUtility.UrlEncode(fileName.Trim(),System.Text.Encoding.UTF8)+".xls";;
}
response.AppendHeader("Content-Disposition","inhert; filename=" + fileName);
response.Clear();
response.Write("<html><head><meta http-equiv=/"Content-Type/" content=/"text/html; charset=GB2312/" /></head><body>"+sb.ToString()+"</body></html>");
// response.Write(sb.ToString());
response.Flush();
response.End();
}
catch(Exception ex)
{
ILog log =LogManager.GetLogger(typeof(PagerControl));
log.Warn("导出数据生成Table表格并导出流时出错!",ex);
}
}
#endregion