summaryrefslogtreecommitdiff
path: root/lib/welding/table/exporft-ocr-data.ts
blob: 729b1bebfe83b404ee09d053aafa9250ea947e64 (plain)
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
// 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<void> {
  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)
}