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

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Apple Machine Learning Research
Apple Machine Learning Research
Last Week in AI
Last Week in AI
Blog — PlanetScale
Blog — PlanetScale
V
Visual Studio Blog
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
博客园 - Franky
IT之家
IT之家
博客园 - 叶小钗
Engineering at Meta
Engineering at Meta
The GitHub Blog
The GitHub Blog
雷峰网
雷峰网
腾讯CDC
博客园 - 聂微东
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
人人都是产品经理
人人都是产品经理
MongoDB | Blog
MongoDB | Blog
大猫的无限游戏
大猫的无限游戏
Martin Fowler
Martin Fowler
宝玉的分享
宝玉的分享
博客园_首页
G
Google Developers Blog

博客园 - Leon0812

Export Excel Jquery pluign development example XML Mail Template Management Resource file management Excel Reader iframe 中显示内嵌页的局部 - Leon0812 - 博客园 easyslider - Leon0812 - 博客园 缩放页面字体 可编辑DIV - Leon0812 - 博客园 vs快捷键大全(转) SQL文件執行 利用IHttpModule、IHttpHandler做授权类 - Leon0812 - 博客园 C#调用非托管的DLL 无缝滚动 - Leon0812 - 博客园 获取-编译-打包 VS作业一条龙批处理 Reporting Service for SQL 2008匿名访问报表方法 獲取控件位置 - Leon0812 - 博客园 Installscript中修改web.config - Leon0812 - 博客园 publish
Export csv
Leon0812 · 2011-12-09 · via 博客园 - Leon0812
public void CreateCSV(DataTable dt, string fileName, HttpContext context)
{

StringBuilder csvDoc = new StringBuilder();
string strTitle = string.Empty;
for (int i = 0; i <= dt.Columns.Count - 1; i++)
{
strTitle += "\"" + dt.Columns[i].ColumnName + "\"";
strTitle += ",";
}
strTitle.Substring(0,strTitle.Length-1);
csvDoc.AppendLine(strTitle);

string strValue = string.Empty;
foreach (DataRow dr in dt.Rows)
{
strValue = string.Empty;
for (int i = 0; i <= dt.Columns.Count - 1; i++)
{
strValue+=("\"" + dr[i].ToString().Replace("'", "''").Replace(",", "") + "\"");
strValue += (",");
}
strValue.Substring(0,strValue.Length - 1);
csvDoc.AppendLine(strValue);
}

byte[] strdata = System.Text.Encoding.Default.GetBytes(csvDoc.ToString());

context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
context.Response.BinaryWrite(strdata);
context.Response.Flush();
context.Response.End();

}