黄金价格上升,24年来今天最高
作者: 王者之剑(http://www.albertsong.com/) 日期: 2008-01-29 22:24
百度搜索正式进军日本
作者: 王者之剑(http://www.albertsong.com/) 日期: 2008-01-23 23:55
Windows下开启Apache mod_rewrite模块完全解答
作者: 王者之剑(http://www.albertsong.com/) 日期: 2008-01-19 19:15
Windows下通过命令行安装和删除tomcat5服务的方法
作者: 王者之剑(http://www.albertsong.com/) 日期: 2008-01-18 13:35
MyEclipse 老是 out of memory的一个解决方法
作者: 王者之剑(http://www.albertsong.com/) 日期: 2008-01-15 09:50
一个读写csv文件的C#类
作者: 王者之剑(http://www.albertsong.com/) 日期: 2008-01-14 21:31
CSV(Comma-Separated Values )文件即用逗号分隔的文本文件。
下面是用C#写的一个简单的读写CSV文件的类。
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace CSVDemo
{
/// <summary>
/// CSVUtil is a helper class handling csv files.
/// </summary>
public class CSVUtil
{
private CSVUtil()
{
}
//write a new file, existed file will be overwritten
public static void WriteCSV(string filePathName,List<String[]>ls)
{
WriteCSV(filePathName,false,ls);
}
//write a file, existed file will be overwritten if append = false
public static void WriteCSV(string filePathName,bool append, List<String[]> ls)
{
StreamWriter fileWriter=new StreamWriter(filePathName,append,Encoding.Default);
foreach(String[] strArr in ls)
{
fileWriter.WriteLine(String.Join (“,",strArr) );
}
fileWriter.Flush();
fileWriter.Close();
}
public static List<String[]> ReadCSV(string filePathName)
{
List<String[]> ls = new List<String[]>();
StreamReader fileReader=new StreamReader(filePathName);
string strLine="";
while (strLine != null)
{
strLine = fileReader.ReadLine();
if (strLine != null && strLine.Length>0)
{
ls.Add(strLine.Split(','));
//Debug.WriteLine(strLine);
}
}
fileReader.Close();
return ls;
}
}
}
如何使用这个类可以看源代码。
CSVDemo.rar (12.76 KB , 下载:2308次)
源代码演示了:
1.listview控件,openFileDialog控件的简单运用;
2.autoseed的Random类的使用;
3.保存CSV文件;
4.读取CSV文件;
5.简单的分层思想,视图-listview,业务数据-data,永久数据-csv file
本代码不涉及:
1.listview控件的复杂控制
2.CSV文件内容合法性检验,例如每行是否有相同的列。
Update
2007-12-14 一个不简单的处理csv文件的C#类(库)
http://www.codeproject.com/KB/database/CsvReader.aspx
我没有用过,连下载都没有,因为我用不着,也许某一天会有用。
2008-1-14 按游客的指点修改了WriteCSV,精简了代码,Demo的代码也已更新。
下面是用C#写的一个简单的读写CSV文件的类。
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace CSVDemo
{
/// <summary>
/// CSVUtil is a helper class handling csv files.
/// </summary>
public class CSVUtil
{
private CSVUtil()
{
}
//write a new file, existed file will be overwritten
public static void WriteCSV(string filePathName,List<String[]>ls)
{
WriteCSV(filePathName,false,ls);
}
//write a file, existed file will be overwritten if append = false
public static void WriteCSV(string filePathName,bool append, List<String[]> ls)
{
StreamWriter fileWriter=new StreamWriter(filePathName,append,Encoding.Default);
foreach(String[] strArr in ls)
{
fileWriter.WriteLine(String.Join (“,",strArr) );
}
fileWriter.Flush();
fileWriter.Close();
}
public static List<String[]> ReadCSV(string filePathName)
{
List<String[]> ls = new List<String[]>();
StreamReader fileReader=new StreamReader(filePathName);
string strLine="";
while (strLine != null)
{
strLine = fileReader.ReadLine();
if (strLine != null && strLine.Length>0)
{
ls.Add(strLine.Split(','));
//Debug.WriteLine(strLine);
}
}
fileReader.Close();
return ls;
}
}
}
如何使用这个类可以看源代码。
CSVDemo.rar (12.76 KB , 下载:2308次)源代码演示了:
1.listview控件,openFileDialog控件的简单运用;
2.autoseed的Random类的使用;
3.保存CSV文件;
4.读取CSV文件;
5.简单的分层思想,视图-listview,业务数据-data,永久数据-csv file
本代码不涉及:
1.listview控件的复杂控制
2.CSV文件内容合法性检验,例如每行是否有相同的列。
Update
2007-12-14 一个不简单的处理csv文件的C#类(库)
http://www.codeproject.com/KB/database/CsvReader.aspx
我没有用过,连下载都没有,因为我用不着,也许某一天会有用。
2008-1-14 按游客的指点修改了WriteCSV,精简了代码,Demo的代码也已更新。
标签: CSV文件










