Add shared export infrastructure
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
using System.Text;
|
||||
|
||||
namespace LehrerApp.Desktop.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Erzeugt semikolongetrennte CSV-Daten für die deutsche Excel-Umgebung und maskiert Felder
|
||||
/// nach RFC-4180-Regeln. Das Trennzeichen bleibt bewusst konfigurierbar für weitere Exportformate.
|
||||
/// </summary>
|
||||
public sealed class CsvBuilder(char separator = ';')
|
||||
{
|
||||
private readonly StringBuilder _content = new();
|
||||
|
||||
public CsvBuilder AddRow(params object?[] values)
|
||||
{
|
||||
for (var index = 0; index < values.Length; index++)
|
||||
{
|
||||
if (index > 0) _content.Append(separator);
|
||||
AppendEscaped(values[index]?.ToString() ?? "");
|
||||
}
|
||||
_content.AppendLine();
|
||||
return this;
|
||||
}
|
||||
|
||||
public CsvBuilder AddBlankRow()
|
||||
{
|
||||
_content.AppendLine();
|
||||
return this;
|
||||
}
|
||||
|
||||
public override string ToString() => _content.ToString();
|
||||
|
||||
private void AppendEscaped(string value)
|
||||
{
|
||||
if (!value.Contains(separator) && !value.Contains('"') &&
|
||||
!value.Contains('\r') && !value.Contains('\n'))
|
||||
{
|
||||
_content.Append(value);
|
||||
return;
|
||||
}
|
||||
|
||||
_content.Append('"');
|
||||
_content.Append(value.Replace("\"", "\"\"", StringComparison.Ordinal));
|
||||
_content.Append('"');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user