Skip to content

Commit

Permalink
!10 为 CsvConfiguration 增加 SplitFn
Browse files Browse the repository at this point in the history
Merge pull request !10 from 韩严重/SplitFn
  • Loading branch information
shps951023 authored and gitee-org committed Jan 7, 2023
2 parents a319bdb + 49cb28c commit 496cb48
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/MiniExcel/Csv/CsvConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.IO;
using System.Text;

Expand All @@ -9,7 +9,8 @@ public class CsvConfiguration : Configuration
private static Encoding _defaultEncoding = new UTF8Encoding(true);

public char Seperator { get; set; } = ',';
public string NewLine { get; set; } = "\r\n";
public string NewLine { get; set; } = "\r\n";
public Func<string, string[]> SplitFn { get; set; }
public Func<Stream, StreamReader> StreamReaderFunc { get; set; } = (stream) => new StreamReader(stream, _defaultEncoding);
public Func<Stream, StreamWriter> StreamWriterFunc { get; set; } = (stream) => new StreamWriter(stream, _defaultEncoding);

Expand Down
15 changes: 11 additions & 4 deletions src/MiniExcel/Csv/CsvReader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using MiniExcelLibs.OpenXml;
using MiniExcelLibs.OpenXml;
using MiniExcelLibs.Utils;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -73,9 +73,16 @@ public IEnumerable<IDictionary<string, object>> Query(bool useHeaderRow, string

private string[] Split(string row)
{
return Regex.Split(row, $"[\t{_config.Seperator}](?=(?:[^\"]|\"[^\"]*\")*$)")
.Select(s => Regex.Replace(s.Replace("\"\"", "\""), "^\"|\"$", "")).ToArray();
//this code from S.O : https://stackoverflow.com/a/11365961/9131476
if (_config.SplitFn != null)
{
return _config.SplitFn(row);
}
else
{
return Regex.Split(row, $"[\t{_config.Seperator}](?=(?:[^\"]|\"[^\"]*\")*$)")
.Select(s => Regex.Replace(s.Replace("\"\"", "\""), "^\"|\"$", "")).ToArray();
//this code from S.O : https://stackoverflow.com/a/11365961/9131476
}
}

public Task<IEnumerable<IDictionary<string, object>>> QueryAsync(bool UseHeaderRow, string sheetName, string startCell,CancellationToken cancellationToken = default(CancellationToken))
Expand Down

0 comments on commit 496cb48

Please sign in to comment.