当前位置: 首页 > 工具软件 > x-spreadsheet > 使用案例 >

报表Excel的制作X-Spreadsheet

邹胜泫
2023-12-01

X-Spreadsheet中文文档

NPM

你可以使用 npm 包的形式安装

基于项目构建

  npm install x-data-spreadsheet

在 html 中,纯html时实现

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="https://unpkg.com/x-data-spreadsheet@1.1.1/dist/xspreadsheet.css">
    <script src="https://unpkg.com/x-data-spreadsheet@1.1.1/dist/xspreadsheet.js"></script>
    <script src="https://unpkg.com/x-data-spreadsheet@1.1.1/dist/locale/zh-cn.js"></script>

</head>
<body>
  <div id="x-spreadsheet-demo"></div>
  
  <script>
    x_spreadsheet.locale('zh-cn');  //中文
    var htmlout = document.getElementById('x-spreadsheet-demo')
    var xs = x_spreadsheet(htmlout)
  </script>
</body>
</html>

在 js 中

import Spreadsheet from "x-data-spreadsheet";
// If you need to override the default options, you can set the override
// const options = {};
// new Spreadsheet('#x-spreadsheet-demo', options);
const s = new Spreadsheet("#x-spreadsheet-demo")
  .loadData({}) // load data
  .change(data => {
    // save data to db
  });

// data validation
s.validate()

默认配置

{
  mode: 'edit', // edit | read
  showToolbar: true,
  showGrid: true,
  showContextmenu: true,
  view: {
    height: () => document.documentElement.clientHeight,
    width: () => document.documentElement.clientWidth,
  },
  row: {
    len: 100,
    height: 25,
  },
  col: {
    len: 26,
    width: 100,
    indexWidth: 60,
    minWidth: 60,
  },
  style: {
    bgcolor: '#ffffff',
    align: 'left',
    valign: 'middle',
    textwrap: false,
    strike: false,
    underline: false,
    color: '#0a0a0a',
    font: {
      name: 'Helvetica',
      size: 10,
      bold: false,
      italic: false,
    },
  },
}

绑定事件

  const s = new Spreadsheet("#x-spreadsheet-demo")
  // event of click on cell
  s.on('cell-selected', (cell, ri, ci) => {});
  s.on('cells-selected', (cell, { sri, sci, eri, eci }) => {});
  // edited on cell
  s.on('cell-edited', (text, ri, ci) => {});

快速设定单元格值

 const s = new Spreadsheet("#x-spreadsheet-demo")
  s.cellText(5, 5, 'xxxx').cellText(6, 5, 'yyy').reRender();

获取选定表格中单元的样式和值

const s = new Spreadsheet("#x-spreadsheet-demo")
// cell(ri, ci, sheetIndex = 0)
s.cell(ri, ci);
// cellStyle(ri, ci, sheetIndex = 0)
s.cellStyle(ri, ci);

快速本地化

如果是在 js 中引入

  // npm
  import Spreadsheet from 'x-data-spreadsheet';
  import zhCN from 'x-data-spreadsheet/dist/locale/zh-cn';

  Spreadsheet.locale('zh-cn', zhCN);
  new Spreadsheet(document.getElementById('xss-demo'))
 类似资料: