본문으로 건너뛰기Skip to content
예제 첫화면Examples home

엑셀 머리글을 컬럼에 이어 읽기Map Excel headers to grid columns

파일의 머리글이 그리드 컬럼과 달라도 columns 옵션으로 이어 읽고, 시트와 범위(range)를 골라요.Link file headers that differ from the grid columns with the columns option, and choose the sheet and the range.

활용Intermediate
직접 써 보기Live preview엑셀 머리글을 컬럼에 이어 읽기Map Excel headers to grid columns

코드Code

탭을 골라 코드를 복사하세요. 위 화면은 이 코드를 그대로 실행한 결과예요.Pick a tab and copy the code. The preview above runs exactly this code.

이 화면은 새 모양 옵션을 켠 모양이에요: density: 'spacious', iconStyle: 'soft', motion: true (코드에 없으면 기본 모양으로 보여요)This preview has the new-look options on: density: 'spacious', iconStyle: 'soft', motion: true (without them in your code you get the default look)

빌드 도구(Vite 등)를 쓰는 프로젝트용 모듈 파일이에요. 설치 없이 바로 돌려 보려면 HTML 탭이나 직접 해보기를 쓰세요.

이 예제는 추가 패키지가 필요해요. 내 컴퓨터에서 돌리려면 npm i maxgrid @maxgrid/export로 설치하세요.

버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.

index.html
<div id="app">
  <div class="toolbar">
    <select aria-label="시트" id="sheet">
      <option value="orders" selected>주문</option>
      <option value="notes">메모</option>
    </select>
    <select aria-label="머리글 이어 주기" id="map">
      <option value="yes" selected>함</option>
      <option value="no">안 함</option>
    </select>
    <input
      type="text"
      aria-label="범위"
      placeholder="범위 (비우면 전체, 예: A1:C3)"
      id="range"
    />
    <button type="button" id="run">읽기</button>
  </div>
  <div id="grid" style="height: 320px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('sheet').addEventListener('change', (event) => actions.sheet(api, event.target.value));
document.getElementById('map').addEventListener('change', (event) => actions.map(api, event.target.value));
document.getElementById('range').addEventListener('input', (event) => actions.range(api, event.target.value));
document.getElementById('run').addEventListener('click', () => actions.run(api));
example.js
import { exportXlsx, withExport, xlsxSheet } from '@maxgrid/export';
// 파일의 머리글("주문번호", "거래처"…)이 그리드 컬럼과 달라도, columns 옵션으로 머리글 → 컬럼 id를 이어 줄 수 있어요.
export const options = {
  columns: [
    { id: 'order', title: '주문', width: 100 },
    { id: 'customer', title: '거래처', width: 130 },
    { id: 'qty', title: '수량', kind: 'number', width: 80 },
    { id: 'ordered', title: '주문일', kind: 'date', width: 110 },
  ],
  rows: [{ order: 'SO-0000', customer: '(예전 행)', qty: 0, ordered: null }],
  rowKey: 'order',
};
const MAP = { 주문번호: 'order', 거래처명: 'customer', 주문수량: 'qty', 주문일자: 'ordered' };
// 두 시트짜리 파일을 그 자리에서 만들어요: "주문" 시트(데이터)와 "메모" 시트.
function sample() {
  return exportXlsx({
    columns: [
      { id: 'order', title: '주문번호' },
      { id: 'customer', title: '거래처명' },
      { id: 'qty', title: '주문수량', kind: 'number' },
      { id: 'ordered', title: '주문일자', kind: 'date' },
    ],
    rows: [
      { order: 'SO-3001', customer: '한빛푸드', qty: 12, ordered: '2026-10-01' },
      { order: 'SO-3002', customer: '파란마켓', qty: 4, ordered: '2026-10-03' },
      { order: 'SO-3003', customer: '단풍상회', qty: 30, ordered: '2026-10-05' },
      { order: 'SO-3004', customer: '바다유통', qty: 7, ordered: '2026-10-08' },
    ],
  }, {
    sheetName: '주문',
    sheets: [
      xlsxSheet('메모', {
        columns: [{ id: 'note', title: '메모' }],
        rows: [{ note: '분기 마감 전 확인' }],
      }),
    ],
  });
}
let sheet = '주문';
let mapped = true;
let range = '';
let say = () => { };
let ex;
export function setup(api, log) {
  say = log;
  ex = withExport(api);
  log({
    ko: '파일의 머리글은 "주문번호" 같은 이름이라 그리드 컬럼 id와 달라요. columns 옵션으로 이어 주거나 범위(range)·시트(sheet)를 골라 읽어요.',
    en: 'The file headers are names like "주문번호", different from the grid column ids. Link them with the columns option, or pick the range and the sheet.',
  });
}
export const actions = {
  sheet: (_api, v) => void (sheet = v === 'notes' ? '메모' : '주문'),
  map: (_api, v) => void (mapped = v !== 'no'),
  range: (_api, v) => void (range = (v ?? '').trim()),
  run: async () => {
    try {
      const r = await ex?.importXlsx(sample(), {
        sheet,
        ...(mapped ? { columns: MAP } : {}),
        ...(range === '' ? {} : { range }),
      });
      if (r)
        say({
          ko: `${r.count}행을 읽었어요. 머리글: ${r.columns.join(', ')}`,
          en: `Read ${r.count} rows. Headers: ${r.columns.join(', ')}`,
        });
    }
    catch (error) {
      // 메모 시트에는 주문 키 열이 없어요: 엔진 문구 대신 무엇을 고칠지 말해 줘요.
      say({
        ko: sheet === '메모'
          ? '이 시트에는 주문번호 열이 없어요. "주문" 시트를 고르세요.'
          : `읽지 못했어요: ${error.message}`,
        en: sheet === '메모'
          ? 'This sheet has no order-number column. Pick the Orders sheet.'
          : `Could not read: ${error.message}`,
      });
    }
  },
};

