一个读写csv文件的C#类(转)
2013-03-27 13:22
567 查看
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;
}
}
}
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;
}
}
}
相关文章推荐
- 一个读写csv文件的C#类
- 一个读写csv文件的C#类 .
- 一个读写csv文件的C#类
- C#读写CSV文件
- 一个读写csv文件的C#类
- 【C#】CSV文件读写
- 问题:C#打开一个文本文档往里面写数据,没有就新建文档 ;结果:c#FileStream文件读写(转)
- C# 多个CSV文件合并成一个文件
- C# CSV文件读写
- c# 实现2个程序同时操作读写一个文件、一个读一个写
- 一个用C#编写的自动读写配置文件的开源组件
- C# CSV文件读写
- C#操作文件(文件读写),这次示例操作txt文件,其他格式一个原理
- C#对 CSV文件读写
- C# CSV文件读写
- C#技巧【调用线程无法访问此对象,因为另一个线程拥有该对象的问题的解决办法】【C#读写EXCEL源码提示“office检测到此文件存在一个问题。为帮助保护您的计算机,不能打开此文件”的解决】
- 一个用C#编写的自动读写配置文件的开源组件--V1.0.2版本
- C#对.CSV格式的文件--逗号分隔值文件 的读写操作及上传ftp服务器操作方法总结
- 支持各种特殊字符的 CSV 解析类 (.net 实现)(C#读写CSV文件)
- C# ArrayList用BinaryFormatter序列化和反序列化进行文件读写的一个简单例子