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

Excel(.xlsx)로 내려받기: 숫자·날짜 유지Download Excel (.xlsx) keeping numbers and dates

숫자와 날짜가 글자가 아니라 진짜 숫자·날짜로 들어가는 엑셀 파일을 만들어요. 단가를 숫자로 받을지 화면 모양 그대로(글자) 받을지 고를 수 있어요.Make an Excel file where numbers and dates stay real numbers and dates. Choose whether the price column is exported as numbers or exactly as displayed (text).

입문Beginner
직접 써 보기Live previewExcel(.xlsx)로 내려받기: 숫자·날짜 유지Download Excel (.xlsx) keeping numbers and dates

코드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="price">
      <option value="number" selected>숫자로 (합계가 계산돼요)</option>
      <option value="text">화면 모양 그대로 (4,200 글자)</option>
    </select>
    <button type="button" id="xlsx">xlsx 내려받기</button>
    <button type="button" id="csv">CSV 내려받기</button>
  </div>
  <div id="grid" style="height: 330px"></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('price').addEventListener('change', (event) => actions.price(api, event.target.value));
document.getElementById('xlsx').addEventListener('click', () => actions.xlsx(api));
document.getElementById('csv').addEventListener('click', () => actions.csv(api));
example.js
import { withExport } from '@maxgrid/export';
const customers = ['한빛푸드', '소나무철물', '파란마켓', '단풍상회', '바다유통', '새벽식자재'];
const regions = ['서울', '부산', '대전', '광주'];
const rows = Array.from({ length: 12 }, (_, i) => ({
  id: i + 1,
  orderNo: `SO-${3001 + i}`,
  customer: customers[i % customers.length],
  region: regions[i % regions.length],
  qty: 3 + ((i * 7) % 28),
  price: 4200 + ((i * 1900) % 21000),
  orderedAt: new Date(2026, 9, 1 + i * 2),
}));
// xlsx는 숫자와 날짜를 글자가 아니라 진짜 숫자·날짜로 담아요. Excel에서 합계를 내거나 날짜로 정렬할 수 있어요.
export const options = {
  columns: [
    { id: 'orderNo', title: '주문번호', width: 100 },
    { id: 'customer', title: '거래처', width: 110 },
    { id: 'region', title: '지역', width: 70 },
    { id: 'qty', title: '수량', kind: 'number', width: 70, align: 'end' },
    {
      id: 'price',
      title: '단가(원)',
      kind: 'number',
      width: 100,
      align: 'end',
      formatValue: (v) => Number(v).toLocaleString('ko-KR'),
    },
    { id: 'orderedAt', title: '주문일', kind: 'date', width: 110 },
  ],
  rows,
  rowKey: 'id',
};
let say = () => { };
let asNumber = true;
export function setup(api, log) {
  say = log;
  withExport(api);
  log({
    ko: '단가를 "숫자로" 내려받으면 엑셀에서 합계가 계산돼요. "화면 모양 그대로"는 4,200 같은 글자가 들어가요.',
    en: 'Download the unit price "as a number" and Excel can sum it. "As shown on screen" puts in text such as 4,200.',
  });
}
// 만든 파일 안에서 단가 칸(E2)이 글자(inlineStr)인지 숫자인지 직접 읽어서 알려 줘요.
function priceCellKind(api, applyFormatValue) {
  const text = new TextDecoder('latin1').decode(api.exportXlsx({ applyFormatValue }));
  return /<c r="E2"[^>]*t="inlineStr"/.test(text) ? '글자' : '숫자';
}
export const actions = {
  price: (_api, value) => void (asNumber = value !== 'text'),
  xlsx: async (api) => {
    const ex = withExport(api);
    // formatValue(쉼표 찍힌 모양)를 쓰면 글자로 들어가요. 숫자로 받으려면 끄세요.
    const applyFormatValue = !asNumber;
    await ex.downloadXlsx('주문.xlsx', { applyFormatValue });
    say({
      ko: `주문.xlsx를 내려받았어요. 단가는 ${priceCellKind(ex, applyFormatValue)}로, 수량은 숫자로, 주문일은 날짜로 들어 있어요.`,
      en: `Downloaded 주문.xlsx. The unit price is stored as ${priceCellKind(ex, applyFormatValue) === '글자' ? 'text' : 'a number'}, the quantity as a number and the order date as a date.`,
    });
  },
  csv: async (api) => {
    await withExport(api).downloadCsv('주문.csv');
    say({
      ko: '주문.csv를 내려받았어요. CSV는 모든 값이 글자라서 엑셀이 형식을 추측해요.',
      en: 'Downloaded 주문.csv. In CSV every value is text, so Excel guesses the format.',
    });
  },
};

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="Price column" id="price">
      <option value="number" selected>As numbers (sums work)</option>
      <option value="text">As displayed (text like 4,200)</option>
    </select>
    <button type="button" id="xlsx">Download xlsx</button>
    <button type="button" id="csv">Download CSV</button>
  </div>
  <div id="grid" style="height: 330px"></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('price').addEventListener('change', (event) => actions.price(api, event.target.value));
