使用 ViteJS 打包表单
ViteJS 是一个用于生成静态站点的现代构建工具。
¥ViteJS is a modern build tool for generating static sites.
SheetJS 是一个用于从电子表格读取和写入数据的 JavaScript 库。
¥SheetJS is a JavaScript library for reading and writing data from spreadsheets.
本 demo 使用 ViteJS 和 SheetJS 导出数据。我们将探讨如何使用 ViteJS 将 SheetJS 添加到网站以及如何将数据导出到电子表格。
¥This demo uses ViteJS and SheetJS to export data. We'll explore how to add SheetJS to a site using ViteJS and how to export data to spreadsheets.
内容演示的 Vite 部分 涵盖资源加载器。它们非常适合静态站点在构建时从工作表中提取数据。
¥The Vite section of the Content demo covers asset loaders. They are ideal for static sites pulling data from sheets at build time.
该演示重点介绍与 ViteJS 打包器的集成细节。
¥This demo focuses on integration details with the ViteJS bundler.
这些演示遵循 "导出教程",其中更详细地介绍了 SheetJS 库的用法。
¥The demos follow the "Export Tutorial", which covers SheetJS library usage in more detail.
本 demo 在以下环境下进行了测试:
¥This demo was tested in the following environments:
ViteJS | 日期 |
---|---|
5.2.10 | 2024-04-27 |
4.5.3 | 2024-04-27 |
3.2.10 | 2024-04-27 |
集成详情
¥Integration Details
"构架" 章节 涵盖了 Yarn 和其他包管理器的安装。
¥The "Frameworks" section covers installation with Yarn and other package managers.
在 ViteJS 项目中安装 SheetJS 模块后,import
语句可以加载库的相关部分。
¥After installing the SheetJS module in a ViteJS project, import
statements
can load relevant parts of the library.
import { read, utils, writeFileXLSX } from 'xlsx';
ViteJS 需要第三方库提供额外的 package.json
元数据。SheetJS 库版本 0.18.10 添加了所需的元数据。
¥ViteJS requires third-party libraries to provide additional package.json
metadata. SheetJS library version 0.18.10 added the required metadata.
强烈推荐给 升级到最新版本
¥It is strongly recommended to upgrade to the latest version
完整示例
¥Complete Example
-
创建一个新的 ViteJS 项目:
¥Create a new ViteJS project:
npm create vite@latest sheetjs-vite -- --template vue-ts
cd sheetjs-vite
npm i
-
添加 SheetJS 依赖:
¥Add the SheetJS dependency:
npm i --save https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz
-
将
src\components\HelloWorld.vue
替换为:¥Replace
src\components\HelloWorld.vue
with:
<script setup lang="ts">
import { version, utils, writeFileXLSX } from 'xlsx';
interface President {
terms: { "type": "prez" | "viceprez"; }[];
name: { first: string; last: string; }
bio: { birthday: string; }
}
async function xport() {
/* fetch JSON data and parse */
const url = "https://xlsx.nodejs.cn/executive.json";
const raw_data: President[] = await (await fetch(url)).json();
/* filter for the Presidents */
const prez = raw_data.filter(row => row.terms.some(term => term.type === "prez"));
/* sort by first presidential term */
prez.forEach(row => row.start = row.terms.find(term => term.type === "prez").start);
prez.sort((l,r) => l.start.localeCompare(r.start));
/* flatten objects */
const rows = prez.map(row => ({
name: row.name.first + " " + row.name.last,
birthday: row.bio.birthday
}));
/* generate worksheet and workbook */
const worksheet = utils.json_to_sheet(rows);
const workbook = utils.book_new();
utils.book_append_sheet(workbook, worksheet, "Dates");
/* fix headers */
utils.sheet_add_aoa(worksheet, [["Name", "Birthday"]], { origin: "A1" });
/* calculate column width */
const max_width = rows.reduce((w, r) => Math.max(w, r.name.length), 10);
worksheet["!cols"] = [ { wch: max_width } ];
/* create an XLSX file and try to save to Presidents.xlsx */
writeFileXLSX(workbook, "Presidents.xlsx");
}
</script>
<template>
<button type="button" @click="xport">Export with SheetJS version {{ version }}</button>
</template>
-
启动开发服务器:
¥Start the development server:
npm run dev
-
打开
http://localhost:5173/
的 Web 浏览器并单击导出按钮。¥Open a web browser to
http://localhost:5173/
and click the export button. -
搭建生产站点:
¥Build the production site:
npx vite build
-
通过在
dist
文件夹中运行本地 Web 服务器来验证新站点:¥Verify the new site by running a local web server in the
dist
folder:
npx http-server dist
-
在 Web 浏览器中访问显示的 URL(通常为
http://localhost:8080
),然后单击导出按钮。¥Access the displayed URL (typically
http://localhost:8080
) in a web browser and click the export button.