12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- import * as XLSX from 'xlsx'
- import { saveAs } from 'file-saver'
- import * as XLSXD from 'xlsx-js-style'
- const exportExcel = (el, header, title) => {
- let $e = el
- try {
- let $table = $e.querySelector('.el-table__fixed')
- if(!$table) {
- $table = $e
- }
- const wb = XLSX.utils.table_to_book($table, {raw:true})
- if (wb && wb.Sheets.Sheet1) {
- let as = ['!cols', '!fullref', '!merges', '!ref', '!rows']
- let bs = header
- for(let i in wb.Sheets.Sheet1) {
- if (as.indexOf(i)<0) {
- if (bs.indexOf(wb.Sheets.Sheet1[i].v)>=0) {
- wb.Sheets.Sheet1[i].s = {
- font:{
- name: '微软雅黑',
- sz: 11,
- bold: true,
- color: {rgb: 'ffffff'}
- },
- alignment: {
- horizontal: 'center',
- vertical: 'center'
- },
- fill:{fgColor: {rgb: '4f81bd'}}
- }
- if (i === 'A1') {
- wb.Sheets.Sheet1['!cols'].push({wch: 30})
- } else if (i === 'B1') {
- wb.Sheets.Sheet1['!cols'].push({wch: 20})
- } else {
- wb.Sheets.Sheet1['!cols'].push({wch: 15})
- }
- } else {
- wb.Sheets.Sheet1[i].s = {
- font:{
- name: '微软雅黑',
- sz: 10,
- bold: false,
- color: {rgb: '000000'}
- },
- alignment: {
- horizontal: 'center',
- vertical: 'center'
- },
- // fill:{fgColor: {rgb: '4f81bd'}}
- }
- }
- }
- }
- }
- const wbout = XLSXD.write(wb, {bookType: 'xlsx', bookSST:true, type: 'array'})
- saveAs(
- new Blob([wbout],{type: 'application/octet-stream'}),
- `${title}.xlsx`,
- )
- } catch (e) {
- if (typeof console !== 'undefined') console.error(e)
- }
- }
- export default {
- exportExcel
- }
|