// lib/export-ocr-data.ts import ExcelJS from "exceljs" import type { OcrRow } from "@/db/schema" /** * OCR 데이터를 엑셀로 내보내는 단순한 함수 */ export async function exportOcrDataToExcel( data: OcrRow[], filename: string = "OCR Data" ): Promise { if (!data || data.length === 0) { throw new Error("No data to export") } // 워크북 생성 const workbook = new ExcelJS.Workbook() const worksheet = workbook.addWorksheet("OCR Data") // 컬럼 정의 (OCR 데이터에 맞게 설정) const columns = [ { key: "fileName", header: "file Name", width: 20 }, { key: "inspectionDate", header: "inspection Date", width: 20 }, { key: "reportNo", header: "Report No", width: 15 }, { key: "no", header: "No", width: 10 }, { key: "identificationNo", header: "Identification No", width: 20 }, { key: "tagNo", header: "Tag No", width: 15 }, { key: "jointNo", header: "Joint No", width: 15 }, { key: "jointType", header: "Joint Type", width: 15 }, { key: "weldingDate", header: "Welding Date", width: 15 }, { key: "confidence", header: "confidence", width: 15 }, { key: "userName", header: "사용자 이름", width: 15 }, { key: "userEmail", header: "사용자 이메일", width: 15 }, { key: "createdAt", header: "생성일", width: 20 }, // 필요한 다른 컬럼들도 추가 가능 ] // 워크시트에 컬럼 설정 worksheet.columns = columns // 헤더 스타일 설정 const headerRow = worksheet.getRow(1) headerRow.font = { bold: true } headerRow.alignment = { horizontal: "center" } headerRow.fill = { type: "pattern", pattern: "solid", fgColor: { argb: "FFCCCCCC" }, } // 데이터 추가 data.forEach((row) => { worksheet.addRow({ reportNo: row.reportNo || "", no: row.no || "", identificationNo: row.identificationNo || "", inspectionDate: row.inspectionDate ? new Date(row.inspectionDate).toLocaleDateString() : "", fileName: row.fileName || "", tagNo: row.tagNo || "", jointNo: row.jointNo || "", jointType: row.jointType || "", confidence: row.confidence || "", weldingDate: row.weldingDate ? new Date(row.weldingDate).toLocaleDateString() : "", userName: row.userName || "", userEmail: row.userEmail || "", createdAt: row.createdAt ? new Date(row.createdAt).toLocaleString() : "", }) }) // 모든 행에 테두리 추가 worksheet.eachRow((row, rowNumber) => { row.eachCell((cell) => { cell.border = { top: { style: "thin" }, left: { style: "thin" }, bottom: { style: "thin" }, right: { style: "thin" }, } }) }) // 파일 다운로드 const buffer = await workbook.xlsx.writeBuffer() const blob = new Blob([buffer], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", }) const url = URL.createObjectURL(blob) const link = document.createElement("a") link.href = url link.download = `${filename}.xlsx` link.click() URL.revokeObjectURL(url) }