1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
import * as ExcelJS from 'exceljs';
import { saveAs } from "file-saver";
import type { TechVendorPossibleItemsData } from '../service';
import { format } from 'date-fns';
import { ko } from 'date-fns/locale';
/**
* 기술영업 벤더 가능 아이템 데이터를 Excel 파일로 내보내기 (새로운 스키마 버전)
*/
export async function exportTechVendorPossibleItemsToExcel(
data: TechVendorPossibleItemsData[]
) {
try {
// 워크북 생성
const workbook = new ExcelJS.Workbook();
workbook.creator = 'Tech Vendor Possible Items Management System';
workbook.created = new Date();
// 워크시트 생성
const worksheet = workbook.addWorksheet('기술영업 벤더 가능 아이템');
// 컬럼 헤더 정의 및 스타일 적용 (새로운 스키마에 맞춰)
worksheet.columns = [
{ header: '번호', key: 'id', width: 10 },
{ header: '벤더코드', key: 'vendorCode', width: 15 },
{ header: '벤더명', key: 'vendorName', width: 25 },
{ header: '벤더이메일', key: 'vendorEmail', width: 30 },
{ header: '벤더타입', key: 'techVendorType', width: 20 },
{ header: '아이템코드', key: 'itemCode', width: 20 },
{ header: '공종', key: 'workType', width: 15 },
{ header: '선종', key: 'shipTypes', width: 20 },
{ header: '아이템리스트', key: 'itemList', width: 30 },
{ header: '서브아이템리스트', key: 'subItemList', width: 30 },
{ header: '생성일시', key: 'createdAt', width: 20 },
];
// 헤더 스타일 적용
const headerRow = worksheet.getRow(1);
headerRow.eachCell((cell) => {
cell.fill = {
type: 'pattern',
pattern: 'solid',
fgColor: { argb: 'FFE6F3FF' }
};
cell.font = {
bold: true,
color: { argb: 'FF1F4E79' }
};
cell.border = {
top: { style: 'thin' },
left: { style: 'thin' },
bottom: { style: 'thin' },
right: { style: 'thin' }
};
cell.alignment = {
vertical: 'middle',
horizontal: 'center'
};
});
// 데이터 추가 (새로운 스키마 필드들 포함)
data.forEach((item, index) => {
// 벤더 타입 파싱
let vendorTypes = '';
try {
const parsed = JSON.parse(item.techVendorType || "[]");
vendorTypes = Array.isArray(parsed) ? parsed.join(', ') : item.techVendorType;
} catch {
vendorTypes = item.techVendorType;
}
const row = worksheet.addRow({
id: item.id,
vendorCode: item.vendorCode || '-',
vendorName: item.vendorName,
vendorEmail: item.vendorEmail || '-',
techVendorType: vendorTypes,
itemCode: item.itemCode,
workType: item.workType || '-',
shipTypes: item.shipTypes || '-',
itemList: item.itemList || '-',
subItemList: item.subItemList || '-',
createdAt: format(item.createdAt, 'yyyy-MM-dd HH:mm', { locale: ko }),
});
// 데이터 행 스타일
row.eachCell((cell, colNumber) => {
cell.border = {
top: { style: 'thin' },
left: { style: 'thin' },
bottom: { style: 'thin' },
right: { style: 'thin' }
};
if (colNumber === 1) {
// ID 컬럼 가운데 정렬
cell.alignment = { vertical: 'middle', horizontal: 'center' };
} else {
// 나머지 컬럼 왼쪽 정렬
cell.alignment = { vertical: 'middle', horizontal: 'left' };
}
// 텍스트 줄바꿈 처리 (긴 텍스트 필드들)
if (colNumber >= 9 && colNumber <= 10) { // itemList, subItemList 컬럼
cell.alignment = {
vertical: 'top',
horizontal: 'left',
wrapText: true
};
}
});
// 홀수 행 배경색
if (index % 2 === 1) {
row.eachCell((cell) => {
cell.fill = {
type: 'pattern',
pattern: 'solid',
fgColor: { argb: 'FFF8F9FA' }
};
});
}
});
// 요약 정보 워크시트 생성 (새로운 스키마 통계 포함)
const summarySheet = workbook.addWorksheet('요약 정보');
const summaryData = [
['기술영업 벤더 가능 아이템 현황 (새로운 스키마)', ''],
['', ''],
['📊 기본 통계:', ''],
['총 항목 수:', data.length.toLocaleString()],
['고유 벤더 수:', new Set(data.map(item => item.vendorId)).size.toLocaleString()],
['고유 아이템 수:', new Set(data.map(item => item.itemCode)).size.toLocaleString()],
['', ''],
['🏢 벤더 타입별 분포:', ''],
...getVendorTypeDistribution(data),
['', ''],
['⚙️ 공종별 분포:', ''],
...getWorkTypeDistribution(data),
['', ''],
['🚢 선종별 분포:', ''],
...getShipTypeDistribution(data),
['', ''],
['📈 데이터 완성도:', ''],
...getDataCompleteness(data),
['', ''],
['내보내기 일시:', format(new Date(), 'yyyy-MM-dd HH:mm:ss', { locale: ko })],
];
summaryData.forEach((rowData, index) => {
const row = summarySheet.addRow(rowData);
if (index === 0) {
// 제목 스타일
row.getCell(1).font = { bold: true, size: 16, color: { argb: 'FF1F4E79' } };
} else if (typeof rowData[0] === 'string' && rowData[0].includes(':') && rowData[1] === '') {
// 섹션 제목 스타일
row.getCell(1).font = { bold: true, color: { argb: 'FF1F4E79' } };
} else if (typeof rowData[0] === 'string' && rowData[0].includes('📊') || rowData[0].includes('🏢') || rowData[0].includes('⚙️') || rowData[0].includes('🚢') || rowData[0].includes('📈')) {
// 이모지 섹션 제목 스타일
row.getCell(1).font = { bold: true, color: { argb: 'FF1F4E79' } };
}
});
summarySheet.getColumn(1).width = 40;
summarySheet.getColumn(2).width = 20;
// 파일 생성 및 다운로드
const buffer = await workbook.xlsx.writeBuffer();
const blob = new Blob([buffer], {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
});
const fileName = `기술영업_벤더_가능_아이템_${format(new Date(), 'yyyyMMdd_HHmmss')}.xlsx`;
saveAs(blob, fileName);
return { success: true };
} catch (error) {
console.error("Excel 내보내기 중 오류:", error);
return {
success: false,
error: error instanceof Error ? error.message : "내보내기 중 오류가 발생했습니다."
};
}
}
/**
* 벤더 타입별 분포 계산
*/
function getVendorTypeDistribution(data: TechVendorPossibleItemsData[]): [string, string][] {
const typeCount = new Map<string, number>();
data.forEach(item => {
try {
const parsed = JSON.parse(item.techVendorType || "[]");
const types = Array.isArray(parsed) ? parsed : [item.techVendorType];
types.forEach(type => {
if (type) {
typeCount.set(type, (typeCount.get(type) || 0) + 1);
}
});
} catch {
if (item.techVendorType) {
typeCount.set(item.techVendorType, (typeCount.get(item.techVendorType) || 0) + 1);
}
}
});
return Array.from(typeCount.entries())
.sort((a, b) => b[1] - a[1])
.map(([type, count]) => [` - ${type}`, count.toLocaleString()]);
}
/**
* 공종별 분포 계산
*/
function getWorkTypeDistribution(data: TechVendorPossibleItemsData[]): [string, string][] {
const workTypeCount = new Map<string, number>();
data.forEach(item => {
const workType = item.workType || '미분류';
workTypeCount.set(workType, (workTypeCount.get(workType) || 0) + 1);
});
return Array.from(workTypeCount.entries())
.sort((a, b) => b[1] - a[1])
.slice(0, 10) // 상위 10개만 표시
.map(([type, count]) => [` - ${type}`, count.toLocaleString()]);
}
/**
* 선종별 분포 계산
*/
function getShipTypeDistribution(data: TechVendorPossibleItemsData[]): [string, string][] {
const shipTypeCount = new Map<string, number>();
data.forEach(item => {
if (item.shipTypes) {
// 여러 선종이 콤마로 구분되어 있을 수 있음
const shipTypes = item.shipTypes.split(',').map(s => s.trim());
shipTypes.forEach(shipType => {
if (shipType) {
shipTypeCount.set(shipType, (shipTypeCount.get(shipType) || 0) + 1);
}
});
} else {
shipTypeCount.set('미분류', (shipTypeCount.get('미분류') || 0) + 1);
}
});
return Array.from(shipTypeCount.entries())
.sort((a, b) => b[1] - a[1])
.slice(0, 10) // 상위 10개만 표시
.map(([type, count]) => [` - ${type}`, count.toLocaleString()]);
}
/**
* 데이터 완성도 계산
*/
function getDataCompleteness(data: TechVendorPossibleItemsData[]): [string, string][] {
const total = data.length;
const completeness = [
['벤더이메일 있음', `${data.filter(item => item.vendorEmail).length}/${total} (${((data.filter(item => item.vendorEmail).length / total) * 100).toFixed(1)}%)`],
['공종 있음', `${data.filter(item => item.workType).length}/${total} (${((data.filter(item => item.workType).length / total) * 100).toFixed(1)}%)`],
['선종 있음', `${data.filter(item => item.shipTypes).length}/${total} (${((data.filter(item => item.shipTypes).length / total) * 100).toFixed(1)}%)`],
['아이템리스트 있음', `${data.filter(item => item.itemList).length}/${total} (${((data.filter(item => item.itemList).length / total) * 100).toFixed(1)}%)`],
['서브아이템리스트 있음', `${data.filter(item => item.subItemList).length}/${total} (${((data.filter(item => item.subItemList).length / total) * 100).toFixed(1)}%)`],
];
return completeness.map(([label, stat]) => [` - ${label}`, stat]);
}
|