document.getElementById('xlsx').addEventListener('click', () => actions.xlsx(api));
document.getElementById('csv').addEventListener('click', () => actions.csv(api));
example.js
import { withExport } from '@maxgrid/export';
const customers = [
  'Harbor Foods',
  'Pine Hardware',
  'Blue Market',
  'Maple Trading',
  'Ocean Supply',
  'Dawn Grocers',
];
const regions = ['Seoul', 'Busan', 'Daejeon', 'Gwangju'];
const rows = Array.from({ length: 12 }, (_, i) => ({
  id: i + 1,
  orderNo: `SO-${3001 + i}`,
  customer: customers[i % customers.length],
  region: regions[i % regions.length],
  qty: 3 + ((i * 7) % 28),
  price: 4200 + ((i * 1900) % 21000),
  orderedAt: new Date(2026, 9, 1 + i * 2),
}));
// xlsx stores numbers and dates as real numbers and dates, not text. In Excel you can sum them or sort by date.
export const options = {
  columns: [
    { id: 'orderNo', title: 'Order no.', width: 100 },
    { id: 'customer', title: 'Customer', width: 110 },
    { id: 'region', title: 'Region', width: 80 },
    { id: 'qty', title: 'Qty', kind: 'number', width: 70, align: 'end' },
    {
      id: 'price',
      title: 'Price ($)',
      kind: 'number',
      width: 100,
      align: 'end',
      formatValue: (v) => Number(v).toLocaleString('en-US'),
    },
    { id: 'orderedAt', title: 'Ordered', kind: 'date', width: 110 },
  ],
  rows,
  rowKey: 'id',
};
let say = () => { };
let asNumber = true;
export function setup(api, log) {
  say = log;
  withExport(api);
  log('Download the price "as a number" and Excel can sum it. "As shown on screen" puts in text such as 4,200.');
}
// Read the price cell (E2) of the built file directly and report whether it is text (inlineStr) or a number.
function priceCellKind(api, applyFormatValue) {
  const text = new TextDecoder('latin1').decode(api.exportXlsx({ applyFormatValue }));
  return /<c r="E2"[^>]*t="inlineStr"/.test(text) ? 'text' : 'number';
}
export const actions = {
  price: (_api, value) => void (asNumber = value !== 'text'),
  xlsx: async (api) => {
    const ex = withExport(api);
    // Using formatValue (the comma look) stores text. Turn it off to get numbers.
    const applyFormatValue = !asNumber;
    await ex.downloadXlsx('orders.xlsx', { applyFormatValue });
    say(`Downloaded orders.xlsx. The price is stored as ${priceCellKind(ex, applyFormatValue) === 'text' ? 'text' : 'a number'}, the quantity as a number and the order date as a date.`);
  },
  csv: async (api) => {
    await withExport(api).downloadCsv('orders.csv');
    say('Downloaded orders.csv. In CSV every value is text, so Excel guesses the format.');
  },
};