Skip to content

Commit b83d9ca

Browse files
author
黄书伟
committed
添加导出csv 文件
1 parent 5393b30 commit b83d9ca

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed

libs/v-table/src/export-csv.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
7+
function getDownloadUrl(content) {
8+
9+
var BOM = '\uFEFF';
10+
11+
if (window.Blob && window.URL && window.URL.createObjectURL) {
12+
var csvData = new Blob([BOM + content], { type: 'text/csv' });
13+
return URL.createObjectURL(csvData);
14+
} else {
15+
return 'data:attachment/csv;charset=utf-8,' + BOM + encodeURIComponent(content);
16+
}
17+
}
18+
19+
function getContent(columns, tableData) {
20+
var separator = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ',';
21+
22+
23+
var newLine = '\r\n';
24+
25+
var result = [],
26+
csvRows = [],
27+
_tableData = [];
28+
29+
if (Array.isArray(columns) && columns.length > 0) {
30+
31+
csvRows = columns.map(function (x) {
32+
33+
return x.title;
34+
});
35+
36+
result.push(csvRows.join(separator));
37+
}
38+
39+
if (Array.isArray(tableData) && tableData.length > 0) {
40+
41+
tableData.forEach(function (row) {
42+
43+
csvRows = columns.map(function (col) {
44+
45+
if (row[col.field]) {
46+
47+
return row[col.field];
48+
}
49+
50+
return '';
51+
});
52+
53+
result.push(csvRows.join(separator));
54+
});
55+
}
56+
57+
return result.join(newLine);
58+
}
59+
60+
var csv = {
61+
download: function download(fileName, columns, tableData) {
62+
63+
var content = getContent(columns, tableData);
64+
65+
if (content && content.length > 0) {
66+
67+
var link = document.createElement('a');
68+
link.download = fileName;
69+
link.href = getDownloadUrl(content);
70+
document.body.appendChild(link);
71+
link.click();
72+
document.body.removeChild(link);
73+
} else {
74+
75+
console.error('vue-easytable::no export data');
76+
}
77+
}
78+
};
79+
80+
exports.default = csv;

packages/v-table/src/export-csv.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// 有csv 根式有很大的局限性,不能合并单元格,目前先不上此功能
2+
function getDownloadUrl(content) {
3+
4+
const BOM = '\uFEFF';
5+
// Add BOM to text for open in excel correctly
6+
if (window.Blob && window.URL && window.URL.createObjectURL) {
7+
const csvData = new Blob([BOM + content], { type: 'text/csv' });
8+
return URL.createObjectURL(csvData);
9+
} else {
10+
return 'data:attachment/csv;charset=utf-8,' + BOM + encodeURIComponent(content);
11+
}
12+
}
13+
14+
function getContent(columns,tableData,separator = ',') {
15+
16+
const newLine = '\r\n';
17+
18+
let result = [],csvRows=[],_tableData=[];
19+
20+
if (Array.isArray(columns) && columns.length > 0){
21+
22+
csvRows = columns.map(x=>{
23+
24+
return x.title;
25+
})
26+
27+
result.push(csvRows.join(separator))
28+
}
29+
30+
if (Array.isArray(tableData) && tableData.length > 0){
31+
32+
tableData.forEach(row=>{
33+
34+
csvRows = columns.map(col=>{
35+
36+
if (row[col.field]){
37+
38+
return row[col.field];
39+
}
40+
41+
return '';
42+
})
43+
44+
result.push(csvRows.join(separator))
45+
})
46+
}
47+
48+
return result.join(newLine);
49+
}
50+
51+
const csv = {
52+
53+
download(fileName,columns,tableData){
54+
55+
56+
let content = getContent(columns,tableData);
57+
58+
if (content && content.length > 0){
59+
60+
const link = document.createElement('a');
61+
link.download = fileName;
62+
link.href = getDownloadUrl(content);
63+
document.body.appendChild(link);
64+
link.click();
65+
document.body.removeChild(link);
66+
}else{
67+
68+
console.error('vue-easytable::no export data');
69+
}
70+
}
71+
}
72+
73+
export default csv;

0 commit comments

Comments
 (0)