JavaScript 如何实现导出excel文件

JavaScript 如何实现导出excel文件

阅读 9.7k
5 个回答

js-xlsx

xlsx

Parser and writer for various spreadsheet formats. Pure-JS cleanroom
implementation from official specifications and related documents.

Supported read formats:

  • Excel 2007+ XML Formats (XLSX/XLSM)
  • Excel 2007+ Binary Format (XLSB)
  • Excel 2003-2004 XML Format (XML "SpreadsheetML")
  • Excel 97-2004 (XLS BIFF8)
  • Excel 5.0/95 (XLS BIFF5)
  • OpenDocument Spreadsheet (ODS)

Supported write formats:

  • XLSX
  • CSV (and general DSV)
  • JSON and JS objects (various styles)

Demo: http://oss.sheetjs.com/js-xlsx

Source: http://git.io/xlsx

js-xlsx 比较难用,代码比较混乱,而且导出带样式的也比较麻烦,最近写了一个,导出还算方便,项目地址:https://github.com/d-band/bet...

附一个简单的 Demo:

const fs = require('fs');
const xlsx = require('better-xlsx');

const file = new xlsx.File();

const sheet = file.addSheet('Sheet1');
const row = sheet.addRow();
const cell = row.addCell();

cell.value = 'I am a cell!';
cell.hMerge = 2;
cell.vMerge = 1;

const style = new xlsx.Style();

style.fill.patternType = 'solid';
style.fill.fgColor = '00FF0000';
style.fill.bgColor = 'FF000000';
style.align.h = 'center';
style.align.v = 'center';

cell.style = style;

file
  .saveAs()
  .pipe(fs.createWriteStream(__dirname + '/simple.xlsx'))
  .on('finish', () => console.log('Done.'));
新手上路,请多包涵

可以借助 SpreadJS 表格控件来实现。无需任何后台代码和第三方组件!SpreadJS 可直接在浏览器中完成 Excel、CSV、JSON 等文件的导入导出、PDF 导出、打印及预览操作.

image

SpreadJS 支持在线导入和导出 Excel (.xlsx),使用该功能,用户可在浏览器中加载并修改各种 Excel 文档,并将修改后的数据保存到数据库中。

点此运行Demo

新手上路,请多包涵
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题