惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
云风的 BLOG
云风的 BLOG
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
D
Docker
H
Help Net Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Azure Blog
Microsoft Azure Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
月光博客
月光博客
C
Check Point Blog
S
SegmentFault 最新的问题
T
The Blog of Author Tim Ferriss
J
Java Code Geeks
M
MIT News - Artificial intelligence
B
Blog RSS Feed
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
美团技术团队
I
InfoQ
Blog — PlanetScale
Blog — PlanetScale

博客园 - 沸石

快速压缩log文件 SQL Server-拆分字符串返回表(存储过程) SQL Server-动态Pivot表的存储过程 突破AD查询1000条限制(转) 轻松创建运行命令 (转) (转)Resolving errors creating a Strong Name Key 以动词开头的习惯搭配:转 枚举的遍历和字符串转枚举,枚举转字符串 转 - 沸石 - 博客园 (转)英语信件中最常用精选句式 控件命名 转:C#委托及事件 转:关于HttpHandlers和HttpModules的不同 js:scrollLeft,scrollWidth,clientWidth,offsetWidth完全详解 商业书信英语-常用结束语 SQL中合并多行记录的方法总汇 C# 中的字符串函数及应用举例(转) 转:YetAnotherForum集成AD用户混合登录(一) AD验证类 - 沸石 - 博客园 英文函电书写基本原则 SQL Server 2005之PIVOT/UNPIVOT行列转换
ASHX 生成excel文件
沸石 · 2013-01-11 · via 博客园 - 沸石

private void GetCandidateExcelConverter(HttpContext context)
{
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;

// Workbook excelWorkbook = new Workbook();
string originalFile = context.Server.MapPath("~/webservice/report.xlsx");

Workbook excelWorkbook = new Workbook(originalFile);

filename = string.Format("report_{0}_{1:yyyyMMddHHmmss}.xls", context.Session["Name"], DateTime.Now);
LoadDataToCandidateExcel(context, excelWorkbook);

#region downlload excel
string exportFile = context.Server.MapPath("~/output/" + filename);
context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
HttpContext.Current.Response.Expires = 1;
HttpContext.Current.Response.ExpiresAbsolute = System.DateTime.Now.AddMinutes(-1);
HttpContext.Current.Response.CacheControl = "Private";
HttpContext.Current.Response.AddHeader("pragma", "no-cache");
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
HttpContext.Current.Response.Charset = String.Empty;
byte[] fileData = excelWorkbook.SaveToStream().ToArray();
context.Response.OutputStream.Write(fileData, 0, fileData.Length);
context.Response.End();
System.IO.MemoryStream ms = new System.IO.MemoryStream(fileData);
Int64 dataToRead = fileData.Length;
int length = 0;
try
{
while (dataToRead > 0)
{
if (HttpContext.Current.Response.IsClientConnected)
{
byte[] buffer = new byte[10000];
length = ms.Read(buffer, 0, 10000);
HttpContext.Current.Response.OutputStream.Write(buffer, 0, length);
HttpContext.Current.Response.Flush();
dataToRead = dataToRead - length;
}
else
{
dataToRead = 1;
}
}
}
finally
{
if (ms == null)
ms.Close();
}
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
#endregion
}