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

한글 Excel에서 안 깨지는 CSV 만들기CSV that opens cleanly in Korean Excel

엑셀에서 한글이 깨지지 않게 하는 설정, 쉼표 찍힌 표시값, 수식으로 읽히는 글자 막기를 바꿔 가며 CSV 내용이 어떻게 달라지는지 미리 봐요.Toggle BOM, displayed values and formula protection and see how the CSV text changes.

활용Intermediate
직접 써 보기Live preview한글 Excel에서 안 깨지는 CSV 만들기CSV that opens cleanly in Korean Excel

코드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="bom">
      <option value="on" selected>켜기 (권장)</option>
      <option value="off">끄기</option>
    </select>
    <select aria-label="값 보여 주기" id="formatted">
      <option value="shown" selected>표시값 (쉼표 있는 모양)</option>
      <option value="raw">원래 값 (쉼표 없이)</option>
    </select>
    <select aria-label="수식으로 읽히는 글자 막기" id="guard">
      <option value="on" selected>막기</option>
      <option value="phone">전화번호 칸만 막지 않기</option>
      <option value="off">막지 않기</option>
    </select>
    <button type="button" id="preview">미리 보기</button>
    <button type="button" id="download">내려받기</button>
  </div>
  <div id="grid" style="height: 200px"></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('bom').addEventListener('change', (event) => actions.bom(api, event.target.value));
document.getElementById('formatted').addEventListener('change', (event) => actions.formatted(api, event.target.value));
document.getElementById('guard').addEventListener('change', (event) => actions.guard(api, event.target.value));
document.getElementById('preview').addEventListener('click', () => actions.preview(api));
document.getElementById('download').addEventListener('click', () => actions.download(api));
example.js
import { withExport } from '@maxgrid/export';
// 한글 Excel에서 CSV가 깨지지 않게 세 가지를 정해요: ① BOM, ② 화면에 보이는 값(쉼표 찍힌 금액),
// ③ =, +, -, @로 시작하는 글자의 수식 방어(Excel이 수식으로 읽는 것을 막아요).
export const options = {
  columns: [
    { id: 'name', title: '이름', width: 90 },
    { id: 'phone', title: '전화번호', width: 130 },
    { id: 'note', title: '메모', width: 160 },
    {
      id: 'amount',
      title: '금액(원)',
      kind: 'number',
      width: 110,
      align: 'end',
      formatValue: (v) => Number(v).toLocaleString('ko-KR'),
    },
  ],
  rows: [
    { id: 1, name: '김서연', phone: '+82-10-2345-6789', note: '일반 고객', amount: 1280000 },
    {
      id: 2,
      name: '이준호',
      phone: '010-9876-5432',
      note: '=1+1 (수식처럼 보이는 메모)',
      amount: 450000,
    },
    { id: 3, name: '박민지', phone: '-', note: '@담당자 확인 필요', amount: 98000 },
  ],
  rowKey: 'id',
};
let bom = true;
let formatted = true;
let guard = 'on';
let say = () => { };
export function setup(api, log) {
  say = log;
  withExport(api);
}
const csvOptions = () => ({
  bom,
  applyFormatValue: formatted,
  newline: '\n',
  sanitizeFormulas: guard === 'phone'
    ? (_text, column) => column.id !== 'phone'
    : guard === 'on',
});
export const actions = {
  bom: (_api, v) => void (bom = v !== 'off'),
  formatted: (_api, v) => void (formatted = v !== 'raw'),
  guard: (_api, v) => void (guard = v ?? 'on'),
  preview: (api) => {
    const text = withExport(api).exportCsv(csvOptions());
    // 기록은 위가 최신이라 거꾸로 적어야 위에서 아래로 읽혀요. 따옴표나 이스케이프 없이 글자 그대로 보여 줘요.
    text
      .split('\n')
      .slice(0, 4)
      .reverse()
      .forEach((line) => say(line.replace('\uFEFF', '')));
    say({
      ko: bom
        ? '파일 맨 앞에 3바이트 표시가 붙어요(엑셀이 한글 파일인 줄 알아봐요).'
        : '맨 앞 표시가 없어요. 한글 엑셀에서 글자가 깨질 수 있어요.',
      en: bom
        ? 'A 3-byte mark is added at the start of the file (Excel recognizes it as a Korean file).'
        : 'No mark at the start. Korean text may be garbled in Korean Excel.',
    });
  },
  download: (api) => void withExport(api).downloadCsv('고객-엑셀용.csv', csvOptions()),
};

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="Keep Korean intact in Excel" id="bom">
      <option value="on" selected>On (recommended)</option>
      <option value="off">Off</option>
    </select>
    <select aria-label="Values" id="formatted">
      <option value="shown" selected>Displayed value (with comma)</option>
      <option value="raw">Original value (no comma)</option>
    </select>
    <select aria-label="Block text read as formulas" id="guard">
      <option value="on" selected>Block</option>
      <option value="phone">Block except phone column</option>
      <option value="off">Do not block</option>
    </select>
    <button type="button" id="preview">Preview</button>
    <button type="button" id="download">Download</button>
  </div>
  <div id="grid" style="height: 200px"></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('bom').addEventListener('change', (event) => actions.bom(api, event.target.value));
