一:
注意:ref="table"
javascript
<el-button type="primary" icon="el-icon-download" @click="exportExcel">导出Excel</el-button>
<el-table :data="codeData" border ref="table">
<el-table-column label="数据实体" prop="dataName" align="center"/>
<el-table-column label="更新时间" prop="updateTime" align="center"/>
</el-table>
二:引入方法:
javascript
import { deleteCol } from '@/utils/excel'
三:methods:
javascript
exportExcel() {
const workbook = this.$xlsx.utils.table_to_book(this.$refs.table.$el, { raw: true })
deleteCol(workbook.Sheets[workbook.SheetNames[0]], 8)
return this.$xlsx.writeFile(workbook, 'download.xlsx')
},
四:引入的文件内容:
/utils/excel
javascript
import * as xlsx from 'xlsx'
function encodeCell(r, c) {
return xlsx.utils.encode_cell({ r, c })
}
export function deleteRow(ws, index) {
const range = xlsx.utils.decode_range(ws['!ref'])
for (let row = index; row < range.e.r; row++) {
for (let col = range.s.c; col <= range.e.c; col++) {
ws[encodeCell(row, col)] = ws[encodeCell(row + 1, col)]
}
}
range.e.r--
ws['!ref'] = xlsx.utils.encode_range(range.s, range.e)
}
export function deleteCol(ws, index) {
const range = xlsx.utils.decode_range(ws['!ref'])
for (let col = index; col < range.e.c; col++) {
for (let row = range.s.r; row <= range.e.r; row++) {
ws[encodeCell(row, col)] = ws[encodeCell(row, col + 1)]
}
}
range.e.c--
ws['!ref'] = xlsx.utils.encode_range(range.s, range.e)
}
export function exportExcelFile(array, sheetName = '表1', fileName = 'example.xlsx') {
let jsonWorkSheet = xlsx.utils.json_to_sheet(array)
let workBook = xlsx.utils.book_new()
workBook = {
SheetNames: [sheetName],
Sheets: {
[sheetName]: jsonWorkSheet,
},
}
return xlsx.writeFile(workBook, fileName)
}