// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Data; namespace Spludlow.Data { public class CSV { public static DataTable Read(string filename) { return Read(filename, Encoding.Default, '"'); } public static DataTable Read(string filename, Encoding encoding) { return Read(filename, encoding, '"'); } public static DataTable Read(string filename, Encoding encoding, char quote) { using (FileStream fileStream = new FileStream(filename, FileMode.Open)) { string tableName = Spludlow.Io.Paths.LegalFileName(Path.GetFileNameWithoutExtension(filename)); return Read(fileStream, tableName, encoding, quote); } } public static DataTable Read(Stream stream) { return Read(stream, null, Encoding.Default, '"'); } public static DataTable Read(Stream stream, string tableName, Encoding encoding, char quote) { DataTable table = null; using (StreamReader reader = new StreamReader(stream, encoding)) { string line; while ((line = reader.ReadLine()) != null) { if (table == null) { table = new DataTable(); if (tableName != null) table.TableName = tableName; string[] columnNames = line.Split(new char[] { ',' }); foreach (string columnName in columnNames) table.Columns.Add(columnName.Trim(), typeof(string)); continue; } DataRow row = table.NewRow(); StringBuilder word = new StringBuilder(); int index = 0; bool inQuote = false; foreach (char ch in line) { if (ch == quote) { inQuote = !inQuote; continue; } if (inQuote == false && ch == ',') { row[index] = word.ToString(); ++index; word.Length = 0; continue; } word.Append(ch); } row[index] = word.ToString(); table.Rows.Add(row); } } return table; } public static void Write(string filename, DataTable table) { using (FileStream fileStream = new FileStream(filename, FileMode.Create)) Write(fileStream, table, Encoding.UTF8, '"'); } public static void Write(string filename, DataTable table, Encoding encoding) { using (FileStream fileStream = new FileStream(filename, FileMode.Create)) Write(fileStream, table, encoding, '"'); } public static void Write(Stream stream, DataTable table, Encoding encoding, char quote) { using (StreamWriter writer = new StreamWriter(stream, encoding)) { for (int colIndex = 0; colIndex < table.Columns.Count; ++colIndex) { if (colIndex > 0) writer.Write(","); writer.Write(table.Columns[colIndex].ColumnName); } writer.WriteLine(); foreach (DataRow row in table.Rows) { for (int colIndex = 0; colIndex < table.Columns.Count; ++colIndex) { if (colIndex > 0) writer.Write(","); if (row.IsNull(colIndex) == false) { if (table.Columns[colIndex].DataType == typeof(string)) { string value = (string)row[colIndex]; bool needQuotes = true; // value.Contains(quote); if (needQuotes == true) writer.Write(quote); writer.Write(value); if (needQuotes == true) writer.Write(quote); } else { writer.Write(Convert.ToString(row[colIndex])); } } } writer.WriteLine(); } } } } }