Skip to content

Instantly share code, notes, and snippets.

@johnsonz
Created September 12, 2016 06:46
Show Gist options
  • Save johnsonz/e8daf2f2b330cc1b2737cd536507c1eb to your computer and use it in GitHub Desktop.
Save johnsonz/e8daf2f2b330cc1b2737cd536507c1eb to your computer and use it in GitHub Desktop.
public static bool ExportExcel(Page page, string FileName, string SheetName, string ContentBody)
{
try
{
string content = "<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>" + SheetName + "</x:Name><x:WorksheetOptions><x:Print><x:ValidPrinterInfo /></x:Print></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body>" + ContentBody + "</body></html>";
page.Response.Buffer = true;
page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + FileName + DateTime.Now.ToString("yyyyMMdd") + ".xls");
page.Response.ContentEncoding = System.Text.Encoding.UTF8;
page.Response.ContentType = "application/vnd.ms-excel";
page.EnableViewState = false;
page.Response.Write(content);
page.Response.End();
return true;
}
catch
{
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment