惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
L
LangChain Blog
H
Help Net Security
博客园_首页
T
Tailwind CSS Blog
Microsoft Security Blog
Microsoft Security Blog
T
The Blog of Author Tim Ferriss
雷峰网
雷峰网
Recent Announcements
Recent Announcements
D
DataBreaches.Net
U
Unit 42
Vercel News
Vercel News
I
InfoQ
Martin Fowler
Martin Fowler
Microsoft Azure Blog
Microsoft Azure Blog
Apple Machine Learning Research
Apple Machine Learning Research
S
SegmentFault 最新的问题
Jina AI
Jina AI
博客园 - 叶小钗
博客园 - 【当耐特】
罗磊的独立博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
Last Week in AI
Last Week in AI

博客园 - DODONG

在现有PDF文件上添加水印 c#获取硬件信息 C#操作Excel文件(读取Excel,写入Excel) . 解决全球化时区问题 利用ReportViewer读取Reporting Service数据 [转]用反射来处理多字段提交 查询数据表中重复的记录 CVS文件导入SQL ComponentArt.Web.UI中AJAX TreeView 抽象工厂(Abstract Factory)模式 简单工厂(Simple Factory)模式 工厂方法(Factory Method)模式 用.NET创建Windows服务 关于SharePoint中查询写法和注意的地方 关于&运算符和^ 初识WAP开发时.. C#操作XML XMLHttp客户端操作数据 asp.net网页智能导航SmartNavigation的替代实现方式
一个读写csv文件的C#类 .
DODONG · 2011-11-07 · via 博客园 - DODONG

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;
        }
        
    }
}

posted on 2011-11-07 17:49  DODONG  阅读(8668)  评论()    收藏  举报