最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
asp.net生成csv文件代码
时间:2011-01-10 编辑:简简单单 来源:一聚教程网
using system.web;
public class sdsavecsv
{
///
/// 构造函数
///
/// httpresponse对象
public sdsavecsv(httpresponse response)
{
this.response = response;
}
///
/// 输入出文件
///
/// 文件名称
/// 文件内容
///成功/失败
public bool outfile(string fullpath, string content)
{
bool retvalue = false;
try
{
this.response.buffer = true;
this.response.clear();
this.response.charset = system.text.encoding.default.bodyname;
this.response.contentencoding = system.text.encoding.getencoding("gb2312");//gb2312用excel打开时,没有乱码。
this.response.appendheader("content-disposition", "attachment;filename=" + fullpath);
this.response.contenttype = "application/ms-excel";
this.response.output.write(content);
this.response.flush();
this.response.end();
retvalue = true;
}
catch (exception ex)
{
retvalue = false;
throw new applicationexception(string.format("输出数据csv出错:{0}",fullpath), ex);
}
return retvalue;
}
}
//===============================以下为调用方法
stringbuilder sbfile = new stringbuilder();
sbfile.append("商户,蛋名称,蛋id,时间,累计领蛋人数,累计发蛋量");
sbfile.appendline();//换行
foreach (datarow row in ds.tables[0].rows)
{
sbfile.appendformat("{0},{1},{2},{3},{4},{5}", eggmerchants1.merchantname, row["eggname"], row["rowid"], row["date"], row["pep"], row["num"]);
sbfile.appendline();
}
new sdsavecsv(this.response).outfile(string.format("fetchegg({0}).csv",datetime.now), sbfile.tostring());
-
下一个: C# 类和对象 方法 语言规范
相关文章
- C#复制数组的两种方式及效率比较解读 10-24
- ASP.NET Identity用法解析 10-24
- ASP.NET MVC使用Identity增删改查用户介绍 10-24
- C语言中atoi函数模拟实现介绍 10-18
- .Net反向代理组件Yarp用法介绍 10-10
- .NET使用YARP通过编码方式配置域名转发实现反向代理教程 10-10