Xlsx sheetname 在反应中长度超过 31 个字符

Xlsx sheetname exceeding 31 characters in length in react

我正在使用 React 从数据库中获取 table 数据并将其导出为 excel 并将 excel 的标题设为 tablename。但是如果 excel 文件名超过 31 个字符,我会遇到问题,sheet 不会被下载。并在浏览器控制台输出中抛出以下错误。

Uncaught Error: Sheet names cannot exceed 31 chars

下面是我的代码

var workbook=XLSX.utils.book_new();
workbook.SheetNames.push(title); //title: tablename
var workSheet=XLSX.utils.aoa_to_sheet(tableData); //tableData: data from Database
workbook.Sheets[title]=workSheet;
var output=XLSX.write(workbook, {bookType:'xlsx', type:'binary'});
function s2ab(s) {
  var buf = new ArrayBuffer(s.length);
  var view = new Uint8Array(buf);
  for (var i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
  return buf;
}
saveAs(new Blob([s2ab(output)], {type:"application/octet-stream"}), title + '.xlsx');

编辑: 我不想更改 sheet 名称。即使 sheet 名称超过 31 个字符

也只是想下载

通过以下更改解决

var workbook=XLSX.utils.book_new();
//workbook.SheetNames.push(title); //title: tablename
var workSheet=XLSX.utils.aoa_to_sheet(tableData); //tableData: data from Database
//workbook.Sheets[title]=workSheet;
XLSX.utils.book_append_sheet(workbook, workSheet, "Test"); // This line solve the problem
var output=XLSX.write(workbook, {bookType:'xlsx', type:'binary'});
function s2ab(s) {
  var buf = new ArrayBuffer(s.length);
  var view = new Uint8Array(buf);
  for (var i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
  return buf;
}
saveAs(new Blob([s2ab(output)], {type:"application/octet-stream"}), title + '.xlsx');