update excel demo

This commit is contained in:
zhigang.li 2018-08-17 16:18:49 +08:00
parent 6f83ca202f
commit 05ac2b3916
3 changed files with 29 additions and 24 deletions

View File

@ -69,26 +69,26 @@ export const export_table_to_excel= (id, filename) => {
// XLSX.writeFile(wb, filename);
}
export const export_json_to_excel = ({data, key, title, filename, autoWidth}) => {
export const export_json_to_excel = ({data, key, title, filename, autoWidth}) => {
const wb = XLSX.utils.book_new();
data.unshift(title);
const ws = XLSX.utils.json_to_sheet(data, {header: key, skipHeader: true});
if(autoWidth){
const arr = json_to_array(key, data);
auto_width(ws, arr);
}
}
XLSX.utils.book_append_sheet(wb, ws, filename);
XLSX.writeFile(wb, filename + '.xlsx');
}
export const export_array_to_excel = ({key, data, title, filename, autoWidth}) => {
export const export_array_to_excel = ({key, data, title, filename, autoWidth}) => {
const wb = XLSX.utils.book_new();
const arr = json_to_array(key, data);
arr.unshift(title);
const ws = XLSX.utils.aoa_to_sheet(arr);
if(autoWidth){
auto_width(ws, arr);
}
}
XLSX.utils.book_append_sheet(wb, ws, filename);
XLSX.writeFile(wb, filename + '.xlsx');
}
@ -104,3 +104,10 @@ export const read = (data, type) => {
const results = XLSX.utils.sheet_to_json(worksheet);
return {header, results};
}
export default {
export_table_to_excel,
export_array_to_excel,
export_json_to_excel,
read
}

View File

@ -14,6 +14,7 @@
</div>
</template>
<script>
import excel from '@/libs/excel'
export default {
name: 'export-excel',
data () {
@ -56,17 +57,15 @@ export default {
exportExcel () {
if (this.tableData.length) {
this.exportLoading = true
import('@/libs/excel').then(excel => {
const params = {
title: ['一级分类', '二级分类', '三级分类'],
key: ['category1', 'category2', 'category3'],
data: this.tableData,
autoWidth: true,
filename: '分类列表'
}
excel.export_array_to_excel(params)
this.exportLoading = false
})
const params = {
title: ['一级分类', '二级分类', '三级分类'],
key: ['category1', 'category2', 'category3'],
data: this.tableData,
autoWidth: true,
filename: '分类列表'
}
excel.export_array_to_excel(params)
this.exportLoading = false
} else {
this.$Message.info('表格数据不能为空!')
}

View File

@ -33,6 +33,7 @@
</div>
</template>
<script>
import excel from '@/libs/excel'
export default {
name: 'upload-excel',
data () {
@ -93,15 +94,13 @@ export default {
reader.onload = e => {
this.$Message.info('文件读取成功')
const data = e.target.result
import('@/libs/excel').then(excel => {
const {header, results} = excel.read(data, 'array')
const tableTitle = header.map(item => { return { title: item, key: item } })
this.tableData = results
this.tableTitle = tableTitle
this.uploadLoading = false
this.tableLoading = false
this.showRemoveFile = true
})
const { header, results } = excel.read(data, 'array')
const tableTitle = header.map(item => { return { title: item, key: item } })
this.tableData = results
this.tableTitle = tableTitle
this.uploadLoading = false
this.tableLoading = false
this.showRemoveFile = true
}
}
},