|
@@ -1,23 +1,23 @@
|
|
|
-using System;
|
|
|
|
|
-using System.Collections.Generic;
|
|
|
|
|
-using System.Data;
|
|
|
|
|
-using System.IO;
|
|
|
|
|
-using System.IO.Abstractions;
|
|
|
|
|
-using System.Linq;
|
|
|
|
|
-using System.Text;
|
|
|
|
|
-
|
|
|
|
|
-namespace Quadarax.Foundation.Core.Data
|
|
|
|
|
-{
|
|
|
|
|
- public static class CsvHelper
|
|
|
|
|
|
|
+using System;
|
|
|
|
|
+using System.Collections.Generic;
|
|
|
|
|
+using System.Data;
|
|
|
|
|
+using System.IO;
|
|
|
|
|
+using System.IO.Abstractions;
|
|
|
|
|
+using System.Linq;
|
|
|
|
|
+using System.Text;
|
|
|
|
|
+
|
|
|
|
|
+namespace Quadarax.Foundation.Core.Data
|
|
|
|
|
+{
|
|
|
|
|
+ public static class CsvHelper
|
|
|
{
|
|
{
|
|
|
- /// <summary>
|
|
|
|
|
- /// Read CSV file and creates DataTable with filled content, handling quoted values.
|
|
|
|
|
- /// </summary>
|
|
|
|
|
- /// <param name="fs">Used filesystem.</param>
|
|
|
|
|
- /// <param name="csvFile">Full path to the CSV file.</param>
|
|
|
|
|
- /// <param name="delimiter">The delimiter used in the CSV file.</param>
|
|
|
|
|
- /// <param name="hasHeaderRow">Indicates whether the CSV file has a header row.</param>
|
|
|
|
|
- /// <returns>New created DataTable with content.</returns>
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// Read CSV file and creates DataTable with filled content, handling quoted values.
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ /// <param name="fs">Used filesystem.</param>
|
|
|
|
|
+ /// <param name="csvFile">Full path to the CSV file.</param>
|
|
|
|
|
+ /// <param name="delimiter">The delimiter used in the CSV file.</param>
|
|
|
|
|
+ /// <param name="hasHeaderRow">Indicates whether the CSV file has a header row.</param>
|
|
|
|
|
+ /// <returns>New created DataTable with content.</returns>
|
|
|
public static DataTable CsvToDataTable(IFileSystem fs, string csvFile, string delimiter = ",", bool hasHeaderRow = true)
|
|
public static DataTable CsvToDataTable(IFileSystem fs, string csvFile, string delimiter = ",", bool hasHeaderRow = true)
|
|
|
{
|
|
{
|
|
|
|
|
|
|
@@ -63,12 +63,12 @@ namespace Quadarax.Foundation.Core.Data
|
|
|
return dt;
|
|
return dt;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /// <summary>
|
|
|
|
|
- /// Parses a single CSV line, handling quoted values and delimiters within quotes.
|
|
|
|
|
- /// </summary>
|
|
|
|
|
- /// <param name="line">The CSV line to parse.</param>
|
|
|
|
|
- /// <param name="delimiter">The delimiter used in the CSV line.</param>
|
|
|
|
|
- /// <returns>An array of field values.</returns>
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// Parses a single CSV line, handling quoted values and delimiters within quotes.
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ /// <param name="line">The CSV line to parse.</param>
|
|
|
|
|
+ /// <param name="delimiter">The delimiter used in the CSV line.</param>
|
|
|
|
|
+ /// <returns>An array of field values.</returns>
|
|
|
private static string[]? ParseCsvLine(string? line, string delimiter)
|
|
private static string[]? ParseCsvLine(string? line, string delimiter)
|
|
|
{
|
|
{
|
|
|
if (string.IsNullOrEmpty(line))
|
|
if (string.IsNullOrEmpty(line))
|
|
@@ -121,13 +121,13 @@ namespace Quadarax.Foundation.Core.Data
|
|
|
return result.ToArray();
|
|
return result.ToArray();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /// <summary>
|
|
|
|
|
- /// Writes a DataTable to a CSV file, properly handling quoting.
|
|
|
|
|
- /// </summary>
|
|
|
|
|
- /// <param name="fs">Used filesystem.</param>
|
|
|
|
|
- /// <param name="dataTable">The DataTable to write.</param>
|
|
|
|
|
- /// <param name="filePath">The full path of the output CSV file.</param>
|
|
|
|
|
- /// <param name="delimiter">The delimiter to use in the CSV file.</param>
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// Writes a DataTable to a CSV file, properly handling quoting.
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ /// <param name="fs">Used filesystem.</param>
|
|
|
|
|
+ /// <param name="dataTable">The DataTable to write.</param>
|
|
|
|
|
+ /// <param name="filePath">The full path of the output CSV file.</param>
|
|
|
|
|
+ /// <param name="delimiter">The delimiter to use in the CSV file.</param>
|
|
|
public static void DataTableToCsv(IFileSystem fs,DataTable dataTable, string filePath, string delimiter = ",")
|
|
public static void DataTableToCsv(IFileSystem fs,DataTable dataTable, string filePath, string delimiter = ",")
|
|
|
{
|
|
{
|
|
|
using (var sw = fs.File.CreateText(filePath))
|
|
using (var sw = fs.File.CreateText(filePath))
|
|
@@ -144,13 +144,13 @@ namespace Quadarax.Foundation.Core.Data
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /// <summary>
|
|
|
|
|
- /// Reads a CSV file and returns it as a list of dictionaries.
|
|
|
|
|
- /// </summary>
|
|
|
|
|
- /// <param name="fs">Used filesystem.</param>
|
|
|
|
|
- /// <param name="csvFile">Full path to the CSV file.</param>
|
|
|
|
|
- /// <param name="delimiter">The delimiter used in the CSV file.</param>
|
|
|
|
|
- /// <returns>List of dictionaries representing the CSV data.</returns>
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// Reads a CSV file and returns it as a list of dictionaries.
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ /// <param name="fs">Used filesystem.</param>
|
|
|
|
|
+ /// <param name="csvFile">Full path to the CSV file.</param>
|
|
|
|
|
+ /// <param name="delimiter">The delimiter used in the CSV file.</param>
|
|
|
|
|
+ /// <returns>List of dictionaries representing the CSV data.</returns>
|
|
|
public static List<Dictionary<string, string>> CsvToDictionaryList(IFileSystem fs, string csvFile, string delimiter = ",")
|
|
public static List<Dictionary<string, string>> CsvToDictionaryList(IFileSystem fs, string csvFile, string delimiter = ",")
|
|
|
{
|
|
{
|
|
|
if (!fs.File.Exists(csvFile)) throw new FileNotFoundException($"File '{csvFile}' not found!", csvFile);
|
|
if (!fs.File.Exists(csvFile)) throw new FileNotFoundException($"File '{csvFile}' not found!", csvFile);
|
|
@@ -189,6 +189,6 @@ namespace Quadarax.Foundation.Core.Data
|
|
|
return $"\"{value.Replace("\"", "\"\"")}\"";
|
|
return $"\"{value.Replace("\"", "\"\"")}\"";
|
|
|
}
|
|
}
|
|
|
return value;
|
|
return value;
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|