http://wiki.asp.net/page.aspx/401/export-to-csv-fi
First let’s see what the CSV file is:
CSV
file is a text based file in which data are separated by comma. It can
be opened by excel so you can use excel functionality. Each row of data
including the title is in separate line. Meanwhile, each row has data
separated by comma.
How to create a CSV file?
We
need to write data to the Response object. Meanwhile the content type
should be text/csv also a header type of attachment should be added to
response. Afterwards, column names are written to response then to
actual data is written (each row should be just in one line).Finally you
need to call Response.End to finalize your work.
Important: if any part of data
has comma unfortunately the CSV file will screw up. What I did in the
code is that I am replacing comma with space so I am sure that the only
comma exists in each row is just for separating data rather than being
part of data.
public class CSVExporter
{
public static void WriteToCSV(List<Person> personList)
{
string attachment = "attachment; filename=PersonList.csv";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.ContentType = "text/csv";
HttpContext.Current.Response.AddHeader("Pragma", "public");
WriteColumnName();
foreach (Person person in personList)
{
WriteUserInfo(person);
}
HttpContext.Current.Response.End();
}
private static void WriteUserInfo(Person person)
{
StringBuilder stringBuilder = new StringBuilder();
AddComma(person.Name, stringBuilder);
AddComma(person.Family, stringBuilder);
AddComma(person.Age.ToString(), stringBuilder);
AddComma(string.Format("{0:C2}", person.Salary), stringBuilder);
HttpContext.Current.Response.Write(stringBuilder.ToString());
HttpContext.Current.Response.Write(Environment.NewLine);
}
private static void AddComma(string value, StringBuilder stringBuilder)
{
stringBuilder.Append(value.Replace(',', ' '));
stringBuilder.Append(", ");
}
private static void WriteColumnName()
{
string columnNames = "Name, Family, Age, Salary";
HttpContext.Current.Response.Write(columnNames);
HttpContext.Current.Response.Write(Environment.NewLine);
}
}
分享到:
相关推荐
asp.net 导出 CSV 文件,有详细案例
一段简单的导出CSV文件格式的代码 希望对大家有用
详细说明、注释、源码、超级经典 详细说明、注释、源码、超级经典
asp.net 导出功能实现(xls,csv格式) 可以根据选择导出xls or csv格式。
Asp.net 导出Excel文件总结(导出csv、Office COM组建、Jet OLEDB、NPOI)
asp.net 导出 pdf 文件
asp.net 导出图片到excel .net导出excel
asp.net里导出excel表方法汇总asp.net里导出excel表方法汇总asp.net里导出excel表方法汇总asp.net里导出excel表方法汇总asp.net里导出excel表方法汇总
ASP.NET导出数据到ExcelASP.NET导出数据到ExcelASP.NET导出数据到ExcelASP.NET导出数据到ExcelASP.NET导出数据到Excel
在网上找了好多代码,要么就是不能指定位置,要么就是不能控制大小,现在我结合别人的方法修改了下,试用成功,可以在指定单元格导出指定大小的图片,也可以在中间显示原图,或者两种一起显示。
ASP.NET导出报表 Gridview数据导出Excel
asp.net 导入 CSV 文件 到数据库
asp.net 导出GridView里的数据到Excel中
asp.net导出EXCEL,把Excel文件当做一个数据源来进行数据的读取操作。
ASP.NET导出Excel控件(示例源码)ASP.NET导出Excel控件(示例源码)
使用asp.net导出word文档,看了之后相信会对你有所帮助的!
ASP.NET导出数据到Excel.rar
Asp.Net导出Excel和TxT文档
asp.net导出万能Excel和word
又四种从ASP.NET页面将数据导出到excel中的方法