protected void btnExport_Click(object sender, EventArgs e)
{
MemoryStream stream = new MemoryStream();
this.OlapGrid1.Export(stream);
StreamReader streamReader = new System.IO.StreamReader(stream);
stream.Position = 0;
string data = streamReader.ReadToEnd();
Response.Clear();
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.Default;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
Response.AddHeader("Content-Disposition", "attachment; filename=Model.xls");
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
Response.AddHeader("Content-Length", data.Length.ToString());
// 指定返回的是一个不能被客户端读取的流,必须被下载
Response.ContentType = "application/ms-excel";
Response.Write("<style type='text/css'>td{text-align:left;border:solid 1px Black;}</style>");
Response.Write(AddExcelHead()); //显示网格线
Response.Write(data);//导出数据
Response.Write(AddExcelbottom());//显示网格线
Response.Flush();
Response.End();
}
public static string AddExcelHead()
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
sb.Append(" <head>");
sb.Append(" <!--[if gte mso 9]><xml>");
sb.Append("<x:ExcelWorkbook>");
sb.Append("<x:ExcelWorksheets>");
sb.Append("<x:ExcelWorksheet>");
sb.Append("<x:Name></x:Name>");
sb.Append("<x:WorksheetOptions>");
sb.Append("<x:Print>");
sb.Append("<x:ValidPrinterInfo />");
sb.Append(" </x:Print>");
sb.Append("</x:WorksheetOptions>");
sb.Append("</x:ExcelWorksheet>");
sb.Append("</x:ExcelWorksheets>");
sb.Append("</x:ExcelWorkbook>");
sb.Append("</xml>");
sb.Append("<![endif]-->");
sb.Append(" </head>");
sb.Append("<body>");
return sb.ToString();
}
public static string AddExcelbottom()
{
StringBuilder sb = new StringBuilder();
sb.Append("</body>");
sb.Append("</html>");
return sb.ToString();
}