C# 读写csv文件的类
来源:网络来源TAG:读写csv浏览数: 次日期:2010-8-9评论:
条
折叠展开C# Code复制内容到剪贴板
- 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;
- }
- }
- }
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
我没有用过,连下载都没有,因为我用不着,也许某一天会有用。
昨天新闻点击排行
一周新闻点击排行
当月新闻点击排行
评论表单加载中...