如何使用js-export-excel设置表格样式
时间: 2023-12-29 18:06:41 浏览: 77
使用js导出带样式的excel表格
使用js-export-excel设置表格样式的步骤如下:
1. 引入js-export-excel库
```javascript
<script src="https://cdn.bootcdn.net/ajax/libs/FileSaver.js/1.3.8/FileSaver.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/xlsx/0.14.5/xlsx.full.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/js-export-excel/0.4.7/js-export-excel.min.js"></script>
```
2. 定义表格数据和样式
```javascript
var data = [
['姓名', '年龄', '性别'],
['张三', '18', '男'],
['李四', '20', '女'],
['王五', '22', '男']
];
var style = {
header: {
font: {
bold: true
},
alignment: {
horizontal: 'center'
}
},
cell: {
alignment: {
horizontal: 'center'
}
}
};
```
3. 创建工作簿
```javascript
var sheetName = 'Sheet1';
var workbook = XLSX.utils.book_new();
var worksheet = XLSX.utils.aoa_to_sheet(data);
XLSX.utils.book_append_sheet(workbook, worksheet, sheetName);
```
4. 设置表格样式
```javascript
var range = XLSX.utils.decode_range(worksheet['!ref']);
for (var rowNum = range.s.r; rowNum <= range.e.r; rowNum++) {
for (var colNum = range.s.c; colNum <= range.e.c; colNum++) {
var cell = XLSX.utils.encode_cell({ r: rowNum, c: colNum });
var cellStyle = {};
if (rowNum === 0) {
cellStyle = style.header;
} else {
cellStyle = style.cell;
}
XLSX.utils.book_set_style(workbook, sheetName, cell, cellStyle);
}
}
```
5. 导出Excel文件
```javascript
var excelFileName = 'example.xlsx';
var workbookOutput = XLSX.write(workbook, { bookType: 'xlsx', type: 'binary' });
var buffer = new ArrayBuffer(workbookOutput.length);
var view = new Uint8Array(buffer);
for (var i = 0; i < workbookOutput.length; i++) {
view[i] = workbookOutput.charCodeAt(i) & 0xFF;
}
var blob = new Blob([buffer], { type: 'application/octet-stream' });
saveAs(blob, excelFileName);
```
完整代码示例:
```javascript
var data = [
['姓名', '年龄', '性别'],
['张三', '18', '男'],
['李四', '20', '女'],
['王五', '22', '男']
];
var style = {
header: {
font: {
bold: true
},
alignment: {
horizontal: 'center'
}
},
cell: {
alignment: {
horizontal: 'center'
}
}
};
var sheetName = 'Sheet1';
var workbook = XLSX.utils.book_new();
var worksheet = XLSX.utils.aoa_to_sheet(data);
XLSX.utils.book_append_sheet(workbook, worksheet, sheetName);
var range = XLSX.utils.decode_range(worksheet['!ref']);
for (var rowNum = range.s.r; rowNum <= range.e.r; rowNum++) {
for (var colNum = range.s.c; colNum <= range.e.c; colNum++) {
var cell = XLSX.utils.encode_cell({ r: rowNum, c: colNum });
var cellStyle = {};
if (rowNum === 0) {
cellStyle = style.header;
} else {
cellStyle = style.cell;
}
XLSX.utils.book_set_style(workbook, sheetName, cell, cellStyle);
}
}
var excelFileName = 'example.xlsx';
var workbookOutput = XLSX.write(workbook, { bookType: 'xlsx', type: 'binary' });
var buffer = new ArrayBuffer(workbookOutput.length);
var view = new Uint8Array(buffer);
for (var i = 0; i < workbookOutput.length; i++) {
view[i] = workbookOutput.charCodeAt(i) & 0xFF;
}
var blob = new Blob([buffer], { type: 'application/octet-stream' });
saveAs(blob, excelFileName);
```
阅读全文