X-Spreadsheet

基于 JavaScript 快速构建 Web Excel

Posted by page on October 31, 2021

x-spreadsheet

中文文档 GitHub x-spreadsheet SheetJs官网

安装

npm i x-data-spreadsheet -S

使用

创建表格

<template>
  <div class="sheetBox"></div>
</template>

<script>
  import XLSX from 'xlsx';
  import Spreadsheet from 'x-data-spreadsheet';
  import 'x-data-spreadsheet/dist/locale/zh-cn';
  Spreadsheet.locale('zh-cn');

  const options = {
    showToolbar: false,  // 显示工具栏
    showContextmenu: false, // 右键显示菜单
    showBottomBar: false,  // 显示底部信息
    view: { height: 618, width: 1000 },  // 表格视图宽高
    row: { height: 30, len: 100 },  // 行高与行数
    col: { width: 90, len: 26 }  // 列高与列数
  };

  const sheetEditor = new Spreadsheet(".sheetBox", options));
</script>

xlsx转换

spreadData转Excel工作表

xtos stox 方法存放于 sheetjs下 sheet/demos/xspreadsheet/xlsxspread.js 目录

import { xtos,stox } from '@/plugins/xlsxspread.js';

const spreadSheetData = sheetEditor.getData();

function toSheet(spreadSheetData) {
  const workbook = xtos(spreadSheetData);
  // XLSX.writeFile(xtos(spreadSheetData), "SheetJS.xlsx");
  return workbook.Sheets[workbook.SheetNames[0]];
}

spreadData转Excel工作表

官方说明:https://docs.sheetjs.com/docs/demos/grid#x-spreadsheet

方法

sheetEditor.sheet.reload() 重载表格

 this.sheetEditor.loadData({}) 清空表格