import { Readable, ReadableOptions, Transform, TransformCallback } from "stream" import { Worksheet, stream, Column, Workbook, Style } from "exceljs" import { Multer} from "multer" function setAdditionalKeyInstance(target:any, option:any):any { if(!target) return option; else { let keys = Object.keys(target); let optionKeys = Object.keys(option); if(!keys) { return option; } if(!optionKeys) { return target; } for(let keyIdx = 0; keyIdx < keys.length; keyIdx++) { let key = keys[keyIdx]; target[key] = !option[key] ? target[key] : option[key]; } for(let keyIdx = 0; keyIdx < optionKeys.length; keyIdx++) { let key = optionKeys[keyIdx]; target[key] = option[key]; } return target; } } export interface ExcelStreamOptions{ // zip: Partial; stream?: import('stream').Stream, // filename: string; //If stream not specified, this field specifies the path to a file to write the XLSX workbook to. useSharedStrings?: boolean, //Specifies whether to use shared strings in the workbook. Default is false useStyles?: boolean } export interface ExcelWorkSheetInfo{ name:string, columns:Column[] } export interface ExcelWorkBookOptions{ category?: string; company?: string; creator?: string; description?: string; keywords?: string; lastModifiedBy?: string; created?: Date; manager?: string; modified?: Date; lastPrinted?: Date; // properties?: WorkbookProperties; subject?: string; title?: string; } export interface ExcelDataInfo{ sheetName:string, row:any, } export interface ExcelParseInfo{ colName:string, targetIndex:number | Array convertFunction?:Function } export interface ExcelParseSheetInfo{ /** * row start index start from 1 */ rowStartIdx?:number, /** * col start index start from 1 */ colStartIdx?:number, /** * row end index excel row number */ rowEndIdx?:number, /** * col end index excel row number */ colEndIdx?:number, /** * additional parse options */ parseInfo?:Array } /** * excel 객체 생성 및 관리 */ export class ExcelExport{ public workbook:Workbook; /** * excel 객체 map */ public worksheetArr:Map = new Map(); /** * excel 객체 생성 및 관리 */ constructor(public fileName:string, public workbookOptions:ExcelWorkBookOptions, public sheetInfos:any[], public defaultWorksheetOption:any) { this.workbook = new Workbook(); if(workbookOptions) { this.workbook.category = workbookOptions.category as string this.workbook.company = workbookOptions.company as string this.workbook.creator = workbookOptions.creator as string this.workbook.description = workbookOptions.description as string this.workbook.keywords = workbookOptions.keywords as string this.workbook.lastModifiedBy = workbookOptions.lastModifiedBy as string this.workbook.created = workbookOptions.created as Date this.workbook.manager = workbookOptions.manager as string this.workbook.modified = workbookOptions.modified as Date this.workbook.lastPrinted = workbookOptions.lastPrinted as Date //this.workbook.properties = workbookOptions.properties this.workbook.subject = workbookOptions.subject as string this.workbook.title = workbookOptions.title as string; } for(let idx=0; idx < sheetInfos.length; idx++) { let sheetInfo = sheetInfos[idx]; let worksheetVal = this.workbook.addWorksheet(sheetInfo.name, !defaultWorksheetOption ? { views: [{showGridLines: true}],properties:{defaultRowHeight:13.2} }: defaultWorksheetOption); worksheetVal.columns = sheetInfo.columns let columnLength = !sheetInfo.columns ? 0 : sheetInfo.columns.length; worksheetVal.columns = sheetInfo.columns; let mergeDistance = 0; if(sheetInfo.headers) { let headers = sheetInfo.headers; for(let infoIdx = 0; infoIdx < headers.length ; infoIdx++) { let header:any = headers[infoIdx]; let targetRow = infoIdx + 1 + mergeDistance; if(typeof header !== "string") { let targetHeaderRow = worksheetVal.getRow(targetRow); if(header.merge) { if(header.merge.cellStart && header.merge.cellEnd) { worksheetVal.mergeCells(header.merge.cellStart, header.merge.cellEnd); let distanceReg = new RegExp('[a-zA-Z]', 'g') let distanceFrom = 0; let distanceEnd = 0; try { distanceFrom = Number(header.merge.cellStart.replace(distanceReg, '')); distanceEnd = Number(header.merge.cellEnd.replace(distanceReg, '')); } catch(e) { } mergeDistance += (distanceFrom - distanceEnd); } else { worksheetVal.mergeCells(header.merge.rowStart, header.merge.colStart, header.merge.rowEnd, header.merge.colEnd); mergeDistance += (header.merge.rowEnd - header.merge.rowStart); } } if(typeof header.value === "string") { targetHeaderRow.getCell(1).value = header.value; } else if(Array.isArray(header.value)) { for(let valIdx = 0; valIdx < header.value.length; valIdx++) { let targetValue = header.value[valIdx]; if(typeof targetValue === "string") { targetHeaderRow.getCell(1).value = targetValue; } else if (targetValue.index) { targetHeaderRow.getCell(targetValue.index).value = targetValue.value; } } } targetHeaderRow.eachCell({ includeEmpty: true }, (cell:any, colNumber:any)=>{ cell.style = this.getDefaultCellStyle(0); }); if(header.style) { let styleValue = header.style; if(Array.isArray(styleValue)) { for(let styleIdx = 0; styleIdx < styleValue.length; styleIdx++) { let singleStyle = styleValue[styleIdx]; // targetHeaderRow.getCell(singleStyle.index).style = singleStyle.style; if(!singleStyle.index) { } else { let getSingleCell = targetHeaderRow.getCell(singleStyle.index); getSingleCell.style = setAdditionalKeyInstance(getSingleCell.style, singleStyle.style) } } } else { targetHeaderRow.eachCell({ includeEmpty: true }, (cell:any, colNumber:any)=>{ // cell.style = styleValue as Partial