新建Export2Excel.js
require('script-loader!file-saver') //保存文件用
require('script-loader!@/vendor/Blob') //转二进制用
require('script-loader!xlsx/dist/xlsx.core.min') //xlsx核心
require('script-loader!xlsx-style/dist/xlsx.full.min')
function generateArray(table) {
var out = []
var rows = table.querySelectorAll('tr')
var ranges = []
for (var R = 0; R < rows.length; ++R) {
var outRow = []
var row = rows[R]
var columns = row.querySelectorAll('td')
for (var C = 0; C < columns.length; ++C) {
var cell = columns[C]
var colspan = cell.getAttribute('colspan')
var rowspan = cell.getAttribute('rowspan')
var cellValue = cell.innerText
if (cellValue !== '' && cellValue == +cellValue) cellValue = +cellValue
//Skip ranges
ranges.forEach(function (range) {
if (R >= range.s.r && R <= range.e.r && outRow.length >= range.s.c && outRow.length <= range.e.c) {
for (var i = 0; i <= range.e.c - range.s.c; ++i) outRow.push(null)
}
})
//Handle Row Span
if (rowspan || colspan) {
rowspan = rowspan || 1
colspan = colspan || 1
ranges.push({ s: { r: R, c: outRow.length }, e: { r: R + rowspan - 1, c: outRow.length + colspan - 1 } })
}
//Handle Value
outRow.push(cellValue !== '' ? cellValue : null)
//Handle Colspan
if (colspan) for (var k = 0; k < colspan - 1; ++k) outRow.push(null)
}
out.push(outRow)
}
return [out, ranges]
}
function datenum(v, date1904) {
if (date1904) v += 1462
var epoch = Date.parse(v)
return (epoch - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000)
}
//通用的数据和格式 (表头合并)
function sheet_from_array_of_arrays_mergeheader(data, ArrMerge) {
var ws = { s: { '!row': [{ wpx: 67 }] } }
ws['!cols'] = []
var range = { s: { c: 10000000, r: 10000000 }, e: { c: 0, r: 0 } } //c是列-1 r是内容行
for (var R = 0; R != data.length; ++R) {
for (var C = 0; C != data[R].length; ++C) {
if (range.s.r > R) range.s.r = R
if (range.s.c > C) range.s.c = C
if (range.e.r < R) range.e.r = R
if (range.e.c < C) range.e.c = C
var cell = { v: data[R][C], s: { alignment: { horizontal: 'center', vertical: 'center' }, font: { sz: '13' } } }
if (R == 0) {
cell = {
v: data[R],
s: { alignment: { horizontal: 'center', vertical: 'center' }, font: { sz: '16', bold: true } }
}
}
if (R == 1) {
ws['!cols'].push({ wpx: 120 })
cell = {
v: data[R][C],
s: { alignment: { horizontal: 'center', vertical: 'center', wrapText: true }, font: { sz: '16', bold: true } }
}
}
if (R == data.length - 1) {
if (data[R][1] == '合计')
cell = {
v: data[R][C],
s: { alignment: { horizontal: 'center', vertical: 'center' }, font: { sz: '16', bold: true } }
}
}
if (cell.v == null) continue
var cell_ref = XLSX.utils.encode_cell({ c: C, r: R })
if (typeof cell.v === 'number') cell.t = 'n'
else if (typeof cell.v === 'boolean') cell.t = 'b'
else if (cell.v instanceof Date) {
cell.t = 'n'
cell.z = XLSX.SSF._table[14]
cell.v = datenum(cell.v)
} else cell.t = 's'
ws[cell_ref] = cell
}
}
ws['!merges'] = ArrMerge
if (range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range)
return ws
}
//通用的数据和格式
function sheet_from_array_of_arrays(data, colNum) {
var ws = { s: { '!row': [{ wpx: 67 }] } }
ws['!cols'] = []
var range = { s: { c: 10000000, r: 10000000 }, e: { c: 0, r: 0 } } //c是列-1 r是内容行
for (var R = 0; R != data.length; ++R) {
for (var C = 0; C != data[R].length; ++C) {
if (range.s.r > R) range.s.r = R
if (range.s.c > C) range.s.c = C
if (range.e.r < R) range.e.r = R
if (range.e.c < C) range.e.c = C
var cell = { v: data[R][C], s: { alignment: { horizontal: 'center', vertical: 'center' }, font: { sz: '13' } } }
if (R == 0) {
cell = {
v: data[R],
s: { alignment: { horizontal: 'center', vertical: 'center' }, font: { sz: '16', bold: true } }
}
}
if (R == 1) {
ws['!cols'].push({ wpx: 120 })
cell = {
v: data[R][C],
s: { alignment: { horizontal: 'center', vertical: 'center', wrapText: true }, font: { sz: '16', bold: true } }
}
}
if (R == data.length - 1) {
if (data[R][1] == '合计')
cell = {
v: data[R][C],
s: { alignment: { horizontal: 'center', vertical: 'center' }, font: { sz: '16', bold: true } }
}
}
if (cell.v == null) continue
var cell_ref = XLSX.utils.encode_cell({ c: C, r: R })
if (typeof cell.v === 'number') cell.t = 'n'
else if (typeof cell.v === 'boolean') cell.t = 'b'
else if (cell.v instanceof Date) {
cell.t = 'n'
cell.z = XLSX.SSF._table[14]
cell.v = datenum(cell.v)
} else cell.t = 's'
ws[cell_ref] = cell
}
}
ws['!merges'] = [{ s: { c: 0, r: 0 }, e: { c: range.e.c, r: 0 } }]
if (range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range)
return ws
}
//个性化数据和格式(1)
function sheet_from_array_of_arrays_jmmxb(data, colNum) {
var ws = { s: { '!row': [{ wpx: 67 }] } }
ws['!cols'] = []
var range = { s: { c: 10000000, r: 10000000 }, e: { c: 0, r: 0 } } //c是列-1 r是内容行
for (var R = 0; R != data.length; ++R) {
for (var C = 0; C != data[R].length; ++C) {
if (range.s.r > R) range.s.r = R
if (range.s.c > C) range.s.c = C
if (range.e.r < R) range.e.r = R
if (range.e.c < C) range.e.c = C
var cell = { v: data[R][C], s: { alignment: { horizontal: 'center', vertical: 'center' }, font: { sz: '13' } } }
if (R == 0) {
cell = {
v: data[R],
s: { alignment: { horizontal: 'center', vertical: 'center' }, font: { sz: '16', bold: true } }
}
}
if (R == 1) {
cell = { v: data[R], s: { alignment: { horizontal: 'left', vertical: 'left' }, font: { sz: '13' } } }
}
if (R == 2) {
ws['!cols'].push({ wpx: 120 })
cell = {
v: data[R][C],
s: { alignment: { horizontal: 'center', vertical: 'center' }, font: { sz: '16', bold: true } }
}
}
if (R == data.length - 1) {
if (data[R][1] == '合计')
cell = {
v: data[R][C],
s: { alignment: { horizontal: 'center', vertical: 'center' }, font: { sz: '16', bold: true } }
}
}
if (cell.v == null) continue
var cell_ref = XLSX.utils.encode_cell({ c: C, r: R })
if (typeof cell.v === 'number') cell.t = 'n'
else if (typeof cell.v === 'boolean') cell.t = 'b'
else if (cell.v instanceof Date) {
cell.t = 'n'
cell.z = XLSX.SSF._table[14]
cell.v = datenum(cell.v)
} else cell.t = 's'
ws[cell_ref] = cell
}
}
ws['!merges'] = [
{ s: { c: 0, r: 0 }, e: { c: range.e.c, r: 0 } },
{ s: { c: 0, r: 1 }, e: { c: range.e.c, r: 1 } }
]
if (range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range)
return ws
}
//个性化数据和格式(2)
function sheet_from_array_of_arrays_hmzcjm(data) {
var ws = { s: { '!row': [{ wpx: 67 }] } }
ws['!cols'] = []
var range = { s: { c: 10000000, r: 10000000 }, e: { c: 0, r: 0 } }
var n = 0
for (var R = 0; R != data.length; ++R) {
for (var C = 0; C != data[R].length; ++C) {
if (range.s.r > R) range.s.r = R
if (range.s.c > C) range.s.c = C
if (range.e.r < R) range.e.r = R
if (range.e.c < C) range.e.c = C
var cell = { v: data[R][C], s: { alignment: { horizontal: 'center', vertical: 'center' }, font: { sz: '13' } } }
if (R == 0) {
ws['!cols'].push({ wpx: 140 })
cell = {
v: data[R][C],
s: { alignment: { horizontal: 'center', vertical: 'center' }, font: { sz: '16', bold: true } }
}
}
if (R == data.length - 1) {
cell = {
v: '备注:' + data[R][C],
s: { alignment: { horizontal: 'center', vertical: 'center' }, font: { sz: '14' } }
}
}
if (cell.v == null) continue
var cell_ref = XLSX.utils.encode_cell({ c: C, r: R })
if (typeof cell.v === 'number') cell.t = 'n'
else if (typeof cell.v === 'boolean') cell.t = 'b'
else if (cell.v instanceof Date) {
cell.t = 'n'
cell.z = XLSX.SSF._table[14]
cell.v = datenum(cell.v)
} else cell.t = 's'
ws[cell_ref] = cell
}
}
// ec控制横向几格。sr控制竖向几格
ws['!merges'] = [
{ s: { c: 0, r: 0 }, e: { c: 3, r: 0 } },
{ s: { c: 0, r: 1 }, e: { c: 0, r: 1 } },
{ s: { c: 0, r: 2 }, e: { c: 0, r: 4 } },
{ s: { c: 0, r: 5 }, e: { c: 0, r: 7 } },
{ s: { c: 0, r: 8 }, e: { c: 0, r: 10 } },
{ s: { c: 0, r: 11 }, e: { c: 0, r: 13 } },
{ s: { c: 0, r: 14 }, e: { c: 0, r: 16 } },
{ s: { c: 0, r: 17 }, e: { c: 0, r: 19 } },
{ s: { c: 0, r: 20 }, e: { c: 0, r: 22 } },
{ s: { c: 0, r: 23 }, e: { c: 0, r: 25 } },
{ s: { c: 0, r: 26 }, e: { c: 3, r: 26 } }
]
if (range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range)
return ws
}
function Workbook() {
if (!(this instanceof Workbook)) return new Workbook()
this.SheetNames = []
this.Sheets = {}
}
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
}
export function export_table_to_excel(id) {
var theTable = document.getElementById(id)
var oo = generateArray(theTable)
var ranges = oo[1]
/* original data */
var data = oo[0]
var ws_name = 'SheetJS'
var wb = new Workbook(),
ws = sheet_from_array_of_arrays(data)
/* add ranges to worksheet */
// ws['!cols'] = ['apple', 'banan'];
ws['!merges'] = ranges
/* add worksheet to workbook */
wb.SheetNames.push(ws_name)
wb.Sheets[ws_name] = ws
var wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: false, type: 'binary' })
saveAs(new Blob([s2ab(wbout)], { type: 'application/octet-stream' }), 'test.xlsx')
}
function formatJson(jsonData) {
// console.log(jsonData)
}
//2003excel
export function export_json_to_excelFapiao(headerList, dataList, defaultTitle, colNum, div) {
/* original data */
var filterVal = []
var tHeader = []
for (var i = 0; i < headerList.length; i++) {
if (headerList[i].ColumnKey === '') {
/* 处理二级列项 */
for (var j = 0; j < headerList[i].ColumnChild.length; j++) {
tHeader = Boolean(headerList[i].ColumnName.replace(/\s/g, ''))
? tHeader.concat(`${headerList[i].ColumnName}(${headerList[i].ColumnChild[j].ColumnName})`)
: tHeader.concat(headerList[i].ColumnChild[j].ColumnName)
filterVal = filterVal.concat(headerList[i].ColumnChild[j].ColumnKey)
}
/* 处理一级列项 */
} else {
tHeader = tHeader.concat(headerList[i].ColumnName)
filterVal = filterVal.concat(headerList[i].ColumnKey)
}
}
// console.log("dataList",dataList)
var data = dataList.map(v => filterVal.map(j => v[j]))
data.unshift(tHeader)
data.unshift([defaultTitle])
if (colNum === undefined) colNum = 1
var ws_name = 'SheetJS'
var wb = new Workbook(),
ws
if (div === undefined) {
ws = sheet_from_array_of_arrays(data, colNum)
} else if (div === 'hmzcjm') {
ws = sheet_from_array_of_arrays_hmzcjm(data)
} else if (div === 'jmmxb') {
var name = localStorage.username
var zbdw = localStorage.orgname
data.splice(1, 0, ['制表单位:' + zbdw + ' ' + '制表人:' + name])
ws = sheet_from_array_of_arrays_jmmxb(data)
}
/* add worksheet to workbook */
wb.SheetNames.push(ws_name)
wb.Sheets[ws_name] = ws
var wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: false, type: 'binary' })
var title = defaultTitle || '列表'
saveAs(new Blob([s2ab(wbout)], { type: 'application/octet-stream' }), title + '.xls')
}
export function export_json_to_excel(headerList, dataList, defaultTitle, colNum, div) {
/* original data */
var filterVal = []
var tHeader = []
for (var i = 0; i < headerList.length; i++) {
if (!headerList[i].ColumnKey) {
/* 处理二级列项 */
for (var j = 0; j < headerList[i].ColumnChild.length; j++) {
tHeader = Boolean(headerList[i].ColumnName.replace(/\s/g, ''))
? tHeader.concat(`${headerList[i].ColumnName}(${headerList[i].ColumnChild[j].ColumnName})`)
: tHeader.concat(headerList[i].ColumnChild[j].ColumnName)
filterVal = filterVal.concat(headerList[i].ColumnChild[j].ColumnKey)
}
} else {
/* 处理一级列项 */
tHeader = tHeader.concat(headerList[i].ColumnName)
filterVal = filterVal.concat(headerList[i].ColumnKey)
}
}
// console.log("dataList",dataList)
var data = dataList.map(v => filterVal.map(j => v[j]))
data.unshift(tHeader)
data.unshift([defaultTitle])
if (colNum === undefined) colNum = 1
var ws_name = 'SheetJS'
var wb = new Workbook(),
ws
if (div === undefined) {
ws = sheet_from_array_of_arrays(data, colNum)
} else if (div === 'hmzcjm') {
ws = sheet_from_array_of_arrays_hmzcjm(data)
} else if (div === 'jmmxb') {
var name = localStorage.username
var zbdw = localStorage.orgname
data.splice(1, 0, ['制表单位:' + zbdw + ' ' + '制表人:' + name])
ws = sheet_from_array_of_arrays_jmmxb(data)
}
/* add worksheet to workbook */
wb.SheetNames.push(ws_name)
wb.Sheets[ws_name] = ws
var wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: false, type: 'binary' })
var title = defaultTitle || '列表'
saveAs(new Blob([s2ab(wbout)], { type: 'application/octet-stream' }), title + '.xlsx')
}
export function export_json_to_excel_Merge(headerList, dataList, defaultTitle, colNum, div) {
/* original data */
var filterVal = []
var tHeader = []
var tHeader2 = []
var ArrMerge = []
var numberone = 0
var numbertwo = 0
headerList.forEach((item, index) => {
if (item.ColumnKey === '') {
ArrMerge.push({
s: { c: numberone + numbertwo, r: 1 },
e: { c: numberone + numbertwo + item.ColumnChild.length - 1, r: 1 }
})
item.ColumnChild.forEach((items, indexs) => {
;(tHeader2 = tHeader2.concat(items.ColumnName)),
(filterVal = filterVal.concat(items.ColumnKey)),
(tHeader = tHeader.concat(item.ColumnName)),
numberone++
})
} else {
tHeader2 = tHeader2.concat(item.ColumnName)
filterVal = filterVal.concat(item.ColumnKey)
tHeader = tHeader.concat(item.ColumnName)
numbertwo++
ArrMerge.push({ s: { c: numberone + numbertwo - 1, r: 1 }, e: { c: numberone + numbertwo - 1, r: 2 } })
}
})
ArrMerge.unshift({ s: { c: 0, r: 0 }, e: { c: numberone + numbertwo - 1, r: 0 } })
var data = dataList.map(v => filterVal.map(j => v[j]))
data.unshift(tHeader2)
data.unshift(tHeader)
data.unshift([defaultTitle])
if (colNum === undefined) colNum = 1
var ws_name = 'SheetJS'
var wb = new Workbook(),
ws
if (div === undefined) {
ws = sheet_from_array_of_arrays(data, colNum)
} else if (div === 'mergeheader') {
ws = sheet_from_array_of_arrays_mergeheader(data, ArrMerge)
} else if (div === 'hmzcjm') {
ws = sheet_from_array_of_arrays_hmzcjm(data)
} else if (div === 'jmmxb') {
var name = localStorage.username
var zbdw = localStorage.orgname
data.splice(1, 0, ['制表单位:' + zbdw + ' ' + '制表人:' + name])
ws = sheet_from_array_of_arrays_jmmxb(data)
}
/* add worksheet to workbook */
wb.SheetNames.push(ws_name)
wb.Sheets[ws_name] = ws
var wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: false, type: 'binary' })
var title = defaultTitle || '列表'
saveAs(new Blob([s2ab(wbout)], { type: 'application/octet-stream' }), title + '.xlsx')
}
在需要使用的地方复制使用
let headList=[{
ColumnAlign: "center",
ColumnKey:"a",
ColumnName: "测试a",
ColumnOrder: "CDHSTATUS",
ColumnWidth: 90},
{
ColumnAlign: "center",
ColumnKey:"b",
ColumnName: "测试b",
ColumnOrder: "CDHSTATUS",
ColumnWidth: 90
}]
let OutDataExcel=[{a:1,b:2....}]
let header = '测试表格' //文件名字
if (dataList.length === 0) {
this.$message.error('无导出数据!')
return
}
require.ensure([], () => {
const { export_json_to_excel } = require('@/vendor/Export2Excel')
export_json_to_excel(headList, OutDataExcel, header)
})
导出的就是正常的Excel文件