给出下面的sheetjs代码片段:
const XLSX = require('xlsx');
const workbook = XLSX.utils.book_new();
const ws = XLSX.utils.aoa_to_sheet([[1, 2, 3], [4,5,6]]);
XLSX.utils.book_append_sheet(workbook, ws, page);
当我试图访问一行时:const rows = ws['!rows'];
返回undefined
。检查ws
显示它也不具有该属性。如何访问行属性(并为第一行设置样式)?
你需要先声明它,然后你可以修改宽度,高度
let wb = XLSX.utils.book_new();
let ws = XLSX.utils.table_to_sheet(document.getElementById(id));
ws['!cols'] = [];
ws['!rows'] = [];
ws['!cols'] = [
{'width' : 30}, // width for col A
{'width' : 30}, // width for col B
{'hidden' : true}, ]; // hidding col C
ws['!rows'] = [
{'hpt' : 30},// height for row 1
{'hpt' : 30}, ]; //height for row 2
XLSX.utils.book_append_sheet(wb, ws, 'sheet1');
XLSX.writeFile(wb, filename+'.xlsx');
!Rows and !cols有你给它的所有属性。所以在你设置一些之前它是没有定义的。下面的代码将第一列设置为15个字符宽。
ws['!cols'] = [{wch: 15}];
这里有更多:https://github.com/sheetjs/sheetjs#worksheet-object