SheetJS CE
SheetJS Community Edition offers battle-tested open-source solutions for extracting useful data from almost any complex spreadsheet and generating new spreadsheets that will work with legacy and modern software alike.SheetJS Community Edition提供久经考验的开源解决方案,可以从几乎任何复杂的电子表格中提取有用的数据,并生成新的电子表格,这些电子表格可以与传统和现代软件一起使用。
SheetJS Pro offers solutions beyond data processing: Edit complex templates with ease; let out your inner Picasso with styling; make custom sheets with images/graphs/PivotTables; evaluate formula expressions and port calculations to web apps; automate common spreadsheet tasks, and much more!提供数据处理以外的解决方案:轻松编辑复杂的模板;通过造型展示你内心的毕加索;使用图像/图表/数据透视表制作自定义工作表;评估公式表达式并将计算结果移植到web应用程序;自动化常见的电子表格任务,等等!
Simple Examples简单示例
The code editors are live -- feel free to edit! They use ReactJS components and run entirely in the web browser.代码编辑器是实时的——请随意编辑!它们使用ReactJS组件,完全在web浏览器中运行。
Export an HTML Table to Excel XLSX将HTML表格导出到Excel XLSX
How to add to your site如何添加到您的网站 (click to show)
- HTML
- React
1) Make sure your table has an ID:确保您的桌子有一个ID:
<table id="TableToExport">
2) Include a reference to the SheetJS library in your page:在您的页面中包含对SheetJS库的引用:
<script src="https://cdn.sheetjs.com/xlsx-0.20.1/package/dist/xlsx.full.min.js"></script>
3) Add a button that users will click to generate an export添加用户将单击以生成导出的按钮
<button id="sheetjsexport"><b>Export as XLSX</b></button>
4) Add an event handler for the 为click
event to export table data to XLSX:click
事件添加一个事件处理程序,以将表数据导出到XLSX:
<script>
document.getElementById("sheetjsexport").addEventListener('click', function() {
/* Create worksheet from HTML DOM TABLE从HTML DOM TABLE创建工作表 */
var wb = XLSX.utils.table_to_book(document.getElementById("TableToExport"));
/* Export to file (start a download)导出到文件(开始下载) */
XLSX.writeFile(wb, "SheetJSTable.xlsx");
});
</script>
This example assumes you have an existing project with an HTML TABLE element:本例假设您有一个现有的项目,其中包含一个HTML TABLE元素:
function App() {
return ( <>
<h3>SheetJS Table</h3>
<table>
<tr><td colSpan="3">SheetJS Table Export</td></tr>
<tr><td>Author</td><td>ID</td><td>你好!</td></tr>
<tr><td>SheetJS</td><td>7262</td><td>வணக்கம்!</td></tr>
<tr><td colSpan="3">
<a href="//sheetjs.com">Powered by SheetJS</a>
</td></tr>
</table>
</> )
}
export default App;
If you are starting from scratch, create a new ViteJS + ReactJS project:如果您是从头开始,请创建一个新的ViteJS+ReactJS项目:
npm create vite@latest -- sheetjs-react --template react --default
cd sheetjs-react
npm install
npm run dev
Replace 将src/App.jsx
with the sample component.src/App.jsx
替换为示例组件。
1) Install the SheetJS library using a package manager:使用包管理器安装SheetJS库:
- npm
- pnpm
- Yarn
npm i --save https://cdn.sheetjs.com/xlsx-0.20.1/xlsx-0.20.1.tgz
pnpm install https://cdn.sheetjs.com/xlsx-0.20.1/xlsx-0.20.1.tgz
yarn add https://cdn.sheetjs.com/xlsx-0.20.1/xlsx-0.20.1.tgz
2) Ensure that your component script imports 确保组件脚本从react库导入useRef
from the react
library:useRef
:
import { useRef } from "react";
3) Add the following line at the top of your component script:在组件脚本的顶部添加以下行:
import { utils, writeFileXLSX } from "xlsx";
4) Create a ref in the body of your function component:
function App() {
const tbl = useRef(null);
// ...
5) Attach the ref to the table element:
function App() {
// ...
return (
{/*...*/}
<table ref={tbl}>
{/*...*/}
6) Add a button with a click handler that will export table data to XLSX:
function App() {
// ...
return (
{/*...*/}
<button onClick={() => {
// generate workbook from table element
const wb = utils.table_to_book(tbl.current);
// write to XLSX
writeFileXLSX(wb, "SheetJSReactExport.xlsx");
}}>Export XLSX</button>
{/*...*/}
How to automate with NodeJS如何使用NodeJS实现自动化 (click to show)
The "Headless Automation" demo“无头自动化”演示 includes complete examples using the 包括使用puppeteer
and playwright
browser automation frameworks.puppeteer
和playwright
浏览器自动化框架的完整示例。
Live Example (click to hide)
Download and Preview Apple Numbers Workbooks下载并预览Apple Numbers工作簿
How to add to your site如何添加到您的网站 (click to show)
1) Create a container DIV for the table:为表创建一个容器DIV:
<div id="TableContainer"></div>
2) Include a reference to the SheetJS library in your page:在您的页面中包含对SheetJS库的引用:
<script src="https://cdn.sheetjs.com/xlsx-0.20.1/package/dist/xlsx.full.min.js"></script>
3) Add a script block to download and update the page:添加脚本块以下载和更新页面:
<script>
(async() => {
/* replace with the URL of the file替换为文件的URL */
const URL_TO_DOWNLOAD = "https://sheetjs.com/pres.numbers";
const ab = await (await fetch(URL_TO_DOWNLOAD)).arrayBuffer();
/* Parse file and get first worksheet分析文件并获取第一个工作表 */
const wb = XLSX.read(ab);
const wsname = wb.SheetNames[0];
const ws = wb.Sheets[wsname];
/* Generate HTML生成HTML */
var output = document.getElementById("TableContainer");
output.innerHTML = XLSX.utils.sheet_to_html(ws);
})();
</script>
Live Example (click to hide)
This demo processes https://sheetjs.com/pres.numbers此演示处理了https://sheetjs.com/pres.numbers
Preview a workbook on your device在设备上预览工作簿
Live Example (click to hide)
This example starts from a CSV string. Use the File Input element to select a workbook to load. Use the "Export XLSX" button to write the table to XLSX.此示例从CSV字符串开始。使用“文件输入”元素可以选择要加载的工作簿。使用“导出XLSX”按钮将表格写入XLSX。