Add shared export infrastructure

This commit is contained in:
2026-08-18 21:21:56 +02:00
parent 41bf2c0756
commit 26461bd2f2
13 changed files with 336 additions and 63 deletions
+45
View File
@@ -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('"');
}
}