Export table reference iview
- Export Table header data and the need for specific data
let columns = [
{ title: '品牌', key: 'brandName' },
{ title: '品种', key: 'breedName' },
{ title: '平均零售价', key: 'avgRetailPrice' },
{ title: '平均批发价', key: 'avgBatchPrice' },
{ title: '平均成本价', key: 'avgCostPrice' },
{ title: '预估折扣', key: 'discount' },
{ title: '加盟分成', key: 'franchiseeSharing' },
{ title: '总部毛利率', key: 'headquartersRossInterestRate' },
{ title: '加盟分成金额', key: 'franchiseeSharingAmount' },
{ title: '总部分成金额', key: 'headquartersSharingAmount' },
{ title: '总部分成利润', key: 'headquartersProfits' },
{ title: '总部分成', key: 'headquartersSharing' },
];
let datas = [
{
"franchiseeSharing": 0,
"brandName": "安姿",
"avgCostPrice": 36.1,
"discount": 1,
"headquartersRossInterestRate": 0.9118,
"categoryName": "太阳镜",
"avgBatchPrice": 44.81,
"headquartersSharing": 1,
"headquartersProfits": 373.25,
"avgRetailPrice": 409.35,
"headquartersSharingAmount": 409.35,
"franchiseeSharingAmount": 0
},
{
"franchiseeSharing": 0,
"brandName": "安姿",
"avgCostPrice": 36.1,
"discount": 0.9,
"headquartersRossInterestRate": 0.902,
"categoryName": "太阳镜",
"avgBatchPrice": 44.81,
"headquartersSharing": 1,
"headquartersProfits": 332.32,
"avgRetailPrice": 409.35,
"headquartersSharingAmount": 368.42,
"franchiseeSharingAmount": 0
},
]
- Converts the data format
used between each column '' is connected, header and source data for each row by '\ r \ n' is connected
csv(columns, datas, options) {
let defaults = {
separator: ',',
quoted: false
}, newLine = '\r\n';
options = Object.assign({}, defaults,options);
let columnOrder;
const content = [], column = [];
columnOrder = columns.map(v => {
column.push(v.title)
return v.key
})
appendLine(content, column, options)
if (Array.isArray(datas)) {
datas.forEach(row => {
if (!Array.isArray(row)) {
row = columnOrder.map(k => row[k])
}
appendLine(content, row, options)
})
}
function appendLine(content, row, { separator, quoted }) {
const line = row.map(data => {
return data
})
content.push(line.join(separator))
}
return content.join(newLine)
},
- Export
exportCsv() {
function has(browser) {
const ua = window.navigator.userAgent;
if (browser === 'ie') {
const isIE = ua.indexOf('compatible') > -1 && ua.indexOf('MSIE') > -1;
if (isIE) {
const reIE = new RegExp('MSIE (\\d+\\.\\d+)');
reIE.test(us);
return parseFloat(RegExp['$1'])
} else {
return false
}
} else {
return ua.indexOf(browser) > -1
}
}
const csv = {
_isIE11() {
let iev = 0;
const ieold = (/MSIE (\d+\.\d+);/.test(navigator.userAgent));
const trident = !!navigator.userAgent.match(/Trident\/7.0/);
const rv = navigator.userAgent.indexOf('rv:11.0');
if (ieold) {
iev = Number(RegExp.$1);
}
if (navigator.appVersion.indexOf('MSIE 10') !== -1) {
iev = 10;
}
if (trident && rv !== -1) {
iev = 11;
}
return iev === 11;
},
_isEdge() {
return /Edge/.test(navigator.userAgent);
},
_getDownloadUrl(text) {
const BOM = '\uFEFF';
if (window.Blob && window.URL && window.URL.createObjectURL) {
const csvData = new Blob([BOM + text], { type: 'text/csv' });
return URL.createObjectURL(csvData)
} else {
return 'data:attachment/csv;charset=utf-8,' + BOM + encodeURIComponent(text)
}
},
download: function (fileName, text) {
if (has('ie') && has('ie') < 10) {
const oWin = window.top.open('about:blank', '_blank');
oWin.document.charset = 'utf-8';
oWin.document.write(text);
oWin.document.close();
oWin.document.execCommand('SaveAs',false, fileName);
oWin.close()
} else if (has('ie') === 10 || this._isIE11() || this._isEdge()) {
const BOM = '\uFEFF';
const csvData = new Blob([BOM + text], { type: 'text/csv' });
navigator.msSaveBlob(csvData, fileName);
} else {
const link = document.createElement('a');
link.download = fileName;
link.href = this._getDownloadUrl(text);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
};
return csv;
}
- Full use
const params = {
filename: 'table.csv',
original: true,
};
let columns = [], datas = [];
const data = this.csv(columns, datas, params);
exportCsv().download(params.filename, data);