diff options
Diffstat (limited to 'lib/items-tech/table/ship')
| -rw-r--r-- | lib/items-tech/table/ship/import-item-handler.tsx | 266 | ||||
| -rw-r--r-- | lib/items-tech/table/ship/item-excel-template.tsx | 220 | ||||
| -rw-r--r-- | lib/items-tech/table/ship/items-table-toolbar-actions.tsx | 352 |
3 files changed, 414 insertions, 424 deletions
diff --git a/lib/items-tech/table/ship/import-item-handler.tsx b/lib/items-tech/table/ship/import-item-handler.tsx index 57546cc6..b0f475ff 100644 --- a/lib/items-tech/table/ship/import-item-handler.tsx +++ b/lib/items-tech/table/ship/import-item-handler.tsx @@ -1,139 +1,129 @@ -"use client" - -import { z } from "zod" -import { createShipbuildingImportItem } from "../../service" // 아이템 생성 서버 액션 - -// 아이템 데이터 검증을 위한 Zod 스키마 -const itemSchema = z.object({ - itemCode: z.string().optional(), - workType: z.enum(["기장", "전장", "선실", "배관", "철의", "선체"], { - required_error: "기능(공종)은 필수입니다", - }), - shipTypes: z.string().nullable().optional(), - itemList: z.string().nullable().optional(), -}); - -interface ProcessResult { - successCount: number; - errorCount: number; - errors: Array<{ row: number; message: string; itemCode?: string; workType?: string }>; -} - -/** - * Excel 파일에서 가져온 조선 아이템 데이터 처리하는 함수 - */ -export async function processFileImport( - jsonData: Record<string, unknown>[], - progressCallback?: (current: number, total: number) => void -): Promise<ProcessResult> { - // 결과 카운터 초기화 - let successCount = 0; - let errorCount = 0; - const errors: Array<{ row: number; message: string }> = []; - - // 빈 행 등 필터링 - const dataRows = jsonData.filter(row => { - // 빈 행 건너뛰기 - if (Object.values(row).every(val => !val)) { - return false; - } - return true; - }); - - // 데이터 행이 없으면 빈 결과 반환 - if (dataRows.length === 0) { - return { successCount: 0, errorCount: 0, errors: [] }; - } - - // 각 행에 대해 처리 - for (let i = 0; i < dataRows.length; i++) { - const row = dataRows[i]; - const rowIndex = i + 1; // 사용자에게 표시할 행 번호는 1부터 시작 - - // 진행 상황 콜백 호출 - if (progressCallback) { - progressCallback(i + 1, dataRows.length); - } - - try { - // 필드 매핑 (한글/영문 필드명 모두 지원) - const itemCode = row["자재 그룹"] || row["itemCode"] || ""; - const workType = row["기능(공종)"] || row["workType"] || ""; - const shipTypes = row["선종"] || row["shipTypes"] || null; - const itemList = row["자재명"] || row["itemList"] || null; - - // 데이터 정제 - const cleanedRow = { - itemCode: typeof itemCode === 'string' ? itemCode.trim() : String(itemCode).trim(), - workType: typeof workType === 'string' ? workType.trim() : String(workType).trim(), - shipTypes: shipTypes ? (typeof shipTypes === 'string' ? shipTypes.trim() : String(shipTypes).trim()) : null, - itemList: itemList ? (typeof itemList === 'string' ? itemList : String(itemList)) : null, - }; - - // 데이터 유효성 검사 - const validationResult = itemSchema.safeParse(cleanedRow); - - if (!validationResult.success) { - const errorMessage = validationResult.error.errors.map( - err => `${err.path.join('.')}: ${err.message}` - ).join(', '); - - errors.push({ - row: rowIndex, - message: errorMessage, - itemCode: cleanedRow.itemCode, - workType: cleanedRow.workType - }); - errorCount++; - continue; - } - - // 아이템 생성 - const result = await createShipbuildingImportItem({ - itemCode: cleanedRow.itemCode, - workType: cleanedRow.workType as "기장" | "전장" | "선실" | "배관" | "철의" | "선체", - shipTypes: cleanedRow.shipTypes, - itemList: cleanedRow.itemList, - }); - - if (result.success || !result.error) { - successCount++; - } else { - errors.push({ - row: rowIndex, - message: result.message || result.error || "알 수 없는 오류", - itemCode: cleanedRow.itemCode, - workType: cleanedRow.workType - }); - errorCount++; - } - - } catch (error) { - console.error(`${rowIndex}행 처리 오류:`, error); - - // cleanedRow가 정의되지 않은 경우를 처리 - const itemCode = row["자재 그룹"] || row["itemCode"] || ""; - const workType = row["기능(공종)"] || row["workType"] || ""; - - errors.push({ - row: rowIndex, - message: error instanceof Error ? error.message : "알 수 없는 오류", - itemCode: typeof itemCode === 'string' ? itemCode.trim() : String(itemCode).trim(), - workType: typeof workType === 'string' ? workType.trim() : String(workType).trim() - }); - errorCount++; - } - - // 비동기 작업 쓰로틀링 - if (i % 5 === 0) { - await new Promise(resolve => setTimeout(resolve, 10)); - } - } - - // 처리 결과 반환 - return { - successCount, - errorCount, - errors - }; +"use client"
+
+import { z } from "zod"
+import { createShipbuildingImportItem } from "../../service" // 아이템 생성 서버 액션
+
+// 아이템 데이터 검증을 위한 Zod 스키마
+const itemSchema = z.object({
+ itemCode: z.string().optional(),
+ workType: z.enum(["기장", "전장", "선실", "배관", "철의", "선체"], {
+ required_error: "기능(공종)은 필수입니다",
+ }),
+ shipTypes: z.string().nullable().optional(),
+ itemList: z.string().nullable().optional(),
+});
+
+interface ProcessResult {
+ successCount: number;
+ errorCount: number;
+ errors: Array<{ row: number; message: string }>;
+}
+
+/**
+ * Excel 파일에서 가져온 조선 아이템 데이터 처리하는 함수
+ */
+export async function processFileImport(
+ jsonData: Record<string, unknown>[],
+ progressCallback?: (current: number, total: number) => void
+): Promise<ProcessResult> {
+ // 결과 카운터 초기화
+ let successCount = 0;
+ let errorCount = 0;
+ const errors: Array<{ row: number; message: string }> = [];
+
+ // 빈 행 등 필터링
+ const dataRows = jsonData.filter(row => {
+ // 빈 행 건너뛰기
+ if (Object.values(row).every(val => !val)) {
+ return false;
+ }
+ return true;
+ });
+
+ // 데이터 행이 없으면 빈 결과 반환
+ if (dataRows.length === 0) {
+ return { successCount: 0, errorCount: 0, errors: [] };
+ }
+
+ // 각 행에 대해 처리
+ for (let i = 0; i < dataRows.length; i++) {
+ const row = dataRows[i];
+ const rowIndex = i + 1; // 사용자에게 표시할 행 번호는 1부터 시작
+
+ // 진행 상황 콜백 호출
+ if (progressCallback) {
+ progressCallback(i + 1, dataRows.length);
+ }
+
+ try {
+ // 필드 매핑 (한글/영문 필드명 모두 지원)
+ const itemCode = row["자재 그룹"] || row["itemCode"] || "";
+ const workType = row["기능(공종)"] || row["workType"] || "";
+ const shipTypes = row["선종"] || row["shipTypes"] || null;
+ const itemList = row["자재명"] || row["itemList"] || null;
+
+ // 데이터 정제
+ const cleanedRow = {
+ itemCode: typeof itemCode === 'string' ? itemCode.trim() : String(itemCode).trim(),
+ workType: typeof workType === 'string' ? workType.trim() : String(workType).trim(),
+ shipTypes: shipTypes ? (typeof shipTypes === 'string' ? shipTypes.trim() : String(shipTypes).trim()) : null,
+ itemList: itemList ? (typeof itemList === 'string' ? itemList : String(itemList)) : null,
+ };
+
+ // 데이터 유효성 검사
+ const validationResult = itemSchema.safeParse(cleanedRow);
+
+ if (!validationResult.success) {
+ const errorMessage = validationResult.error.errors.map(
+ err => `${err.path.join('.')}: ${err.message}`
+ ).join(', ');
+
+ errors.push({
+ row: rowIndex,
+ message: errorMessage,
+ });
+ errorCount++;
+ continue;
+ }
+
+ // 아이템 생성
+ const result = await createShipbuildingImportItem({
+ itemCode: cleanedRow.itemCode,
+ workType: cleanedRow.workType as "기장" | "전장" | "선실" | "배관" | "철의" | "선체",
+ shipTypes: cleanedRow.shipTypes,
+ itemList: cleanedRow.itemList,
+ });
+
+ if (result.success || !result.error) {
+ successCount++;
+ } else {
+ errors.push({
+ row: rowIndex,
+ message: result.message || result.error || "알 수 없는 오류",
+ });
+ errorCount++;
+ }
+
+ } catch (error) {
+ console.error(`${rowIndex}행 처리 오류:`, error);
+
+ errors.push({
+ row: rowIndex,
+ message: error instanceof Error ? error.message : "알 수 없는 오류",
+ });
+ errorCount++;
+ }
+
+ // 비동기 작업 쓰로틀링
+ if (i % 5 === 0) {
+ await new Promise(resolve => setTimeout(resolve, 10));
+ }
+ }
+
+ // 처리 결과 반환
+ return {
+ successCount,
+ errorCount,
+ errors
+ };
}
\ No newline at end of file diff --git a/lib/items-tech/table/ship/item-excel-template.tsx b/lib/items-tech/table/ship/item-excel-template.tsx index 401fb911..fdff0de0 100644 --- a/lib/items-tech/table/ship/item-excel-template.tsx +++ b/lib/items-tech/table/ship/item-excel-template.tsx @@ -1,111 +1,111 @@ -import * as ExcelJS from 'exceljs'; -import { saveAs } from "file-saver"; - -/** - * 조선 아이템 데이터 가져오기를 위한 Excel 템플릿 파일 생성 및 다운로드 - */ -export async function exportItemTemplate() { - // 워크북 생성 - const workbook = new ExcelJS.Workbook(); - workbook.creator = 'Shipbuilding Item Management System'; - workbook.created = new Date(); - - // 워크시트 생성 - const worksheet = workbook.addWorksheet('조선 아이템'); - - // 컬럼 헤더 정의 및 스타일 적용 - worksheet.columns = [ - { header: '자재 그룹', key: 'itemCode', width: 15 }, - { header: '기능(공종)', key: 'workType', width: 15 }, - { header: '선종', key: 'shipTypes', width: 15 }, - { header: '자재명', key: 'itemList', width: 30 }, - ]; - - // 헤더 스타일 적용 - const headerRow = worksheet.getRow(1); - headerRow.font = { bold: true }; - headerRow.fill = { - type: 'pattern', - pattern: 'solid', - fgColor: { argb: 'FFE0E0E0' } - }; - headerRow.alignment = { vertical: 'middle', horizontal: 'center' }; - - // 테두리 스타일 적용 - headerRow.eachCell((cell) => { - cell.border = { - top: { style: 'thin' }, - left: { style: 'thin' }, - bottom: { style: 'thin' }, - right: { style: 'thin' } - }; - }); - - // 샘플 데이터 추가 - const sampleData = [ - { - itemCode: 'BG0001', - workType: '기장', - shipTypes: 'A-MAX', - itemList: '자재명', - }, - { - itemCode: 'BG0002', - workType: '전장', - shipTypes: 'LNGC', - itemList: '자재명', - }, - { - itemCode: 'BG0003', - workType: '선실', - shipTypes: 'VLCC', - itemList: '자재명', - } - ]; - - // 데이터 행 추가 - sampleData.forEach(item => { - worksheet.addRow(item); - }); - - // 데이터 행 스타일 적용 - worksheet.eachRow((row, rowNumber) => { - if (rowNumber > 1) { // 헤더를 제외한 데이터 행 - row.eachCell((cell) => { - cell.border = { - top: { style: 'thin' }, - left: { style: 'thin' }, - bottom: { style: 'thin' }, - right: { style: 'thin' } - }; - }); - } - }); - - // 워크시트 보호 (선택적) - worksheet.protect('', { - selectLockedCells: true, - selectUnlockedCells: true, - formatColumns: true, - formatRows: true, - insertColumns: false, - insertRows: true, - insertHyperlinks: false, - deleteColumns: false, - deleteRows: true, - sort: true, - autoFilter: true, - pivotTables: false - }); - - try { - // 워크북을 Blob으로 변환 - const buffer = await workbook.xlsx.writeBuffer(); - const blob = new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }); - saveAs(blob, 'shipbuilding-item-template.xlsx'); - return true; - } catch (error) { - console.error('Excel 템플릿 생성 오류:', error); - throw error; - } +import * as ExcelJS from 'exceljs';
+import { saveAs } from "file-saver";
+
+/**
+ * 조선 아이템 데이터 가져오기를 위한 Excel 템플릿 파일 생성 및 다운로드
+ */
+export async function exportItemTemplate() {
+ // 워크북 생성
+ const workbook = new ExcelJS.Workbook();
+ workbook.creator = 'Shipbuilding Item Management System';
+ workbook.created = new Date();
+
+ // 워크시트 생성
+ const worksheet = workbook.addWorksheet('조선 아이템');
+
+ // 컬럼 헤더 정의 및 스타일 적용
+ worksheet.columns = [
+ { header: '자재 그룹', key: 'itemCode', width: 15 },
+ { header: '기능(공종)', key: 'workType', width: 15 },
+ { header: '선종', key: 'shipTypes', width: 15 },
+ { header: '자재명', key: 'itemList', width: 30 },
+ ];
+
+ // 헤더 스타일 적용
+ const headerRow = worksheet.getRow(1);
+ headerRow.font = { bold: true };
+ headerRow.fill = {
+ type: 'pattern',
+ pattern: 'solid',
+ fgColor: { argb: 'FFE0E0E0' }
+ };
+ headerRow.alignment = { vertical: 'middle', horizontal: 'center' };
+
+ // 테두리 스타일 적용
+ headerRow.eachCell((cell) => {
+ cell.border = {
+ top: { style: 'thin' },
+ left: { style: 'thin' },
+ bottom: { style: 'thin' },
+ right: { style: 'thin' }
+ };
+ });
+
+ // 샘플 데이터 추가
+ const sampleData = [
+ {
+ itemCode: 'BG0001',
+ workType: '기장',
+ shipTypes: 'A-MAX',
+ itemList: '자재명',
+ },
+ {
+ itemCode: 'BG0002',
+ workType: '전장',
+ shipTypes: 'LNGC',
+ itemList: '자재명',
+ },
+ {
+ itemCode: 'BG0003',
+ workType: '선실',
+ shipTypes: 'VLCC',
+ itemList: '자재명',
+ }
+ ];
+
+ // 데이터 행 추가
+ sampleData.forEach(item => {
+ worksheet.addRow(item);
+ });
+
+ // 데이터 행 스타일 적용
+ worksheet.eachRow((row, rowNumber) => {
+ if (rowNumber > 1) { // 헤더를 제외한 데이터 행
+ row.eachCell((cell) => {
+ cell.border = {
+ top: { style: 'thin' },
+ left: { style: 'thin' },
+ bottom: { style: 'thin' },
+ right: { style: 'thin' }
+ };
+ });
+ }
+ });
+
+ // 워크시트 보호 (선택적)
+ worksheet.protect('', {
+ selectLockedCells: true,
+ selectUnlockedCells: true,
+ formatColumns: true,
+ formatRows: true,
+ insertColumns: false,
+ insertRows: true,
+ insertHyperlinks: false,
+ deleteColumns: false,
+ deleteRows: true,
+ sort: true,
+ autoFilter: true,
+ pivotTables: false
+ });
+
+ try {
+ // 워크북을 Blob으로 변환
+ const buffer = await workbook.xlsx.writeBuffer();
+ const blob = new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
+ saveAs(blob, 'shipbuilding-item-template.xlsx');
+ return true;
+ } catch (error) {
+ console.error('Excel 템플릿 생성 오류:', error);
+ throw error;
+ }
}
\ No newline at end of file diff --git a/lib/items-tech/table/ship/items-table-toolbar-actions.tsx b/lib/items-tech/table/ship/items-table-toolbar-actions.tsx index 29995327..82ceb298 100644 --- a/lib/items-tech/table/ship/items-table-toolbar-actions.tsx +++ b/lib/items-tech/table/ship/items-table-toolbar-actions.tsx @@ -1,177 +1,177 @@ -"use client" - -import * as React from "react" -import { type Table } from "@tanstack/react-table" -import { Download, FileDown } from "lucide-react" -import * as ExcelJS from 'exceljs' -import { saveAs } from "file-saver" - -import { Button } from "@/components/ui/button" -import { - DropdownMenu, - DropdownMenuContent, - DropdownMenuItem, - DropdownMenuTrigger, -} from "@/components/ui/dropdown-menu" - -import { DeleteItemsDialog } from "../delete-items-dialog" -import { AddItemDialog } from "../add-items-dialog" -import { exportItemTemplate } from "./item-excel-template" -import { ImportItemButton } from "../import-excel-button" - -// 조선 아이템 타입 정의 -interface ShipbuildingItem { - id: number; - itemId: number; - workType: "기장" | "전장" | "선실" | "배관" | "철의" | "선체"; - shipTypes: string; - itemCode: string; - itemName: string; - itemList: string | null; - description: string | null; - createdAt: Date; - updatedAt: Date; -} - -interface ItemsTableToolbarActionsProps { - table: Table<ShipbuildingItem> -} - -export function ItemsTableToolbarActions({ table }: ItemsTableToolbarActionsProps) { - const [refreshKey, setRefreshKey] = React.useState(0) - - // 가져오기 성공 후 테이블 갱신 - const handleImportSuccess = () => { - setRefreshKey(prev => prev + 1) - } - - // Excel 내보내기 함수 - const exportTableToExcel = async ( - table: Table<ShipbuildingItem>, - options: { - filename: string; - excludeColumns?: string[]; - sheetName?: string; - } - ) => { - const { filename, excludeColumns = [], sheetName = "조선 아이템 목록" } = options; - - // 워크북 생성 - const workbook = new ExcelJS.Workbook(); - workbook.creator = 'Shipbuilding Item Management System'; - workbook.created = new Date(); - - // 워크시트 생성 - const worksheet = workbook.addWorksheet(sheetName); - - // 테이블 데이터 가져오기 - const data = table.getFilteredRowModel().rows.map(row => row.original); - console.log("내보내기 데이터:", data); - - // 필요한 헤더 직접 정의 (필터링 문제 해결) - const headers = [ - { key: 'itemCode', header: '자재 그룹' }, - { key: 'workType', header: '기능(공종)' }, - { key: 'shipTypes', header: '선종' }, - { key: 'itemList', header: '자재명' }, - { key: 'subItemList', header: '자재명(상세)' }, - ].filter(header => !excludeColumns.includes(header.key)); - - console.log("내보내기 헤더:", headers); - // 컬럼 정의 - worksheet.columns = headers.map(header => ({ - header: header.header, - key: header.key, - width: 20 // 기본 너비 - })); - - // 스타일 적용 - const headerRow = worksheet.getRow(1); - headerRow.font = { bold: true }; - headerRow.fill = { - type: 'pattern', - pattern: 'solid', - fgColor: { argb: 'FFE0E0E0' } - }; - headerRow.alignment = { vertical: 'middle', horizontal: 'center' }; - - // 데이터 행 추가 - data.forEach(item => { - const row: Record<string, any> = {}; - headers.forEach(header => { - row[header.key] = item[header.key as keyof ShipbuildingItem]; - }); - worksheet.addRow(row); - }); - - // 전체 셀에 테두리 추가 - worksheet.eachRow((row) => { - row.eachCell((cell) => { - cell.border = { - top: { style: 'thin' }, - left: { style: 'thin' }, - bottom: { style: 'thin' }, - right: { style: 'thin' } - }; - }); - }); - - try { - // 워크북을 Blob으로 변환 - const buffer = await workbook.xlsx.writeBuffer(); - const blob = new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }); - saveAs(blob, `${filename}.xlsx`); - return true; - } catch (error) { - console.error("Excel 내보내기 오류:", error); - return false; - } - } - - return ( - <div className="flex items-center gap-2"> - {/* 선택된 로우가 있으면 삭제 다이얼로그 */} - {table.getFilteredSelectedRowModel().rows.length > 0 ? ( - <DeleteItemsDialog - items={table.getFilteredSelectedRowModel().rows.map((row) => row.original) as any} - onSuccess={() => table.toggleAllRowsSelected(false)} - itemType="shipbuilding" - /> - ) : null} - - {/* 새 아이템 추가 다이얼로그 */} - <AddItemDialog itemType="shipbuilding" /> - - {/* Import 버튼 */} - <ImportItemButton itemType="ship" onSuccess={handleImportSuccess} /> - - {/* Export 드롭다운 메뉴 */} - <DropdownMenu> - <DropdownMenuTrigger asChild> - <Button variant="outline" size="sm" className="gap-2"> - <Download className="size-4" aria-hidden="true" /> - <span className="hidden sm:inline">Export</span> - </Button> - </DropdownMenuTrigger> - <DropdownMenuContent align="end"> - <DropdownMenuItem - onClick={() => - exportTableToExcel(table, { - filename: "shipbuilding_items", - excludeColumns: ["select", "actions"], - sheetName: "조선 아이템 목록" - }) - } - > - <FileDown className="mr-2 h-4 w-4" /> - <span>현재 데이터 내보내기</span> - </DropdownMenuItem> - <DropdownMenuItem onClick={() => exportItemTemplate()}> - <FileDown className="mr-2 h-4 w-4" /> - <span>템플릿 다운로드</span> - </DropdownMenuItem> - </DropdownMenuContent> - </DropdownMenu> - </div> - ) +"use client"
+
+import * as React from "react"
+import { type Table } from "@tanstack/react-table"
+import { Download, FileDown } from "lucide-react"
+import * as ExcelJS from 'exceljs'
+import { saveAs } from "file-saver"
+
+import { Button } from "@/components/ui/button"
+import {
+ DropdownMenu,
+ DropdownMenuContent,
+ DropdownMenuItem,
+ DropdownMenuTrigger,
+} from "@/components/ui/dropdown-menu"
+
+import { DeleteItemsDialog } from "../delete-items-dialog"
+import { AddItemDialog } from "../add-items-dialog"
+import { exportItemTemplate } from "./item-excel-template"
+import { ImportItemButton } from "../import-excel-button"
+
+// 조선 아이템 타입 정의
+interface ShipbuildingItem {
+ id: number;
+ itemId: number;
+ workType: "기장" | "전장" | "선실" | "배관" | "철의" | "선체";
+ shipTypes: string;
+ itemCode: string;
+ itemName: string;
+ itemList: string | null;
+ description: string | null;
+ createdAt: Date;
+ updatedAt: Date;
+}
+
+interface ItemsTableToolbarActionsProps {
+ table: Table<ShipbuildingItem>
+}
+
+export function ItemsTableToolbarActions({ table }: ItemsTableToolbarActionsProps) {
+ const [refreshKey, setRefreshKey] = React.useState(0)
+
+ // 가져오기 성공 후 테이블 갱신
+ const handleImportSuccess = () => {
+ setRefreshKey(prev => prev + 1)
+ }
+
+ // Excel 내보내기 함수
+ const exportTableToExcel = async (
+ table: Table<ShipbuildingItem>,
+ options: {
+ filename: string;
+ excludeColumns?: string[];
+ sheetName?: string;
+ }
+ ) => {
+ const { filename, excludeColumns = [], sheetName = "조선 아이템 목록" } = options;
+
+ // 워크북 생성
+ const workbook = new ExcelJS.Workbook();
+ workbook.creator = 'Shipbuilding Item Management System';
+ workbook.created = new Date();
+
+ // 워크시트 생성
+ const worksheet = workbook.addWorksheet(sheetName);
+
+ // 테이블 데이터 가져오기
+ const data = table.getFilteredRowModel().rows.map(row => row.original);
+ console.log("내보내기 데이터:", data);
+
+ // 필요한 헤더 직접 정의 (필터링 문제 해결)
+ const headers = [
+ { key: 'itemCode', header: '자재 그룹' },
+ { key: 'workType', header: '기능(공종)' },
+ { key: 'shipTypes', header: '선종' },
+ { key: 'itemList', header: '자재명' },
+ { key: 'subItemList', header: '자재명(상세)' },
+ ].filter(header => !excludeColumns.includes(header.key));
+
+ console.log("내보내기 헤더:", headers);
+ // 컬럼 정의
+ worksheet.columns = headers.map(header => ({
+ header: header.header,
+ key: header.key,
+ width: 20 // 기본 너비
+ }));
+
+ // 스타일 적용
+ const headerRow = worksheet.getRow(1);
+ headerRow.font = { bold: true };
+ headerRow.fill = {
+ type: 'pattern',
+ pattern: 'solid',
+ fgColor: { argb: 'FFE0E0E0' }
+ };
+ headerRow.alignment = { vertical: 'middle', horizontal: 'center' };
+
+ // 데이터 행 추가
+ data.forEach(item => {
+ const row: Record<string, any> = {};
+ headers.forEach(header => {
+ row[header.key] = item[header.key as keyof ShipbuildingItem];
+ });
+ worksheet.addRow(row);
+ });
+
+ // 전체 셀에 테두리 추가
+ worksheet.eachRow((row) => {
+ row.eachCell((cell) => {
+ cell.border = {
+ top: { style: 'thin' },
+ left: { style: 'thin' },
+ bottom: { style: 'thin' },
+ right: { style: 'thin' }
+ };
+ });
+ });
+
+ try {
+ // 워크북을 Blob으로 변환
+ const buffer = await workbook.xlsx.writeBuffer();
+ const blob = new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
+ saveAs(blob, `${filename}.xlsx`);
+ return true;
+ } catch (error) {
+ console.error("Excel 내보내기 오류:", error);
+ return false;
+ }
+ }
+
+ return (
+ <div className="flex items-center gap-2">
+ {/* 선택된 로우가 있으면 삭제 다이얼로그 */}
+ {table.getFilteredSelectedRowModel().rows.length > 0 ? (
+ <DeleteItemsDialog
+ items={table.getFilteredSelectedRowModel().rows.map((row) => row.original) as any}
+ onSuccess={() => table.toggleAllRowsSelected(false)}
+ itemType="shipbuilding"
+ />
+ ) : null}
+
+ {/* 새 아이템 추가 다이얼로그 */}
+ <AddItemDialog itemType="shipbuilding" />
+
+ {/* Import 버튼 */}
+ <ImportItemButton itemType="ship" onSuccess={handleImportSuccess} />
+
+ {/* Export 드롭다운 메뉴 */}
+ <DropdownMenu>
+ <DropdownMenuTrigger asChild>
+ <Button variant="outline" size="sm" className="gap-2">
+ <Download className="size-4" aria-hidden="true" />
+ <span className="hidden sm:inline">Export</span>
+ </Button>
+ </DropdownMenuTrigger>
+ <DropdownMenuContent align="end">
+ <DropdownMenuItem
+ onClick={() =>
+ exportTableToExcel(table, {
+ filename: "shipbuilding_items",
+ excludeColumns: ["select", "actions"],
+ sheetName: "조선 아이템 목록"
+ })
+ }
+ >
+ <FileDown className="mr-2 h-4 w-4" />
+ <span>현재 데이터 내보내기</span>
+ </DropdownMenuItem>
+ <DropdownMenuItem onClick={() => exportItemTemplate()}>
+ <FileDown className="mr-2 h-4 w-4" />
+ <span>템플릿 다운로드</span>
+ </DropdownMenuItem>
+ </DropdownMenuContent>
+ </DropdownMenu>
+ </div>
+ )
}
\ No newline at end of file |
