// import type { sheetParameter } from '@/index'; import * as XlsxPopulate from '@xlsx/xlsx-populate'; import type { SheetParameter } from './index'; const makeCellStyle =(styleQ: XlsxPopulate.StyleQuery )=>{ styleQ.fill({ type: "solid", color: {rgb:"9bc1e7"} }); styleQ.borderEach( { style: "thin", color: { rgb: "FFFFFF" } } ); styleQ.wrapText(true); // styleQ.fontColor({rgb:"FFFFFF"}); styleQ.fontSize(13); styleQ.horizontalAlignment("center"); styleQ.verticalAlignment("center"); styleQ.bold(true); } const makeExcelFile = async(workbook: XlsxPopulate.Workbook,fileNm:string)=>{ const outputData = await workbook.output("array-buffer"); const blob = new Blob([outputData], { type: "application/octet-stream" }); const link = document.createElement("a"); link.href = URL.createObjectURL(blob); link.download = fileNm+".xlsx"; link.click(); } export const excelStyle = async (data: ArrayBuffer, range: string = "A1:V3", fileNm:string) => { const workbook = await XlsxPopulate.Workbook.fromData(data); const rangeCells = workbook.activeSheet(0)?.sheet(0)?.range(range); console.log(rangeCells); rangeCells?.style((styleQ)=>{ makeCellStyle(styleQ) }); makeExcelFile(workbook, fileNm) } export const excelArrayStyle = async (data: ArrayBuffer, range: SheetParameter[], fileNm: string) => { const workbook = await XlsxPopulate.Workbook.fromData(data); if (range) { range.forEach((item, index) => { const rangeCells = workbook.activeSheet(index)?.sheet(index)?.range(item.headerRange); console.log(rangeCells); rangeCells?.style((styleQ) => { makeCellStyle(styleQ) }); // 모든 데이터 셀에 세로 가운데 정렬 적용 if (item.data && item.data.length > 0) { const numCols = item.data[0]?.length || 0; const numRows = item.data.length; const lastColLetter = getExcelColumnLetter(numCols); const dataRange = workbook.activeSheet(index)?.sheet(index)?.range(`A1:${lastColLetter}${numRows}`); dataRange?.style((styleQ) => { styleQ.verticalAlignment("center"); }); } }); } makeExcelFile(workbook, fileNm) } export const excelStyleWithKoreanHeaders = async ( data: ArrayBuffer, range: string = "A1:V3", fileNm: string, headerMapping: { [key: string]: string }, sampleData: any ) => { const workbook = await XlsxPopulate.Workbook.fromData(data); const sheet = workbook.activeSheet(); if (!sheet) { throw new Error('워크시트를 찾을 수 없습니다.'); } // 1. 영문 헤더를 한글로 교체 const englishHeaders = Object.keys(sampleData); englishHeaders.forEach((englishHeader, index) => { const koreanHeader = headerMapping[englishHeader] || englishHeader; const cellAddress = String.fromCharCode(65 + index) + '1'; // A1, B1, C1, ... sheet.cell(cellAddress).value(koreanHeader); }); // 2. 헤더 스타일 적용 const headerCells = sheet.range(range); headerCells?.style((styleQ) => { makeCellStyle(styleQ); }); // 3. 모든 데이터 셀에 세로 가운데 정렬 적용 const numCols = englishHeaders.length; const numRows = sheet.usedRange()?.endCell()?.rowNumber() || 1; if (numRows > 1) { const dataRange = sheet.range(`A1:${String.fromCharCode(65 + numCols - 1)}${numRows}`); dataRange?.style((styleQ) => { styleQ.verticalAlignment("center"); styleQ.borderEach({ style: "thin", color: { rgb: "CCCCCC" } }); }); } // 4. 파일 다운로드 await makeExcelFile(workbook, fileNm); }; const getExcelColumnLetter = (colIndex: number): string => { let letter = ''; while (colIndex > 0) { const mod = (colIndex - 1) % 26; letter = String.fromCharCode(65 + mod) + letter; colIndex = Math.floor((colIndex - 1) / 26); } return letter; };