Module files for a project with a bundler such as Vite. To run it with no setup, use the HTML tab or Try it.

This example needs an extra package. To run it locally, install with npm i maxgrid @maxgrid/export.

When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.

index.html
<div id="app">
  <div class="toolbar">
    <select aria-label="Sheet" id="sheet">
      <option value="orders" selected>Orders</option>
      <option value="notes">Notes</option>
    </select>
    <select aria-label="Header mapping" id="map">
      <option value="yes" selected>On</option>
      <option value="no">Off</option>
    </select>
    <input
      type="text"
      aria-label="Range"
      placeholder="Range (blank = all, e.g. A1:C3)"
      id="range"
    />
    <button type="button" id="run">Read</button>
  </div>
  <div id="grid" style="height: 320px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('sheet').addEventListener('change', (event) => actions.sheet(api, event.target.value));
document.getElementById('map').addEventListener('change', (event) => actions.map(api, event.target.value));
document.getElementById('range').addEventListener('input', (event) => actions.range(api, event.target.value));
document.getElementById('run').addEventListener('click', () => actions.run(api));
example.js
import { exportXlsx, withExport, xlsxSheet } from '@maxgrid/export';
// Even when the file headers ("Order No", "Client"...) differ from the grid columns, the columns option links header text to column id.
export const options = {
  columns: [
    { id: 'order', title: 'Order', width: 100 },
    { id: 'customer', title: 'Customer', width: 130 },
    { id: 'qty', title: 'Qty', kind: 'number', width: 80 },
    { id: 'ordered', title: 'Ordered', kind: 'date', width: 110 },
  ],
  rows: [{ order: 'SO-0000', customer: '(old row)', qty: 0, ordered: null }],
  rowKey: 'order',
};
const MAP = {
  'Order No': 'order',
  Client: 'customer',
  Quantity: 'qty',
  'Order date': 'ordered',
};
// Builds a two-sheet file on the spot: an "Orders" sheet (the data) and a "Notes" sheet.
function sample() {
  return exportXlsx({
    columns: [
      { id: 'order', title: 'Order No' },
      { id: 'customer', title: 'Client' },
      { id: 'qty', title: 'Quantity', kind: 'number' },
      { id: 'ordered', title: 'Order date', kind: 'date' },
    ],
    rows: [
      { order: 'SO-3001', customer: 'Harbor Foods', qty: 12, ordered: '2026-10-01' },
      { order: 'SO-3002', customer: 'Blue Market', qty: 4, ordered: '2026-10-03' },
      { order: 'SO-3003', customer: 'Maple Trading', qty: 30, ordered: '2026-10-05' },
      { order: 'SO-3004', customer: 'Ocean Supply', qty: 7, ordered: '2026-10-08' },
    ],
  }, {
    sheetName: 'Orders',
    sheets: [
      xlsxSheet('Notes', {
        columns: [{ id: 'note', title: 'Note' }],
        rows: [{ note: 'Check before quarter close' }],
      }),
    ],
  });
}
let sheet = 'Orders';
let mapped = true;
let range = '';
let say = () => { };
let ex;
export function setup(api, log) {
  say = log;
  ex = withExport(api);
  log('The file headers are names like "Order No", different from the grid column ids. Link them with the columns option, or pick the range and the sheet.');
}
export const actions = {
  sheet: (_api, v) => void (sheet = v === 'notes' ? 'Notes' : 'Orders'),
  map: (_api, v) => void (mapped = v !== 'no'),
  range: (_api, v) => void (range = (v ?? '').trim()),
  run: async () => {
    try {
      const r = await ex?.importXlsx(sample(), {
        sheet,
        ...(mapped ? { columns: MAP } : {}),
        ...(range === '' ? {} : { range }),
      });
      if (r)
        say(`Read ${r.count} rows. Headers: ${r.columns.join(', ')}`);
    }
    catch (error) {
      // The Notes sheet has no order key column: say what to fix instead of the engine message.
      say(sheet === 'Notes'
        ? 'This sheet has no order-number column. Pick the Orders sheet.'
        : `Could not read: ${error.message}`);
    }
  },
};