From 0367277f3dec3ac76ce19bb40d1e3ad162ebb507 Mon Sep 17 00:00:00 2001 From: 63f1bb74d2451104dc7d8b9f <63f1bb74d2451104dc7d8b9f@devide> Date: Sat, 1 Jul 2023 20:01:37 +0000 Subject: [PATCH] Auto Commit --- src/components/SearchGrade.vue | 39 +++++++++++++++++------ src/components/const.js | 9 ++---- src/components/writeExcel.js | 58 ++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 16 deletions(-) create mode 100644 src/components/writeExcel.js diff --git a/src/components/SearchGrade.vue b/src/components/SearchGrade.vue index 6a6aff8..a044d28 100644 --- a/src/components/SearchGrade.vue +++ b/src/components/SearchGrade.vue @@ -4,9 +4,11 @@ import Author from './Author.vue' import { tableColumns } from './const' import { getRemoteCsdnGrade } from '../service/csdnApi' import { onMounted, reactive, onUnmounted } from 'vue'; +import { exportExcel } from './writeExcel' const state = reactive({ - title:'csdn用户根据id快速查分数改文章', + title: 'csdn用户根据id快速查分数改文章', loading: false, + exportLoading: false, searchValue: localStorage.getItem('csdnUid') || '', firstChart: null, columns: tableColumns, @@ -152,8 +154,8 @@ function initFirstData() { show: true, position: 'middle', color: 'red', - lineHeight:35, - backgroundColor:'rgba(255,255,255.7)', + lineHeight: 35, + backgroundColor: 'rgba(255,255,255.7)', formatter: (params) => { console.log('params markline', params) return params.name + ":" + params.value @@ -184,6 +186,17 @@ function initFirstData() { } }); + const exportTableDataFunc = () => { + if (!state.dataSource || state.dataSource.length === 0) { + return window.alert('表格数据为空') + } + state.exportLoading = true + exportExcel(state.dataSource, '用户质量分').then(res => { + state.exportLoading = false + console.log('res', res) + }) + } + onMounted(() => { const storageUid = localStorage.getItem('csdnUid') console.log('window.localStorage', localStorage) @@ -215,6 +228,7 @@ function initFirstData() { onSearch() } }) + } @@ -231,8 +245,8 @@ function initFirstData() {
csdn用户id:
- -
+ + 导出
@@ -242,11 +256,18 @@ function initFirstData() { {{ record.url }} - diff --git a/src/components/const.js b/src/components/const.js index 0063fa8..89bccf2 100644 --- a/src/components/const.js +++ b/src/components/const.js @@ -1,9 +1,5 @@ export const tableColumns= [ { - title: '序号', - dataIndex: 'index', - fixed: true - }, { title: '文章名称', dataIndex: 'title', fixed: true @@ -28,7 +24,7 @@ export const tableColumns= [ sorter: (a, b) => a.diggCount - b.diggCount, },{ title: '质量分', - dataIndex: 'score', + key: 'score', sorter: (a, b) => a.score - b.score, }, { title: '发文时间', @@ -39,6 +35,5 @@ export const tableColumns= [ dataIndex: 'message', }, { title: '操作', - key: 'option', - fixed: true + key: 'option' }] \ No newline at end of file diff --git a/src/components/writeExcel.js b/src/components/writeExcel.js new file mode 100644 index 0000000..f5ac24f --- /dev/null +++ b/src/components/writeExcel.js @@ -0,0 +1,58 @@ +// 输出base64编码 +const base64 = s => window.btoa(unescape(encodeURIComponent(s))); +const tableToExcel = async (jsonData) => { + return new Promise(resolve => { + try { + console.log('start', jsonData) + let str = '' + Object.keys(jsonData[0]).forEach(name => { + const tdStr = `${name}` + str += tdStr + }) + str += '' + for (let i = 0; i < jsonData.length; i++) { + str += ''; + for (const key in jsonData[i]) { + // 增加\t为了不让表格显示科学计数法或者其他格式 + str += `${jsonData[i][key] + '\t'}`; + } + str += ''; + } + // Worksheet名 + const worksheet = '用户质量分' + const uri = 'data:application/vnd.ms-excel;base64,'; + // 下载的表格模板数据 + const template = ` + + + ${str}
`; + console.log('str', str) + // 下载模板 + window.location.href = uri + base64(template); + resolve(true) + } + catch (r) { + console.log(r, 'r') + resolve(false) + } + }) +}; + + +export async function exportExcel(tableData) { + return new Promise(async (resolve) => { + try { + await tableToExcel(tableData) + resolve(true) + } + catch (r) { + console.log(r, 'r') + resolve(false) + } + }) +} \ No newline at end of file -- GitLab