diff --git a/backend/src/services/csvExportService.ts b/backend/src/services/csvExportService.ts index 7f7cea9..bf380df 100644 --- a/backend/src/services/csvExportService.ts +++ b/backend/src/services/csvExportService.ts @@ -215,15 +215,16 @@ class CSVExportService { */ private static escapeCSVValue(value: string): string { if (!value) return ''; - - // Replace newlines with spaces and trim - const cleanValue = value.replace(/\n/g, ' ').replace(/\r/g, ' ').trim(); - + + // Normalize line endings but preserve newlines for readability in spreadsheet cells + const cleanValue = value.replace(/\r\n/g, '\n').replace(/\r/g, '\n').trim(); + // If value contains comma, quote, or newline, wrap in quotes and escape internal quotes + // Excel/Google Sheets render newlines within quoted cells correctly if (cleanValue.includes(',') || cleanValue.includes('"') || cleanValue.includes('\n')) { return `"${cleanValue.replace(/"/g, '""')}"`; } - + return cleanValue; }