Explicación detallada de cómo leer y escribir archivos CSV en C#

Tabla de contenido
  • Estándar de archivo CSV
    • Ejemplo de archivo
    • RFC 4180
    • Estándares de simplificación
  • Leer y escribir archivos CSV
    • Usando CsvHelper
    • Usar método personalizado
  • Resumir

En los proyectos, a menudo nos encontramos con la necesidad de leer y escribir archivos CSV, la principal dificultad es el análisis de los archivos CSV. Este artículo presentará tres métodos para analizar archivos CSV: CsvHelper , TextFieldParser y expresiones regulares . Por cierto, también presentará cómo escribir archivos CSV.

Estándar de archivo CSV

Antes de presentar los métodos de lectura y escritura de archivos CSV, debemos comprender el formato de los archivos CSV.

Ejemplo de archivo

Un archivo CSV simple:

?

1

2

3

Test1,Test2,Test3,Test4,Test5,Test6

str1,str2,str3,str4,str5,str6

str1,str2,str3,str4,str5,str6

Un archivo CSV no tan simple:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

"Test1

"",""","Test2

"",""","Test3

"",""","Test4

"",""","Test5

"",""","Test6

"","""

" 中文,D23 ","3DFD4234""""""1232""1S2","ASD1"",""23,,,,213

23F32","

",,asd

" 中文,D23 ","3DFD4234""""""1232""1S2","ASD1"",""23,,,,213

23F32","

",,asd

Leíste bien, los dos anteriores son archivos CSV y ambos tienen solo 3 líneas de datos CSV. Mirar el segundo archivo dos veces es contaminación mental, pero es inevitable que dichos archivos aparezcan en el proyecto.

RFC 4180

No existe un estándar oficial para archivos CSV, pero los proyectos generales cumplirán con el estándar RFC 4180. Este es un estándar no oficial y el contenido es el siguiente:

Cada registro se ubica en una línea separada, delimitada por un salto de línea (CRLF).

El último registro del archivo puede tener o no un salto de línea final.

Es posible que aparezca una línea de encabezado opcional como la primera línea del archivo con el mismo formato que las líneas de registro normales. Este encabezado contendrá los nombres correspondientes a los campos del archivo y debe contener el mismo número de campos que los registros del resto del archivo (la presencia o ausencia de la línea del encabezado debe indicarse mediante el parámetro opcional "encabezado" de este Tipo de Mimica).

Dentro del encabezado y de cada registro, puede haber uno o más campos, separados por comas. Cada línea debe contener la misma cantidad de campos en todo el archivo. Los espacios se consideran parte de un campo y no deben ignorarse. El último campo del registro no debe ir seguido de una coma.

Cada campo puede o no estar entre comillas dobles (sin embargo, algunos programas, como Microsoft Excel, no utilizan comillas dobles en absoluto). Si los campos no están entre comillas dobles, es posible que las comillas dobles no aparezcan dentro de los campos.

Los campos que contienen saltos de línea (CRLF), comillas dobles y comas deben estar entre comillas dobles.

Si se utilizan comillas dobles para encerrar campos, entonces las comillas dobles que aparecen dentro de un campo se deben evitar precediéndolas con otra comilla doble.

traducir:

  • Cada registro está en una línea separada, separada por un carácter de nueva línea (CRLF).
  • El último registro del archivo puede tener o no un carácter de nueva línea final.
  • Puede haber una línea de encabezado opcional que aparece como la primera línea del archivo, en el mismo formato que una línea de registro normal. Este encabezado contendrá nombres que corresponden a los campos del archivo y debe contener la misma cantidad de campos que los registros del resto del archivo (la presencia o ausencia de una fila de encabezado debe indicarse mediante el parámetro opcional "encabezado" de este tipo MIME).
  • En el encabezado y en cada registro puede haber uno o más campos, separados por comas. En todo el archivo, cada línea debe contener la misma cantidad de campos. Los espacios en blanco se consideran parte del campo y no deben ignorarse. No puede haber una coma después del último campo del registro.
  • Cada campo puede estar entre comillas dobles o no (pero algunos programas, como Microsoft Excel, no usan comillas dobles en absoluto). Si el campo no está entre comillas dobles, es posible que las comillas dobles no aparezcan dentro del campo.
  • Los campos que contienen nuevas líneas (CRLF), comillas dobles y comas deben estar entre comillas dobles.
  • Si utiliza comillas dobles para encerrar un campo, la comilla doble que aparece en el campo debe ir precedida por otra comilla doble.

Estándares de simplificación

El estándar anterior puede resultar un poco difícil de pronunciar, por lo que lo simplificaremos un poco. Cabe señalar que la simplificación no consiste simplemente en eliminar reglas, sino en fusionar otras similares para facilitar su comprensión.
El siguiente código también utilizará los criterios de simplificación, que son los siguientes:

  • Cada registro está en una línea separada, separada por un carácter de nueva línea (CRLF).
  • Nota: La línea aquí no es una línea en el sentido normal del texto, sino que se refiere a un registro que se ajusta al formato de archivo CSV (en adelante, línea CSV ) , que puede ocupar varias líneas en el texto.
  • El último registro del archivo debe tener un carácter de nueva línea final y la primera línea del archivo debe ser una fila de encabezado (la fila de encabezado contiene los nombres correspondientes a los campos y el número de encabezados es el mismo que el número de campos). en el expediente).
  • Nota: Las opciones opcionales en el estándar original se especifican uniformemente como obligatorias para facilitar el análisis posterior, y no hay una línea de encabezado que permita a otros ver los datos.
  • En el encabezado y en cada registro puede haber uno o más campos , separados por comas. En todo el archivo, cada línea debe contener la misma cantidad de campos . Los espacios en blanco se consideran parte del campo y no deben ignorarse . No puede haber una coma después del último campo del registro .
  • Nota: Este estándar no se ha simplificado. Aunque existen otros estándares que utilizan espacios, tabulaciones, etc. para la separación, ¿los archivos que no utilizan comas todavía se denominan archivos de valores separados por comas?
  • Cada campo está entre comillas dobles y una comilla doble que aparece en un campo debe estar precedida por otra comilla doble.
  • Nota: El estándar original tiene situaciones en las que se deben usar comillas dobles y se usan comillas dobles opcionales, por lo que definitivamente no saldrá mal si se usan todas las comillas dobles. *

Leer y escribir archivos CSV

Antes de leer y escribir oficialmente archivos CSV, debemos definir una clase de prueba para realizar pruebas. El código se muestra a continuación:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

dieciséis

17

18

19

20

21

22

23

24

25

26

27

28

29

30

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文件时用到

    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;

        }

    }

}

Genere algunos datos de prueba, el código es el siguiente:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

dieciséis

17

18

19

20

21

22

23

24

25

26

27

28

29

30

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

    //测试数据

    var records = new List<Test> { test, test };

    //写CSV文件

    /*

    *直接把后面的写CSV文件代码复制到此处

    */

    //读CSV文件

     /*

    *直接把后面的读CSV文件代码复制到此处

    */

    

    Console.ReadLine();

}

Usando CsvHelper

CsvHelper es una biblioteca para leer y escribir archivos CSV, que admite la lectura y escritura de objetos de clase personalizados.

github上标星最高的CSV文件读写C#库,使用MS-PL、Apache 2.0开源协议。

使用NuGet下载CsvHelper,读写CSV文件的代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

//写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>());

}

如果你只想要拿来就能用的库,那文章基本上到这里就结束了。

使用自定义方法

为了与CsvHelper区分,新建一个CsvFile类存放自定义读写CSV文件的代码,最后会提供类的完整源码。CsvFile类定义如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

/// <summary>

/// CSV文件读写工具类

/// </summary>

public class CsvFile

{

    #region 写CSV文件

    //具体代码...

    #endregion

    #region 读CSV文件(使用TextFieldParser)

    //具体代码...

    #endregion

    #region 读CSV文件(使用正则表达式)

    //具体代码...

    #endregion

}

基于简化标准的写CSV文件

根据简化标准(具体标准内容见前文),写CSV文件代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

#region 写CSV文件

//字段数组转为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;

}

//默认的字段转换方法

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文件,默认第一行为标题

/// </summary>

/// <typeparam name="T"></typeparam>

/// <param name="list">数据列表</param>

/// <param name="path">文件路径</param>

/// <param name="append">追加记录</param>

/// <param name="func">字段转换方法</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

使用时,代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

//写CSV文件

//使用自定义的字段转换方法,也是文章开头复杂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;

}));

//使用默认的字段转换方法

//CsvFile.Write(records, path);

你也可以使用默认的字段转换方法,代码如下:

?

1

CsvFile.Save(records, path);

使用TextFieldParser解析CSV文件

TextFieldParser是VB中解析CSV文件的类,C#虽然没有类似功能的类,不过可以调用VB的TextFieldParser来实现功能。

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

#region 读CSV文件(使用TextFieldParser)

/// <summary>

/// 读CSV文件,默认第一行为标题

/// </summary>

/// <typeparam name="T"></typeparam>

/// <param name="path">文件路径</param>

/// <param name="func">字段解析规则</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;

        //设定逗号分隔符

        parser.SetDelimiters(",");

        //设定不忽略字段前后的空格

        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

            {

                //忽略标题行业

                isLine = true;

            }

        }

    }

    return list;

}

#endregion

使用时,代码如下:

?

1

2

//读CSV文件

records = CsvFile.Read(path, Test.Parse);

使用正则表达式解析CSV文件

如果你有一个问题,想用正则表达式来解决,那么你就有两个问题了。

正则表达式有一定的学习门槛,而且学习后不经常使用就会忘记。正则表达式解决的大多数是一些不易变更需求的问题,这就导致一个稳定可用的正则表达式可以传好几代。

本节的正则表达式来自 《精通正则表达式(第3版)》 第6章 打造高效正则表达式——简单的消除循环的例子,有兴趣的可以去了解一下,表达式说明如下:

Nota: La versión final de la expresión regular para analizar archivos CSV en este libro es la versión Java que utiliza cuantificadores posesivos en lugar de agrupación solidificada. También es una versión que se ve a menudo en Baidu. Sin embargo, hay un problema con el cuantificador de prioridad posesiva en C#. No puedo resolverlo debido a mi capacidad limitada, así que utilicé la versión que se muestra arriba. Sin embargo, no existe diferencia en el rendimiento entre las dos versiones de expresiones regulares.

La expresión regular que analiza el código del archivo CSV es la siguiente:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

dieciséis

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

sesenta y cinco

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

#region 读CSV文件(使用正则表达式)

/// <summary>

/// 读CSV文件,默认第一行为标题

/// </summary>

/// <typeparam name="T"></typeparam>

/// <param name="path">文件路径</param>

/// <param name="func">字段解析规则</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();

            //一个完整的CSV记录行,它的双引号一定是偶数

            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

                {

                    //忽略标题行业

                    isLine = true;

                }

                sbr.Clear();

            }

            else

            {

                sbr.Append(Environment.NewLine);

            }                  

        }

    }

    if (sbr.Length > 0)

    {

        //有解析失败的字符串,报错或忽略

    }

    return list;

}

//重写ReadLine方法,只有\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

El código cuando se utiliza es el siguiente:

?

1

2

//读CSV文件

records = CsvFile.Read_Regex(path, Test.Parse);

Hasta el momento no se han encontrado errores en el análisis de expresiones regulares, pero aún no se recomienda su uso.

Clase de herramienta CsvFile completa

El código completo de la clase CsvFile es el siguiente:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

dieciséis

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

sesenta y cinco

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

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文件

        //字段数组转为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;

        }

        //默认的字段转换方法

        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文件,默认第一行为标题

        /// </summary>

        /// <typeparam name="T"></typeparam>

        /// <param name="list">数据列表</param>

        /// <param name="path">文件路径</param>

        /// <param name="append">追加记录</param>

        /// <param name="func">字段转换方法</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文件,默认第一行为标题

        /// </summary>

        /// <typeparam name="T"></typeparam>

        /// <param name="path">文件路径</param>

        /// <param name="func">字段解析规则</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;

                //设定逗号分隔符

                parser.SetDelimiters(",");

                //设定不忽略字段前后的空格

                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

                    {

                        //忽略标题行业

                        isLine = true;

                    }

                }

            }

            return list;

        }

        #endregion

        #region 读CSV文件(使用正则表达式)

        /// <summary>

        /// 读CSV文件,默认第一行为标题

        /// </summary>

        /// <typeparam name="T"></typeparam>

        /// <param name="path">文件路径</param>

        /// <param name="func">字段解析规则</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();

                    //一个完整的CSV记录行,它的双引号一定是偶数

                    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

                        {

                            //忽略标题行业

                            isLine = true;

                        }

                        sbr.Clear();

                    }

                    else

                    {

                        sbr.Append(Environment.NewLine);

                    }                  

                }

            }

            if (sbr.Length > 0)

            {

                //有解析失败的字符串,报错或忽略

            }

            return list;

        }

        //重写ReadLine方法,只有\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

    }

}

Cómo usarlo:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

dieciséis

17

18

19

20

//写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);

Supongo que te gusta

Origin blog.csdn.net/qq_15509251/article/details/131994316
Recomendado
Clasificación