document.getElementById('formatted').addEventListener('change', (event) => actions.formatted(api, event.target.value));
document.getElementById('guard').addEventListener('change', (event) => actions.guard(api, event.target.value));
document.getElementById('preview').addEventListener('click', () => actions.preview(api));
document.getElementById('download').addEventListener('click', () => actions.download(api));
example.js
import { withExport } from '@maxgrid/export';
// Three settings keep a CSV intact in Excel: (1) BOM, (2) displayed values (amounts with commas),
// (3) formula protection for text starting with =, +, - or @ (stops Excel from reading it as a formula).
export const options = {
  columns: [
    { id: 'name', title: 'Name', width: 110 },
    { id: 'phone', title: 'Phone', width: 130 },
    { id: 'note', title: 'Note', width: 200 },
    {
      id: 'amount',
      title: 'Amount ($)',
      kind: 'number',
      width: 110,
      align: 'end',
      formatValue: (v) => Number(v).toLocaleString('en-US'),
    },
  ],
  rows: [
    {
      id: 1,
      name: 'Emma Stone',
      phone: '+1-415-555-0132',
      note: 'Regular customer',
      amount: 1280000,
    },
    {
      id: 2,
      name: 'Liam Carter',
      phone: '415-555-0187',
      note: '=1+1 (a note that looks like a formula)',
      amount: 450000,
    },
    { id: 3, name: 'Olivia Reed', phone: '-', note: '@owner needs to confirm', amount: 98000 },
  ],
  rowKey: 'id',
};
let bom = true;
let formatted = true;
let guard = 'on';
let say = () => { };
export function setup(api, log) {
  say = log;
  withExport(api);
}
const csvOptions = () => ({
  bom,
  applyFormatValue: formatted,
  newline: '\n',
  sanitizeFormulas: guard === 'phone'
    ? (_text, column) => column.id !== 'phone'
    : guard === 'on',
});
export const actions = {
  bom: (_api, v) => void (bom = v !== 'off'),
  formatted: (_api, v) => void (formatted = v !== 'raw'),
  guard: (_api, v) => void (guard = v ?? 'on'),
  preview: (api) => {
    const text = withExport(api).exportCsv(csvOptions());
    // The log shows the newest line on top, so write in reverse to read top to bottom. Shown as-is, without quoting or escaping.
    text
      .split('\n')
      .slice(0, 4)
      .reverse()
      .forEach((line) => say(line.replace('', '')));
    say(bom
      ? 'A 3-byte mark is added at the start of the file (Excel recognizes it as UTF-8).'
      : 'No mark at the start. Non-ASCII text may be garbled in Excel.');
  },
  download: (api) => void withExport(api).downloadCsv('customers-excel.csv', csvOptions()),
};