@@ -139,6 +139,22 @@ function applyHeaderStyle(sheet, columns, headerStyle) {
139139 }
140140}
141141
142+ /**
143+ * 지정된 행에 헤더 스타일을 적용
144+ * @param {Object } sheet - ExcelJS 워크시트 객체
145+ * @param {Array } columns - 컬럼 배열
146+ * @param {Object } headerStyle - 헤더 스타일 객체
147+ * @param {number } rowNumber - 헤더가 위치할 행 번호
148+ */
149+ function applyHeaderStyleAtRow ( sheet , columns , headerStyle , rowNumber ) {
150+ if ( ! headerStyle || ! sheet || ! columns ) return ;
151+
152+ for ( let i = 1 ; i <= columns . length ; i ++ ) {
153+ const cell = sheet . getRow ( rowNumber ) . getCell ( i ) ;
154+ applyCellStyle ( cell , headerStyle ) ;
155+ }
156+ }
157+
142158/**
143159 * 데이터 행들에 스타일을 적용
144160 * @param {Object } sheet - ExcelJS 워크시트 객체
@@ -158,6 +174,26 @@ function applyBodyStyle(sheet, columns, dataRowCount, bodyStyle) {
158174 }
159175}
160176
177+ /**
178+ * 지정된 시작 행부터 데이터 행들에 스타일을 적용
179+ * @param {Object } sheet - ExcelJS 워크시트 객체
180+ * @param {Array } columns - 컬럼 배열
181+ * @param {number } dataRowCount - 데이터 행 수
182+ * @param {Object } bodyStyle - 데이터 스타일 객체
183+ * @param {number } startRow - 데이터 시작 행 번호
184+ */
185+ function applyBodyStyleAtRow ( sheet , columns , dataRowCount , bodyStyle , startRow ) {
186+ if ( ! bodyStyle || ! sheet || ! columns || dataRowCount <= 0 ) return ;
187+
188+ for ( let r = 0 ; r < dataRowCount ; r ++ ) {
189+ const row = sheet . getRow ( startRow + r ) ;
190+ for ( let i = 1 ; i <= columns . length ; i ++ ) {
191+ const cell = row . getCell ( i ) ;
192+ applyCellStyle ( cell , bodyStyle ) ;
193+ }
194+ }
195+ }
196+
161197/**
162198 * 컬럼 너비를 자동으로 계산
163199 * @param {Array } columns - 컬럼명 배열
@@ -194,7 +230,7 @@ function calculateColumnWidths(columns, data, colwidths = { min: 10, max: 30 })
194230 * @param {Object } excelStyle.header - 헤더 스타일
195231 * @param {Object } excelStyle.body - 데이터 스타일
196232 */
197- function applySheetStyle ( sheet , data , excelStyle ) {
233+ function applySheetStyle ( sheet , data , excelStyle , startRow = 1 ) {
198234 if ( ! sheet || ! data || data . length === 0 ) return ;
199235
200236 const columns = Object . keys ( data [ 0 ] ) ;
@@ -217,17 +253,28 @@ function applySheetStyle(sheet, data, excelStyle) {
217253 sheet . columns = columns . map ( key => ( { header : key , key } ) ) ;
218254 }
219255
220- // 데이터 추가
221- sheet . addRows ( data ) ;
256+ // 헤더 행 추가 (startRow 위치에)
257+ const headerRow = sheet . getRow ( startRow ) ;
258+ columns . forEach ( ( col , index ) => {
259+ headerRow . getCell ( index + 1 ) . value = col ;
260+ } ) ;
261+
262+ // 데이터 행 추가 (startRow + 1부터)
263+ data . forEach ( ( row , rowIndex ) => {
264+ const dataRow = sheet . getRow ( startRow + 1 + rowIndex ) ;
265+ columns . forEach ( ( col , colIndex ) => {
266+ dataRow . getCell ( colIndex + 1 ) . value = row [ col ] ;
267+ } ) ;
268+ } ) ;
222269
223- // 헤더 스타일 적용
270+ // 헤더 스타일 적용 (startRow에 적용)
224271 if ( excelStyle . header ) {
225- applyHeaderStyle ( sheet , columns , excelStyle . header ) ;
272+ applyHeaderStyleAtRow ( sheet , columns , excelStyle . header , startRow ) ;
226273 }
227274
228- // 데이터 스타일 적용
275+ // 데이터 스타일 적용 (startRow + 1부터)
229276 if ( excelStyle . body ) {
230- applyBodyStyle ( sheet , columns , data . length , excelStyle . body ) ;
277+ applyBodyStyleAtRow ( sheet , columns , data . length , excelStyle . body , startRow + 1 ) ;
231278 }
232279}
233280
0 commit comments