欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實(shí)現(xiàn)讀寫CSV文件的方法詳解

 更新時(shí)間:2022年06月15日 08:08:09   作者:time-flies  
項(xiàng)目中經(jīng)常遇到CSV文件的讀寫需求,其中的難點(diǎn)主要是CSV文件的解析。本文會介紹CsvHelper、TextFieldParser、正則表達(dá)式三種解析CSV文件的方法,需要的可以參考一下

項(xiàng)目中經(jīng)常遇到CSV文件的讀寫需求,其中的難點(diǎn)主要是CSV文件的解析。本文會介紹CsvHelper、TextFieldParser、正則表達(dá)式三種解析CSV文件的方法,順帶也會介紹一下CSV文件的寫方法。

CSV文件標(biāo)準(zhǔn)

在介紹CSV文件的讀寫方法前,我們需要了解一下CSV文件的格式。

文件示例

一個(gè)簡單的CSV文件:

Test1,Test2,Test3,Test4,Test5,Test6
str1,str2,str3,str4,str5,str6
str1,str2,str3,str4,str5,str6

一個(gè)不簡單的CSV文件:

"Test1
"",""","Test2
"",""","Test3
"",""","Test4
"",""","Test5
"",""","Test6
"","""
" 中文,D23 ","3DFD4234""""""1232""1S2","ASD1"",""23,,,,213
23F32","
",,asd
" 中文,D23 ","3DFD4234""""""1232""1S2","ASD1"",""23,,,,213
23F32","
",,asd

你沒看錯(cuò),上面兩個(gè)都是CSV文件,都只有3行CSV數(shù)據(jù)。第二個(gè)文件多看一眼都是精神污染,但項(xiàng)目中無法避免會出現(xiàn)這種文件。

RFC 4180

CSV文件沒有官方的標(biāo)準(zhǔn),但一般項(xiàng)目都會遵守 RFC 4180 標(biāo)準(zhǔn)。這是一個(gè)非官方的標(biāo)準(zhǔn),內(nèi)容如下:

Each record is located on a separate line, delimited by a line break (CRLF).

The last record in the file may or may not have an ending line break.

There maybe an optional header line appearing as the first line of the file with the same format as normal record lines. This header will contain names corresponding to the fields in the file and should contain the same number of fields as the records in the rest of the file (the presence or absence of the header line should be indicated via the optional "header" parameter of this MIME type).

Within the header and each record, there may be one or more fields, separated by commas. Each line should contain the same number of fields throughout the file. Spaces are considered part of a field and should not be ignored. The last field in the record must not be followed by a comma.

Each field may or may not be enclosed in double quotes (however some programs, such as Microsoft Excel, do not use double quotes at all). If fields are not enclosed with double quotes, then double quotes may not appear inside the fields.

Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double-quotes.

If double-quotes are used to enclose fields, then a double-quote appearing inside a field must be escaped by preceding it with another double quote.

翻譯一下:

  • 每條記錄位于單獨(dú)的行上,由換行符 (CRLF) 分隔。
  • 文件中的最后一條記錄可能有也可能沒有結(jié)束換行符。
  • 可能有一個(gè)可選的標(biāo)題行出現(xiàn)在文件的第一行,格式與普通記錄行相同。此標(biāo)題將包含與文件中的字段對應(yīng)的名稱,并且應(yīng)包含與文件其余部分中的記錄相同數(shù)量的字段(標(biāo)題行的存在或不存在應(yīng)通過此 MIME 類型的可選“標(biāo)頭”參數(shù)指示)。
  • 在標(biāo)題和每條記錄中,可能有一個(gè)或多個(gè)字段,以逗號分隔。在整個(gè)文件中,每行應(yīng)包含相同數(shù)量的字段。空格被視為字段的一部分,不應(yīng)忽略。記錄中的最后一個(gè)字段后面不能有逗號。
  • 每個(gè)字段可以用雙引號括起來,也可以不用雙引號(但是某些程序,例如 Microsoft Excel,根本不使用雙引號)。如果字段沒有用雙引號括起來,那么雙引號可能不會出現(xiàn)在字段內(nèi)。
  • 包含換行符 (CRLF)、雙引號和逗號的字段應(yīng)該用雙引號括起來。
  • 如果使用雙引號將字段括起來,則出現(xiàn)在字段中的雙引號必須在其前面加上另一個(gè)雙引號。

簡化標(biāo)準(zhǔn)

上面的標(biāo)準(zhǔn)可能比較拗口,我們對它進(jìn)行一些簡化。要注意一下,簡化不是簡單的刪減規(guī)則,而是將類似的類似進(jìn)行合并便于理解。
后面的代碼也會使用簡化標(biāo)準(zhǔn),簡化標(biāo)準(zhǔn)如下:

  • 每條記錄位于單獨(dú)的行上,由換行符 (CRLF) 分隔。
  • 注:此處的行不是普通文本意義上的行,是指符合CSV文件格式的一條記錄(后面簡稱為CSV行),在文本上可能占據(jù)多行。
  • 文件中的最后一條記錄需有結(jié)束換行符,文件的第一行為標(biāo)題行(標(biāo)題行包含字段對應(yīng)的名稱,標(biāo)題數(shù)與記錄的字段數(shù)相同)。
  • 注:原標(biāo)準(zhǔn)中可有可無的選項(xiàng)統(tǒng)一規(guī)定為必須有,方便后期的解析,而且沒有標(biāo)題行讓別人怎么看數(shù)據(jù)。
  • 在標(biāo)題和每條記錄中,可能有一個(gè)或多個(gè)字段,以逗號分隔。在整個(gè)文件中,每行應(yīng)包含相同數(shù)量的字段空格被視為字段的一部分,不應(yīng)忽略。記錄中的最后一個(gè)字段后面不能有逗號。
  • 注:此標(biāo)準(zhǔn)未做簡化,雖然也有其它標(biāo)準(zhǔn)使用空格、制表符等做分割的,但不使用逗號分割的文件還叫逗號分隔值文件嗎。
  • 每個(gè)字段都用雙引號括起來,出現(xiàn)在字段中的雙引號必須在其前面加上另一個(gè)雙引號
  • 注:原標(biāo)準(zhǔn)有必須使用雙引號和可選雙引號的情況,那全部使用雙引號肯定不會出錯(cuò)。*

讀寫CSV文件

在正式讀寫CSV文件前,我們需要先定義一個(gè)用于測試的Test類。代碼如下:

class Test
{
    public string Test1{get;set;}
    public string Test2 { get; set; }
    public string Test3 { get; set; }
    public string Test4 { get; set; }
    public string Test5 { get; set; }
    public string Test6 { get; set; }

    //Parse方法會在自定義讀寫CSV文件時(shí)用到
    public static Test Parse (string[]fields )
    {
        try
        {
            Test ret = new Test();
            ret.Test1 = fields[0];
            ret.Test2 = fields[1];
            ret.Test3 = fields[2];
            ret.Test4 = fields[3];
            ret.Test5 = fields[4];
            ret.Test6 = fields[5];
            return ret;
        }
        catch (Exception)
        {
            //做一些異常處理,寫日志之類的
            return null;
        }
    }
}

生成一些測試數(shù)據(jù),代碼如下:

static void Main(string[] args)
{
    //文件保存路徑
    string path = "tset.csv";
    //清理之前的測試文件
    File.Delete("tset.csv");
      
    Test test = new Test();
    test.Test1 = " 中文,D23 ";
    test.Test2 = "3DFD4234\"\"\"1232\"1S2";
    test.Test3 = "ASD1\",\"23,,,,213\r23F32";
    test.Test4 = "\r";
    test.Test5 = string.Empty;
    test.Test6 = "asd";

    //測試數(shù)據(jù)
    var records = new List<Test> { test, test };

    //寫CSV文件
    /*
    *直接把后面的寫CSV文件代碼復(fù)制到此處
    */

    //讀CSV文件
     /*
    *直接把后面的讀CSV文件代碼復(fù)制到此處
    */
   
    Console.ReadLine();
}

使用CsvHelper

CsvHelper 是用于讀取和寫入 CSV 文件的庫,支持自定義類對象的讀寫。

github上標(biāo)星最高的CSV文件讀寫C#庫,使用MS-PL、Apache 2.0開源協(xié)議。

使用NuGet下載CsvHelper,讀寫CSV文件的代碼如下:

 //寫CSV文件
using (var writer = new StreamWriter(path))
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
    csv.WriteRecords(records);
}

using (var writer = new StreamWriter(path,true))
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
    //追加
    foreach (var record in records)
    {
        csv.WriteRecord(record);
    }
}

//讀CSV文件
using (var reader = new StreamReader(path))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
    records = csv.GetRecords<Test>().ToList();
    //逐行讀取
    //records.Add(csv.GetRecord<Test>());
}

如果你只想要拿來就能用的庫,那文章基本上到這里就結(jié)束了。

使用自定義方法

為了與CsvHelper區(qū)分,新建一個(gè)CsvFile類存放自定義讀寫CSV文件的代碼,最后會提供類的完整源碼。CsvFile類定義如下:

/// <summary>
/// CSV文件讀寫工具類
/// </summary>
public class CsvFile
{
    #region 寫CSV文件
    //具體代碼...
    #endregion

    #region 讀CSV文件(使用TextFieldParser)
    //具體代碼...
    #endregion

    #region 讀CSV文件(使用正則表達(dá)式)
    //具體代碼...
    #endregion

}

基于簡化標(biāo)準(zhǔn)的寫CSV文件

根據(jù)簡化標(biāo)準(zhǔn)(具體標(biāo)準(zhǔn)內(nèi)容見前文),寫CSV文件代碼如下:

#region 寫CSV文件
//字段數(shù)組轉(zhuǎn)為CSV記錄行
private static string FieldsToLine(IEnumerable<string> fields)
{
    if (fields == null) return string.Empty;
    fields = fields.Select(field =>
    {
        if (field == null) field = string.Empty;
        //簡化標(biāo)準(zhǔn),所有字段都加雙引號
        field = string.Format("\"{0}\"", field.Replace("\"", "\"\""));

        //不簡化標(biāo)準(zhǔn)
        //field = field.Replace("\"", "\"\"");
        //if (field.IndexOfAny(new char[] { ',', '"', ' ', '\r' }) != -1)
        //{
        //    field = string.Format("\"{0}\"", field);
        //}
        return field;
    });
    string line = string.Format("{0}{1}", string.Join(",", fields), Environment.NewLine);
    return line;
}

//默認(rèn)的字段轉(zhuǎn)換方法
private static IEnumerable<string> GetObjFields<T>(T obj, bool isTitle) where T : class
{
    IEnumerable<string> fields;
    if (isTitle)
    {
        fields = obj.GetType().GetProperties().Select(pro => pro.Name);
    }
    else
    {
        fields = obj.GetType().GetProperties().Select(pro => pro.GetValue(obj)?.ToString());
    }
    return fields;
}

/// <summary>
/// 寫CSV文件,默認(rèn)第一行為標(biāo)題
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list">數(shù)據(jù)列表</param>
/// <param name="path">文件路徑</param>
/// <param name="append">追加記錄</param>
/// <param name="func">字段轉(zhuǎn)換方法</param>
/// <param name="defaultEncoding"></param>
public static void Write<T>(List<T> list, string path,bool append=true, Func<T, bool, IEnumerable<string>> func = null, Encoding defaultEncoding = null) where T : class
{
    if (list == null || list.Count == 0) return;
    if (defaultEncoding == null)
    {
        defaultEncoding = Encoding.UTF8;
    }
    if (func == null)
    {
        func = GetObjFields;
    }
    if (!File.Exists(path)|| !append)
    {
        var fields = func(list[0], true);
        string title = FieldsToLine(fields);
        File.WriteAllText(path, title, defaultEncoding);
    }
    using (StreamWriter sw = new StreamWriter(path, true, defaultEncoding))
    {
        list.ForEach(obj =>
        {
            var fields = func(obj, false);
            string line = FieldsToLine(fields);
            sw.Write(line);
        });
    }
}
#endregion

使用時(shí),代碼如下:

//寫CSV文件
//使用自定義的字段轉(zhuǎn)換方法,也是文章開頭復(fù)雜CSV文件使用字段轉(zhuǎn)換方法
CsvFile.Write(records, path, true, new Func<Test, bool, IEnumerable<string>>((obj, isTitle) =>
{
    IEnumerable<string> fields;
    if (isTitle)
    {
        fields = obj.GetType().GetProperties().Select(pro => pro.Name + Environment.NewLine + "\",\"");
    }
    else
    {
        fields = obj.GetType().GetProperties().Select(pro => pro.GetValue(obj)?.ToString());
    }
    return fields;
}));

//使用默認(rèn)的字段轉(zhuǎn)換方法
//CsvFile.Write(records, path);

你也可以使用默認(rèn)的字段轉(zhuǎn)換方法,代碼如下:

CsvFile.Save(records, path);

使用TextFieldParser解析CSV文件

TextFieldParser是VB中解析CSV文件的類,C#雖然沒有類似功能的類,不過可以調(diào)用VB的TextFieldParser來實(shí)現(xiàn)功能。

TextFieldParser解析CSV文件的代碼如下:

#region 讀CSV文件(使用TextFieldParser)
/// <summary>
/// 讀CSV文件,默認(rèn)第一行為標(biāo)題
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="path">文件路徑</param>
/// <param name="func">字段解析規(guī)則</param>
/// <param name="defaultEncoding">文件編碼</param>
/// <returns></returns>
public static List<T> Read<T>(string path, Func<string[], T> func, Encoding defaultEncoding = null) where T : class
{
    if (defaultEncoding == null)
    {
        defaultEncoding = Encoding.UTF8;
    }
    List<T> list = new List<T>();
    using (TextFieldParser parser = new TextFieldParser(path, defaultEncoding))
    {
        parser.TextFieldType = FieldType.Delimited;
        //設(shè)定逗號分隔符
        parser.SetDelimiters(",");
        //設(shè)定不忽略字段前后的空格
        parser.TrimWhiteSpace = false;
        bool isLine = false;
        while (!parser.EndOfData)
        {
            string[] fields = parser.ReadFields();
            if (isLine)
            {
                var obj = func(fields);
                if (obj != null) list.Add(obj);
            }
            else
            {
                //忽略標(biāo)題行業(yè)
                isLine = true;
            }
        }
    }
    return list;
}
#endregion

使用時(shí),代碼如下:

//讀CSV文件
records = CsvFile.Read(path, Test.Parse);

使用正則表達(dá)式解析CSV文件

如果你有一個(gè)問題,想用正則表達(dá)式來解決,那么你就有兩個(gè)問題了。

正則表達(dá)式有一定的學(xué)習(xí)門檻,而且學(xué)習(xí)后不經(jīng)常使用就會忘記。正則表達(dá)式解決的大多數(shù)是一些不易變更需求的問題,這就導(dǎo)致一個(gè)穩(wěn)定可用的正則表達(dá)式可以傳好幾代。

本節(jié)的正則表達(dá)式來自 《精通正則表達(dá)式(第3版)》 第6章 打造高效正則表達(dá)式——簡單的消除循環(huán)的例子,有興趣的可以去了解一下,表達(dá)式說明如下:

注:這本書最終版的解析CSV文件的正則表達(dá)式是Jave版的使用占有優(yōu)先量詞取代固化分組的版本,也是百度上經(jīng)常見到的版本。不過占有優(yōu)先量詞在C#中有點(diǎn)問題,本人能力有限解決不了,所以使用了上圖的版本。不過,這兩版正則表達(dá)式性能上沒有差異。

正則表達(dá)式解析CSV文件代碼如下:

#region 讀CSV文件(使用正則表達(dá)式)
/// <summary>
/// 讀CSV文件,默認(rèn)第一行為標(biāo)題
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="path">文件路徑</param>
/// <param name="func">字段解析規(guī)則</param>
/// <param name="defaultEncoding">文件編碼</param>
/// <returns></returns>
public static List<T> Read_Regex<T>(string path, Func<string[], T> func, Encoding defaultEncoding = null) where T : class
{
    List<T> list = new List<T>();
    StringBuilder sbr = new StringBuilder(100);
    Regex lineReg = new Regex("\"");
    Regex fieldReg = new Regex("\\G(?:^|,)(?:\"((?>[^\"]*)(?>\"\"[^\"]*)*)\"|([^\",]*))");
    Regex quotesReg = new Regex("\"\"");

    bool isLine = false;
    string line = string.Empty;
    using (StreamReader sr = new StreamReader(path))
    {
        while (null != (line = ReadLine(sr)))
        {
            sbr.Append(line);
            string str = sbr.ToString();
            //一個(gè)完整的CSV記錄行,它的雙引號一定是偶數(shù)
            if (lineReg.Matches(sbr.ToString()).Count % 2 == 0)
            {
                if (isLine)
                {
                    var fields = ParseCsvLine(sbr.ToString(), fieldReg, quotesReg).ToArray();
                    var obj = func(fields.ToArray());
                    if (obj != null) list.Add(obj);
                }
                else
                {
                    //忽略標(biāo)題行業(yè)
                    isLine = true;
                }
                sbr.Clear();
            }
            else
            {
                sbr.Append(Environment.NewLine);
            }                   
        }
    }
    if (sbr.Length > 0)
    {
        //有解析失敗的字符串,報(bào)錯(cuò)或忽略
    }
    return list;
}

//重寫ReadLine方法,只有\(zhòng)r\n才是正確的一行
private static string ReadLine(StreamReader sr) 
{
    StringBuilder sbr = new StringBuilder();
    char c;
    int cInt;
    while (-1 != (cInt =sr.Read()))
    {
        c = (char)cInt;
        if (c == '\n' && sbr.Length > 0 && sbr[sbr.Length - 1] == '\r')
        {
            sbr.Remove(sbr.Length - 1, 1);
            return sbr.ToString();
        }
        else 
        {
            sbr.Append(c);
        }
    }
    return sbr.Length>0?sbr.ToString():null;
}

private static List<string> ParseCsvLine(string line, Regex fieldReg, Regex quotesReg)
{
    var fieldMath = fieldReg.Match(line);
    List<string> fields = new List<string>();
    while (fieldMath.Success)
    {
        string field;
        if (fieldMath.Groups[1].Success)
        {
            field = quotesReg.Replace(fieldMath.Groups[1].Value, "\"");
        }
        else
        {
            field = fieldMath.Groups[2].Value;
        }
        fields.Add(field);
        fieldMath = fieldMath.NextMatch();
    }
    return fields;
}
#endregion

使用時(shí)代碼如下:

//讀CSV文件
records = CsvFile.Read_Regex(path, Test.Parse);

目前還未發(fā)現(xiàn)正則表達(dá)式解析有什么bug,不過還是不建議使用。

完整的CsvFile工具類

完整的CsvFile類代碼如下:

using Microsoft.VisualBasic.FileIO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;


namespace ConsoleApp4
{
    /// <summary>
    /// CSV文件讀寫工具類
    /// </summary>
    public class CsvFile
    {
        #region 寫CSV文件
        //字段數(shù)組轉(zhuǎn)為CSV記錄行
        private static string FieldsToLine(IEnumerable<string> fields)
        {
            if (fields == null) return string.Empty;
            fields = fields.Select(field =>
            {
                if (field == null) field = string.Empty;
                //所有字段都加雙引號
                field = string.Format("\"{0}\"", field.Replace("\"", "\"\""));

                //不簡化
                //field = field.Replace("\"", "\"\"");
                //if (field.IndexOfAny(new char[] { ',', '"', ' ', '\r' }) != -1)
                //{
                //    field = string.Format("\"{0}\"", field);
                //}
                return field;
            });
            string line = string.Format("{0}{1}", string.Join(",", fields), Environment.NewLine);
            return line;
        }

        //默認(rèn)的字段轉(zhuǎn)換方法
        private static IEnumerable<string> GetObjFields<T>(T obj, bool isTitle) where T : class
        {
            IEnumerable<string> fields;
            if (isTitle)
            {
                fields = obj.GetType().GetProperties().Select(pro => pro.Name);
            }
            else
            {
                fields = obj.GetType().GetProperties().Select(pro => pro.GetValue(obj)?.ToString());
            }
            return fields;
        }

        /// <summary>
        /// 寫CSV文件,默認(rèn)第一行為標(biāo)題
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list">數(shù)據(jù)列表</param>
        /// <param name="path">文件路徑</param>
        /// <param name="append">追加記錄</param>
        /// <param name="func">字段轉(zhuǎn)換方法</param>
        /// <param name="defaultEncoding"></param>
        public static void Write<T>(List<T> list, string path,bool append=true, Func<T, bool, IEnumerable<string>> func = null, Encoding defaultEncoding = null) where T : class
        {
            if (list == null || list.Count == 0) return;
            if (defaultEncoding == null)
            {
                defaultEncoding = Encoding.UTF8;
            }
            if (func == null)
            {
                func = GetObjFields;
            }
            if (!File.Exists(path)|| !append)
            {
                var fields = func(list[0], true);
                string title = FieldsToLine(fields);
                File.WriteAllText(path, title, defaultEncoding);
            }
            using (StreamWriter sw = new StreamWriter(path, true, defaultEncoding))
            {
                list.ForEach(obj =>
                {
                    var fields = func(obj, false);
                    string line = FieldsToLine(fields);
                    sw.Write(line);
                });
            }
        }
        #endregion

        #region 讀CSV文件(使用TextFieldParser)
        /// <summary>
        /// 讀CSV文件,默認(rèn)第一行為標(biāo)題
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path">文件路徑</param>
        /// <param name="func">字段解析規(guī)則</param>
        /// <param name="defaultEncoding">文件編碼</param>
        /// <returns></returns>
        public static List<T> Read<T>(string path, Func<string[], T> func, Encoding defaultEncoding = null) where T : class
        {
            if (defaultEncoding == null)
            {
                defaultEncoding = Encoding.UTF8;
            }
            List<T> list = new List<T>();
            using (TextFieldParser parser = new TextFieldParser(path, defaultEncoding))
            {
                parser.TextFieldType = FieldType.Delimited;
                //設(shè)定逗號分隔符
                parser.SetDelimiters(",");
                //設(shè)定不忽略字段前后的空格
                parser.TrimWhiteSpace = false;
                bool isLine = false;
                while (!parser.EndOfData)
                {
                    string[] fields = parser.ReadFields();
                    if (isLine)
                    {
                        var obj = func(fields);
                        if (obj != null) list.Add(obj);
                    }
                    else
                    {
                        //忽略標(biāo)題行業(yè)
                        isLine = true;
                    }
                }
            }
            return list;
        }
        #endregion

        #region 讀CSV文件(使用正則表達(dá)式)
        /// <summary>
        /// 讀CSV文件,默認(rèn)第一行為標(biāo)題
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path">文件路徑</param>
        /// <param name="func">字段解析規(guī)則</param>
        /// <param name="defaultEncoding">文件編碼</param>
        /// <returns></returns>
        public static List<T> Read_Regex<T>(string path, Func<string[], T> func, Encoding defaultEncoding = null) where T : class
        {
            List<T> list = new List<T>();
            StringBuilder sbr = new StringBuilder(100);
            Regex lineReg = new Regex("\"");
            Regex fieldReg = new Regex("\\G(?:^|,)(?:\"((?>[^\"]*)(?>\"\"[^\"]*)*)\"|([^\",]*))");
            Regex quotesReg = new Regex("\"\"");

            bool isLine = false;
            string line = string.Empty;
            using (StreamReader sr = new StreamReader(path))
            {
                while (null != (line = ReadLine(sr)))
                {
                    sbr.Append(line);
                    string str = sbr.ToString();
                    //一個(gè)完整的CSV記錄行,它的雙引號一定是偶數(shù)
                    if (lineReg.Matches(sbr.ToString()).Count % 2 == 0)
                    {
                        if (isLine)
                        {
                            var fields = ParseCsvLine(sbr.ToString(), fieldReg, quotesReg).ToArray();
                            var obj = func(fields.ToArray());
                            if (obj != null) list.Add(obj);
                        }
                        else
                        {
                            //忽略標(biāo)題行業(yè)
                            isLine = true;
                        }
                        sbr.Clear();
                    }
                    else
                    {
                        sbr.Append(Environment.NewLine);
                    }                   
                }
            }
            if (sbr.Length > 0)
            {
                //有解析失敗的字符串,報(bào)錯(cuò)或忽略
            }
            return list;
        }

        //重寫ReadLine方法,只有\(zhòng)r\n才是正確的一行
        private static string ReadLine(StreamReader sr) 
        {
            StringBuilder sbr = new StringBuilder();
            char c;
            int cInt;
            while (-1 != (cInt =sr.Read()))
            {
                c = (char)cInt;
                if (c == '\n' && sbr.Length > 0 && sbr[sbr.Length - 1] == '\r')
                {
                    sbr.Remove(sbr.Length - 1, 1);
                    return sbr.ToString();
                }
                else 
                {
                    sbr.Append(c);
                }
            }
            return sbr.Length>0?sbr.ToString():null;
        }
       
        private static List<string> ParseCsvLine(string line, Regex fieldReg, Regex quotesReg)
        {
            var fieldMath = fieldReg.Match(line);
            List<string> fields = new List<string>();
            while (fieldMath.Success)
            {
                string field;
                if (fieldMath.Groups[1].Success)
                {
                    field = quotesReg.Replace(fieldMath.Groups[1].Value, "\"");
                }
                else
                {
                    field = fieldMath.Groups[2].Value;
                }
                fields.Add(field);
                fieldMath = fieldMath.NextMatch();
            }
            return fields;
        }
        #endregion

    }
}

使用方法如下:

//寫CSV文件
CsvFile.Write(records, path, true, new Func<Test, bool, IEnumerable<string>>((obj, isTitle) =>
{
    IEnumerable<string> fields;
    if (isTitle)
    {
        fields = obj.GetType().GetProperties().Select(pro => pro.Name + Environment.NewLine + "\",\"");
    }
    else
    {
        fields = obj.GetType().GetProperties().Select(pro => pro.GetValue(obj)?.ToString());
    }
    return fields;
}));

//讀CSV文件
records = CsvFile.Read(path, Test.Parse);

//讀CSV文件
records = CsvFile.Read_Regex(path, Test.Parse);

總結(jié)

  • 介紹了CSV文件的 RFC 4180 標(biāo)準(zhǔn)及其簡化理解版本
  • 介紹了CsvHelper、TextFieldParser、正則表達(dá)式三種解析CSV文件的方法
  • 項(xiàng)目中推薦使用CsvHelper,如果不想引入太多開源組件可以使用TextFieldParser,不建議使用正則表達(dá)式

以上就是C#實(shí)現(xiàn)讀寫CSV文件的方法詳解的詳細(xì)內(nèi)容,更多關(guān)于C#讀寫CSV文件的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C#儀器數(shù)據(jù)文件解析Excel文件的方法淺析(xls、xlsx)

    C#儀器數(shù)據(jù)文件解析Excel文件的方法淺析(xls、xlsx)

    這篇文章主要給大家介紹了關(guān)于C#儀器數(shù)據(jù)文件如何解析Excel文件的方法,包括解析xls、xlsx兩種格式,文中介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-10-10
  • C#實(shí)現(xiàn)簡單學(xué)生信息管理系統(tǒng)

    C#實(shí)現(xiàn)簡單學(xué)生信息管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)簡單學(xué)生信息管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • C#多線程學(xué)習(xí)之(五)使用定時(shí)器進(jìn)行多線程的自動管理

    C#多線程學(xué)習(xí)之(五)使用定時(shí)器進(jìn)行多線程的自動管理

    這篇文章主要介紹了C#多線程學(xué)習(xí)之使用定時(shí)器進(jìn)行多線程的自動管理,實(shí)例分析了C#使用timer定時(shí)器類實(shí)現(xiàn)針對多線程的自動管理功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-04-04
  • C#編程實(shí)現(xiàn)連接ACCESS數(shù)據(jù)庫實(shí)例詳解

    C#編程實(shí)現(xiàn)連接ACCESS數(shù)據(jù)庫實(shí)例詳解

    這篇文章主要介紹了C#編程實(shí)現(xiàn)連接ACCESS數(shù)據(jù)庫的方法,以實(shí)例形式較為詳細(xì)的分析了C#連接access數(shù)據(jù)庫的具體步驟與相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-11-11
  • C# winform分頁查詢的實(shí)現(xiàn)示例

    C# winform分頁查詢的實(shí)現(xiàn)示例

    這篇文章主要介紹了C# winform分頁查詢的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • 解決WCF不能直接序列化SqlParameter類型的問題

    解決WCF不能直接序列化SqlParameter類型的問題

    這篇文章介紹了解決WCF不能直接序列化SqlParameter類型的問題,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • C# 正則表達(dá)式常用的符號和模式解析(最新推薦)

    C# 正則表達(dá)式常用的符號和模式解析(最新推薦)

    這篇文章主要介紹了C# 正則表達(dá)式常用的符號和模式解析,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-12-12
  • C#獲取進(jìn)程或線程相關(guān)信息的方法

    C#獲取進(jìn)程或線程相關(guān)信息的方法

    這篇文章主要介紹了C#獲取進(jìn)程或線程相關(guān)信息的方法,涉及C#操作進(jìn)程及線程的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-08-08
  • C#迭代器模式(Iterator Pattern)實(shí)例教程

    C#迭代器模式(Iterator Pattern)實(shí)例教程

    這篇文章主要介紹了C#迭代器模式(Iterator Pattern),包括了迭代器的適用范圍及用法實(shí)例,需要的朋友可以參考下
    2014-09-09
  • C#調(diào)用Python腳本程序的兩種方法

    C#調(diào)用Python腳本程序的兩種方法

    本文主要介紹了C#調(diào)用Python腳本程序的兩種方法,包含介紹了通過C#IronPython開源庫和通過Process類來運(yùn)行python解釋器這兩種,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-02-02

最新評論