Export: Add Ebics and improve Bki export

This commit is contained in:
2023-09-08 00:43:53 +02:00
parent 2de4739e9d
commit f5f00a7739
5 changed files with 180 additions and 48 deletions

View File

@ -2,43 +2,54 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Elwig.Helpers.Export {
public abstract class Csv<T> : IExporter<T> {
public static string FileExtension => "csv";
protected readonly StreamWriter Writer;
private readonly StreamWriter _writer;
protected readonly char Separator;
protected string? Header;
public Csv(string filename, char separator = ';') : this(filename, separator, Encoding.UTF8) { }
public Csv(string filename, char separator = ';') : this(filename, separator, Utils.UTF8) { }
public Csv(string filename, char separator, Encoding encoding) {
Writer = new StreamWriter(filename, false, encoding);
_writer = new StreamWriter(filename, false, encoding);
Separator = separator;
}
public void Dispose() {
GC.SuppressFinalize(this);
Writer.Dispose();
_writer.Dispose();
}
public ValueTask DisposeAsync() {
GC.SuppressFinalize(this);
return Writer.DisposeAsync();
return _writer.DisposeAsync();
}
public async Task ExportAsync(IEnumerable<T> data) {
if (Header != null) await Writer.WriteLineAsync(Header);
public async Task ExportAsync(IEnumerable<T> data, IProgress<double>? progress = null) {
progress?.Report(0.0);
int count = data.Count() + 2, i = 0;
if (Header != null) await _writer.WriteLineAsync(Header);
progress?.Report(100.0 * ++i / count);
foreach (var row in data) {
await Writer.WriteLineAsync(FormatRow(row));
await _writer.WriteLineAsync(FormatRow(row));
progress?.Report(100.0 * ++i / count);
}
await _writer.FlushAsync();
progress?.Report(100.0);
}
public void Export(IEnumerable<T> data) {
ExportAsync(data).GetAwaiter().GetResult();
public void Export(IEnumerable<T> data, IProgress<double>? progress = null) {
ExportAsync(data, progress).GetAwaiter().GetResult();
}
public string FormatRow(IEnumerable row) {