excelize-wasm.js是一款在浏览器中读取操作修改excel电子表格XLT的插件

excelize-wasm.js是一款在浏览器中读取操作修改excel电子表格XLT的插件

excelize-wasm.js是一款在浏览器中读取操作修改excel电子表格的插件,Excelize-wasm 是 Go Excelize库的纯 WebAssembly / Javascript 插件,允许您写入和读取 XLAM / XLSM / XLSX / XLTM / XLTX 文件。支持读取和写入 Microsoft Excel™ 2007 及更高版本生成的电子表格文档。高兼容性支持复杂组件。

使用方式:

Node.js

npm install --save excelize-wasm

const { init } = require('excelize-wasm');
const fs = require('fs');

init('./node_modules/excelize-wasm/excelize.wasm.gz').then((excelize) => {
  const f = excelize.NewFile();
  // Create a new sheet.
  const { index } = f.NewSheet('Sheet2');
  // Set value of a cell.
  f.SetCellValue('Sheet2', 'A2', 'Hello world.');
  f.SetCellValue('Sheet1', 'B2', 100);
  // Set active sheet of the workbook.
  f.SetActiveSheet(index);
  // Save spreadsheet by the given path.
  const { buffer, error } = f.WriteToBuffer();
  if (error) {
    console.log(error);
    return;
  }
  fs.writeFile('Book1.xlsx', buffer, 'binary', (error) => {
    if (error) {
      console.log(error);
    }
  });
});

浏览器

script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/excelize-wasm.js"></script>

<html>
<head>
  <meta charset="utf-8">
  script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/excelize-wasm.js"></script>
</head>
<body>
  <div>
    <button onclick="download()">Download</button>
  </div>
  <script>
  function download() {
    excelizeWASM
      .init('//repo.bfw.wiki/bfwrepo/js/excelize.wasm.gz')
      .then((excelize) => {
        const f = excelize.NewFile();
        // Create a new sheet.
        const { index } = f.NewSheet('Sheet2');
        // Set value of a cell.
        f.SetCellValue('Sheet2', 'A2', 'Hello world.');
        f.SetCellValue('Sheet1', 'B2', 100);
        // Set active sheet of the workbook.
        f.SetActiveSheet(index);
        // Save spreadsheet by the given path.
        const { buffer, error } = f.WriteToBuffer();
        if (error) {
          console.log(error);
          return;
        }
        const link = document.createElement('a');
        link.download = 'Book1.xlsx';
        link.href = URL.createObjectURL(
          new Blob([buffer], {
            type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
          })
        );
        link.click();
      });
    }
  </script>
</body>

完整代码:

<html>

<head>
    <meta charset="utf-8">
    <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/excelize-wasm.js"></script>
</head>

<body>
    <div>
        <button onclick="download()">新建一个excel文件创建多个sheet并下载</button>
    </div>
    <script>
        function download() {
            excelizeWASM
              .init('//repo.bfw.wiki/bfwrepo/js/excelize.wasm.gz')
              .then((excelize) => {
                const f = excelize.NewFile();
                // Create a new sheet.
                const { index } = f.NewSheet('Sheet2');
                // Set value of a cell.
                f.SetCellValue('Sheet2', 'A2', 'Hello world.');
                f.SetCellValue('Sheet1', 'B2', 100);
                // Set active sheet of the workbook.
                f.SetActiveSheet(0);
                
            
  [
    [null, 'Apple', 'Orange', 'Pear'],
    ['Small', 2, 3, 3],
    ['Normal', 5, 2, 4],
    ['Large', 6, 7, 8],
  ].forEach((row, idx) => {
    const ret1 = excelize.CoordinatesToCellName(1, idx + 1);
    if (ret1.error) {
      console.log(ret1.error);
      return;
    }
    const res2 = f.SetSheetRow('Sheet1', ret1.cell, row);
    if (res2.error) {
      console.log(res2.error);
      return;
    }
  });
  const ret3 = f.AddChart('Sheet1', 'E1', {
    Type: excelize.Col3DClustered,
    Series: [
      {
        Name: 'Sheet1!$A$2',
        Categories: 'Sheet1!$B$1:$D$1',
        Values: 'Sheet1!$B$2:$D$2',
      },
      {
        Name: 'Sheet1!$A$3',
        Categories: 'Sheet1!$B$1:$D$1',
        Values: 'Sheet1!$B$3:$D$3',
      },
      {
        Name: 'Sheet1!$A$4',
        Categories: 'Sheet1!$B$1:$D$1',
        Values: 'Sheet1!$B$4:$D$4',
      },
    ],
    Title: {
      Name: 'Fruit 3D Clustered Column Chart',
    },
  });
                // Save spreadsheet by the given path.
                const { buffer, error } = f.WriteToBuffer();
                if (error) {
                  console.log(error);
                  return;
                }
                
                const link = document.createElement('a');
                link.download = 'Book1.xlsx';
                link.href = URL.createObjectURL(
                  new Blob([buffer], {
                    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                  })
                );
                link.click();
              });
            }
    </script>

</body>

</html>

api文档:https://xuri.me/excelize/
立即下载excelize-wasm.js查看所有js插件

网友评论0

程序员在线工具箱