/// <summary> /// 初始化写入流 /// </summary> /// <param name="stream">要写入的流</param> /// <param name="dataEncoding">字符编码</param> /// <param name="flag">csv 字段分隔符与限定符</param> /// <param name="dataHandler">通知事件参数, 每次通返回自上次通知以来写入的行数. </param> /// <param name="cancelToken">取消操作的token</param> /// <param name="writeProgressSize">当写入多少条数据时应触发进度通知事件, 默认为 1000, 此值应大于 0.</param> /// <param name="writeBufferLength">写入 IO 缓存,默认为 4M.</param> public CsvWriteHelper(Stream stream, Encoding dataEncoding, CsvFlag flag, Action <CsvWriteProgressInfo> dataHandler , CancellationToken cancelToken, int writeProgressSize = 1000, int writeBufferLength = 40960) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } if (dataEncoding == null) { throw new ArgumentNullException(nameof(dataEncoding)); } if (flag == null) { throw new ArgumentNullException(nameof(flag)); } if (writeProgressSize <= 0) { throw new ArgumentException("The property 'writeProgressSize' must be greater than 0"); } if (writeBufferLength <= 0) { throw new ArgumentException("The property 'writeBufferLength' must be greater than 0"); } columnCount = 0; TotalRowCount = 0L; DataEncoding = dataEncoding; Flag = flag; CancelToken = cancelToken; this.dataHandler = dataHandler; WriteProgressSize = writeProgressSize; WriteBufferLength = writeBufferLength; csvStream = new StreamWriter(stream, DataEncoding, WriteBufferLength); }
/// <summary> /// 初始化读取流 /// </summary> /// <param name="stream">要读取的流</param> /// <param name="dataEncoding">字符编码</param> /// <param name="flag">csv 字段分隔符与限定符</param> /// <param name="cancelToken">取消用token</param> /// <param name="firstRowIsHead">是否将第一行数据做为标题行</param> /// <param name="readBufferLength">流读取缓冲大小, 默认为 40960 字节.</param> public CsvReadHelper(Stream stream, Encoding dataEncoding, CsvFlag flag, CancellationToken cancelToken, bool firstRowIsHead = true, int readBufferLength = 40960) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } if (dataEncoding == null) { throw new ArgumentNullException(nameof(dataEncoding)); } if (flag == null) { throw new ArgumentNullException(nameof(flag)); } DataEncoding = dataEncoding; Flag = flag; CancelToken = cancelToken; FirstRowIsHead = firstRowIsHead; this.readBufferLength = readBufferLength; csvStream = new StreamReader(stream, DataEncoding, false, this.readBufferLength); }
/// <summary> /// 使用 Encoding.UTF8 编码初始化写入文件 /// </summary> /// <param name="csvFileName">csv 文件路径及名称</param> /// <param name="flag">csv 字段分隔符与限定符</param> /// <param name="cancelToken">取消操作的token</param> /// <param name="progress">通知事件参数, 每次通返回自上次通知以来写入的行数. 默认为 null, 表示不通知.</param> /// <param name="writeProgressSize">当写入多少条数据时应触发进度通知事件, 默认为 1000, 此值应大于 0.</param> /// <param name="writeBufferLength">写入 IO 缓存,默认为 4M.</param> public CsvWriteHelper(string csvFileName, CsvFlag flag, Action <CsvWriteProgressInfo> dataHandler = null , int writeProgressSize = 1000, int writeBufferLength = 40960) : this(csvFileName, Encoding.UTF8, flag, dataHandler, CancellationToken.None, writeProgressSize, writeBufferLength) { }
/// <summary> /// 初始化写入文件 /// </summary> /// <param name="csvFileName">csv 文件路径及名称</param> /// <param name="dataEncoding">字符编码</param> /// <param name="flag">csv 字段分隔符与限定符</param> /// <param name="cancelToken">取消操作的token</param> /// <param name="progress">通知事件参数, 每次通返回自上次通知以来写入的行数. </param> /// <param name="writeProgressSize">当写入多少条数据时应触发进度通知事件, 默认为 1000, 此值应大于 0.</param> /// <param name="writeBufferLength">写入 IO 缓存,默认为 4M.</param> public CsvWriteHelper(string csvFileName, Encoding dataEncoding, CsvFlag flag, Action <CsvWriteProgressInfo> dataHandler , CancellationToken cancelToken, int writeProgressSize = 1000, int writeBufferLength = 40960) : this(File.Open(csvFileName, FileMode.Create), dataEncoding, flag, dataHandler, cancelToken, writeProgressSize, writeBufferLength) { }
/// <summary> /// 使用 Encoding.UTF8 编码初始化写入流 /// </summary> /// <param name="stream">要写入的流</param> /// <param name="flag">csv 字段分隔符与限定符</param> /// <param name="progress">通知事件参数, 每次通返回自上次通知以来写入的行数. </param> /// <param name="writeProgressSize">当写入多少条数据时应触发进度通知事件, 默认为 1000, 此值应大于 0.</param> /// <param name="writeBufferLength">写入 IO 缓存,默认为 4M.</param> public CsvWriteHelper(Stream stream, CsvFlag flag, Action <CsvWriteProgressInfo> dataHandler , int writeProgressSize = 1000, int writeBufferLength = 40960) : this(stream, Encoding.UTF8, flag, dataHandler, CancellationToken.None, writeProgressSize, writeBufferLength) { }
/// <summary> /// 使用 Encoding.UTF8 编码初始化读取文件 /// </summary> /// <param name="csvFileName">要读取的文件路径及名称</param> /// <param name="flag">csv 字段分隔符与限定符</param> /// <param name="firstRowIsHead">是否将第一行数据做为标题行</param> /// <param name="readBufferLength">流读取缓冲大小, 默认为 40960 字节.</param> public CsvReadHelper(string csvFileName, CsvFlag flag, bool firstRowIsHead = true, int readBufferLength = 40960) : this(csvFileName, Encoding.UTF8, flag, CancellationToken.None, firstRowIsHead, readBufferLength) { }
/// <summary> /// 初始化读取文件 /// </summary> /// <param name="csvFileName">要读取的文件路径及名称</param> /// <param name="dataEncoding">字符编码</param> /// <param name="flag">csv 字段分隔符与限定符</param> /// <param name="cancelToken">取消用token</param> /// <param name="firstRowIsHead">是否将第一行数据做为标题行</param> /// <param name="readBufferLength">流读取缓冲大小, 默认为 40960 字节.</param> public CsvReadHelper(string csvFileName, Encoding dataEncoding, CsvFlag flag, CancellationToken cancelToken, bool firstRowIsHead = true, int readBufferLength = 40960) : this(File.Open(csvFileName, FileMode.Open), dataEncoding, flag, cancelToken, firstRowIsHead, readBufferLength) { }