diff --git a/jshERP-web/package.json b/jshERP-web/package.json index ca5e0566..799c9305 100644 --- a/jshERP-web/package.json +++ b/jshERP-web/package.json @@ -21,6 +21,7 @@ "lodash.pick": "^4.4.0", "md5": "^2.2.1", "nprogress": "^0.2.0", + "xlsx": "^0.14.3", "viser-vue": "^2.4.4", "vue": "^2.6.10", "vue-area-linkage": "^5.1.0", diff --git a/jshERP-web/src/utils/util.js b/jshERP-web/src/utils/util.js index 16f7f572..b4372dd0 100644 --- a/jshERP-web/src/utils/util.js +++ b/jshERP-web/src/utils/util.js @@ -1,6 +1,6 @@ import * as api from '@/api/api' import { isURL } from '@/utils/validate' -import Vue from 'vue' +import XLSX from 'xlsx' export function timeFix() { const time = new Date() @@ -540,6 +540,35 @@ export function getNowFormatDateTime() { return currentdate; } +/** + * js获取当前时间, 格式“MMddHHMMSS” + */ +export function getNowFormatStr() { + var date = new Date(); + var month = date.getMonth() + 1; + var strDate = date.getDate(); + var strHours = date.getHours(); + var strMinutes = date.getMinutes(); + var strSeconds = date.getSeconds(); + if (month >= 1 && month <= 9) { + month = "0" + month; + } + if (strDate >= 0 && strDate <= 9) { + strDate = "0" + strDate; + } + if (strHours >= 0 && strHours <= 9) { + strHours = "0" + strHours; + } + if (strMinutes >= 0 && strMinutes <= 9) { + strMinutes = "0" + strMinutes; + } + if (strSeconds >= 0 && strSeconds <= 9) { + strSeconds = "0" + strSeconds; + } + var currentdate = month + strDate + strHours + strMinutes + strSeconds; + return currentdate; +} + /** * JS中根据指定值删除数组中的元素 * @param arrylist @@ -570,4 +599,58 @@ export function changeListFmtMinus(arr) { } } return newArr; +} + +/** + 通用的打开下载对话框方法,没有测试过具体兼容性 + @param url 下载地址,也可以是一个blob对象,必选 + @param saveName 保存文件名,可选 + */ +export function openDownloadDialog (url, saveName) { + if (typeof url === 'object' && url instanceof Blob) { + url = URL.createObjectURL(url) // 创建blob地址 + } + let aLink = document.createElement('a') + aLink.href = url + saveName = saveName + '_' + getNowFormatStr() + '.xlsx' + aLink.download = saveName || '' // HTML5新增的属性,指定保存文件名,可以不要后缀,注意,file:///模式下不会生效 + let event + if (window.MouseEvent) event = new MouseEvent('click') + else { + event = document.createEvent('MouseEvents') + event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null) + } + aLink.dispatchEvent(event) +} + +/** + * 将一个sheet转成最终的excel文件的blob对象,然后利用URL.createObjectURL下载 + * @param sheet + * @param sheetName + * @returns {Blob} + */ +export function sheet2blob (aoa, sheetName) { + let sheet = XLSX.utils.aoa_to_sheet(aoa) + sheetName = sheetName || 'sheet1' + let workbook = { + SheetNames: [sheetName], + Sheets: {} + } + workbook.Sheets[sheetName] = sheet + // 生成excel的配置项 + let wopts = { + bookType: 'xlsx', // 要生成的文件类型 + bookSST: false, // 是否生成Shared String Table,官方解释是,如果开启生成速度会下降,但在低版本IOS设备上有更好的兼容性 + type: 'binary' + } + let wbout = XLSX.write(workbook, wopts) + let blob = new Blob([s2ab(wbout)], { type: 'application/octet-stream' }) + // 字符串转ArrayBuffer + function s2ab (s) { + let buf = new ArrayBuffer(s.length) + let view = new Uint8Array(buf) + for (let i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF + return buf + } + return blob } \ No newline at end of file diff --git a/jshERP-web/src/views/report/AccountReport.vue b/jshERP-web/src/views/report/AccountReport.vue index 342585c5..4e9fa484 100644 --- a/jshERP-web/src/views/report/AccountReport.vue +++ b/jshERP-web/src/views/report/AccountReport.vue @@ -17,10 +17,11 @@ - + 查询 - 打印 + 打印 + 导出 @@ -63,6 +64,7 @@