x-spreadsheet(Star 9.8k)

github:https://github.com/myliang/x-spreadsheet
X-Spreadsheet中文文档
轻量级,基于 JavaScript 快速构建 web excel,数据驱动的基于 Web 的 Excel 库,基于 canvas 的电子表格库。

效果

image.png

使用

  1. npm install x-data-spreadsheet

react代码

  1. import React, { useRef, useEffect } from 'react';
  2. import Spreadsheet from "x-data-spreadsheet";
  3. import zhCN from 'x-data-spreadsheet/dist/locale/zh-cn';
  4. Spreadsheet.locale('zh-cn', zhCN); // 设置中文
  5. const options = {
  6. mode: 'edit', // edit | read
  7. showToolbar: true,
  8. showGrid: true,
  9. showContextmenu: true,
  10. view: {
  11. // width: () => 1200,
  12. // height: () => 600,
  13. height: () => document.documentElement.clientHeight, // 必须是一个function
  14. width: () => document.documentElement.clientWidth,
  15. },
  16. row: {
  17. len: 100,
  18. height: 25,
  19. },
  20. col: {
  21. len: 26,
  22. width: 100,
  23. indexWidth: 60,
  24. minWidth: 60,
  25. },
  26. style: {
  27. bgcolor: '#ffffff',
  28. align: 'left',
  29. valign: 'middle',
  30. textwrap: false,
  31. strike: false,
  32. underline: false,
  33. color: '#0a0a0a',
  34. font: {
  35. name: 'Helvetica',
  36. size: 10,
  37. bold: false,
  38. italic: false,
  39. },
  40. },
  41. }
  42. const webExcel = () => {
  43. const spreadsheetRef = useRef();
  44. useEffect(() => {
  45. // If you need to override the default options, you can set the override
  46. // new Spreadsheet('#x-spreadsheet-demo'); // 没有option会使用默认配置
  47. // const s = new Spreadsheet('#x-spreadsheet-demo', options) // 使用元素id
  48. const s = new Spreadsheet(spreadsheetRef.current, options)
  49. .loadData({}) // load data
  50. .change(data => {
  51. // save data to db
  52. });
  53. // data validation
  54. s.validate();
  55. }, []);
  56. return (
  57. <div id="x-spreadsheet-demo" ref={spreadsheetRef}></div>
  58. );
  59. };
  60. export default webExcel;

jexcel(Star 4.9k)

https://github.com/paulhodel/jexcel

一个轻量级、功能强大的电子表格库。轻松实现复杂数据的表格管理,支持 JS 数组、JSON、CSV 等数据,并且可以实现 excel 文件的直接复制和粘贴。

效果

jexcel、x-spreadsheet(表格管理) - 图2

使用

  1. var data = [
  2. ['Jazz', 'Honda', '2019-02-12', '', true, '$ 2.000,00', '#777700'],
  3. ['Civic', 'Honda', '2018-07-11', '', true, '$ 4.000,01', '#007777'],
  4. ];
  5. jexcel(document.getElementById('spreadsheet'), {
  6. data:data,
  7. columns: [
  8. { type: 'text', title:'Car', width:120 },
  9. { type: 'dropdown', title:'Make', width:200, source:[ "Alfa Romeo", "Audi", "Bmw" ] },
  10. { type: 'calendar', title:'Available', width:200 },
  11. { type: 'image', title:'Photo', width:120 },
  12. { type: 'checkbox', title:'Stock', width:80 },
  13. { type: 'numeric', title:'Price', width:100, mask:'$ #.##,00', decimal:',' },
  14. { type: 'color', width:100, render:'square', }
  15. ]
  16. });