@@ -68,14 +68,14 @@ class ExcelGenerator {
6868 const ext = FileUtils . getExtension ( outputPath ) . toLowerCase ( ) ;
6969 const dir = FileUtils . getDirname ( outputPath ) ;
7070 const base = FileUtils . getBasename ( outputPath ) ;
71- const extNoDot = ext . startsWith ( '.' ) ? ext . slice ( 1 ) : ext ;
72- const targetDir = path . join ( dir , `${ base } _${ extNoDot } ` ) ;
71+ const targetDir = path . join ( dir , `${ base } ` ) ;
7372 if ( ! fs . existsSync ( targetDir ) ) {
7473 fs . mkdirSync ( targetDir , { recursive : true } ) ;
7574 }
7675 const delimiter = format === 'txt' ? '\t' : ',' ;
7776 const withBOM = true ;
7877 const crlf = '\r\n' ;
78+ const isCsv = format === 'csv' ;
7979
8080 function sanitizeFilename ( name ) {
8181 const replaced = String ( name ) . replace ( / [ \\ / : * ? " < > | ] / g, '_' ) . trim ( ) ;
@@ -99,10 +99,15 @@ class ExcelGenerator {
9999 function escapeCsv ( val ) {
100100 if ( val === null || val === undefined ) return '' ;
101101 const s = val instanceof Date ? formatDateTimeLocal ( val ) : String ( val ) ;
102- if ( s . includes ( '"' ) || s . includes ( '\n' ) || s . includes ( '\r' ) || s . includes ( delimiter ) ) {
103- return '"' + s . replace ( / " / g, '""' ) + '"' ;
102+ const t = s . replace ( / \r ? \n / g, ' ' ) ;
103+ if ( t . includes ( '"' ) || t . includes ( '\n' ) || t . includes ( '\r' ) || t . includes ( delimiter ) ) {
104+ return '"' + t . replace ( / " / g, '""' ) + '"' ;
104105 }
105- return s ;
106+ return t ;
107+ }
108+ function toPlain ( val ) {
109+ if ( val === null || val === undefined ) return '' ;
110+ return val instanceof Date ? formatDateTimeLocal ( val ) : String ( val ) . replace ( / \r ? \n / g, ' ' ) ;
106111 }
107112 function toSqlLiteral ( val ) {
108113 if ( val === null || val === undefined ) return 'NULL' ;
@@ -149,7 +154,10 @@ class ExcelGenerator {
149154 const lines = [ ] ;
150155 if ( columns . length > 0 ) lines . push ( columns . join ( delimiter ) ) ;
151156 for ( const r of rows ) {
152- const vals = columns . map ( c => escapeCsv ( r && r [ c ] !== undefined ? r [ c ] : '' ) ) ;
157+ const vals = columns . map ( c => {
158+ const v = r && r [ c ] !== undefined ? r [ c ] : '' ;
159+ return isCsv ? escapeCsv ( v ) : toPlain ( v ) ;
160+ } ) ;
153161 lines . push ( vals . join ( delimiter ) ) ;
154162 }
155163 const content = lines . join ( crlf ) + crlf ;
0 commit comments