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

推荐订阅源

Hugging Face - Blog
Hugging Face - Blog
V
Visual Studio Blog
Last Week in AI
Last Week in AI
Stack Overflow Blog
Stack Overflow Blog
The GitHub Blog
The GitHub Blog
Recent Announcements
Recent Announcements
博客园 - Franky
D
DataBreaches.Net
B
Blog
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
Microsoft Azure Blog
Microsoft Azure Blog
人人都是产品经理
人人都是产品经理
WordPress大学
WordPress大学
P
Proofpoint News Feed
J
Java Code Geeks
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Martin Fowler
Martin Fowler
月光博客
月光博客
宝玉的分享
宝玉的分享
Engineering at Meta
Engineering at Meta
阮一峰的网络日志
阮一峰的网络日志
F
Fortinet All Blogs
博客园 - 【当耐特】

博客园 - 沸石

快速压缩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
}