iview/src/components/table/export-csv.js

76 lines
2.4 KiB
JavaScript
Raw Normal View History

function has (browser) {
const ua = navigator.userAgent;
if (browser === 'ie') {
const isIE = ua.indexOf('compatible') > -1 && ua.indexOf('MSIE') > -1;
if (isIE) {
2016-12-25 22:49:42 +08:00
const reIE = new RegExp('MSIE (\\d+\\.\\d+);');
reIE.test(ua);
2016-12-25 22:49:42 +08:00
return parseFloat(RegExp['$1']);
} else {
2016-12-25 22:49:42 +08:00
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/);
2016-12-25 22:49:42 +08:00
const rv = navigator.userAgent.indexOf('rv:11.0');
if (ieold) {
iev = Number(RegExp.$1);
}
2016-12-25 22:49:42 +08:00
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) {
2016-12-25 22:49:42 +08:00
const BOM = '\uFEFF';
// Add BOM to text for open in excel correctly
2017-07-24 18:02:29 +08:00
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 (filename, text) {
if (has('ie') && has('ie') < 10) {
// has module unable identify ie11 and Edge
2016-12-25 22:49:42 +08:00
const oWin = window.top.open('about:blank', '_blank');
oWin.document.charset = 'utf-8';
oWin.document.write(text);
oWin.document.close();
oWin.document.execCommand('SaveAs', filename);
oWin.close();
2016-12-25 22:49:42 +08:00
} 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);
}
}
};
export default csv;