报表增加导出功能

This commit is contained in:
季圣华
2021-09-08 00:46:22 +08:00
parent 8a3b5f981e
commit 30b0e7ab37
5 changed files with 203 additions and 7 deletions

View File

@@ -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
}

View File

@@ -17,10 +17,11 @@
<a-input placeholder="请输入编号" v-model="queryParam.serialNo"></a-input>
</a-form-item>
</a-col>
<a-col :md="3" :sm="24">
<a-col :md="4" :sm="24">
<span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
<a-button type="primary" @click="searchQuery">查询</a-button>
<a-button style="margin-left: 8px" v-print="'#reportPrint'" type="primary" icon="printer">打印</a-button>
<a-button style="margin-left: 8px" v-print="'#reportPrint'" icon="printer">打印</a-button>
<a-button style="margin-left: 8px" @click="exportExcel" icon="download">导出</a-button>
</span>
</a-col>
<a-col :md="3" :sm="24">
@@ -63,6 +64,7 @@
<script>
import AccountInOutList from './modules/AccountInOutList'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { openDownloadDialog, sheet2blob} from "@/utils/util"
import JEllipsis from '@/components/jeecg/JEllipsis'
import {getAction} from '@/api/manage'
export default {
@@ -138,6 +140,15 @@
this.$refs.accountInOutList.show(record);
this.$refs.accountInOutList.title = "查看账户流水";
this.$refs.accountInOutList.disableSubmit = false;
},
exportExcel() {
let aoa = [['名称', '编号', '期初金额', '本月发生额', '账户流水']]
for (let i = 0; i < this.dataSource.length; i++) {
let ds = this.dataSource[i]
let itemDevice = [ds.name, ds.serialNo, ds.initialAmount, ds.thisMonthAmount, ds.currentAmount]
aoa.push(itemDevice)
}
openDownloadDialog(sheet2blob(aoa), '账户统计')
}
}
}

View File

@@ -35,7 +35,8 @@
<span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
<a-col :md="4" :sm="24">
<a-button type="primary" @click="searchQuery">查询</a-button>
<a-button style="margin-left: 8px" v-print="'#reportPrint'" type="primary" icon="printer">打印</a-button>
<a-button style="margin-left: 8px" v-print="'#reportPrint'" icon="printer">打印</a-button>
<a-button style="margin-left: 8px" @click="exportExcel" icon="download">导出</a-button>
</a-col>
</span>
<a-col :md="3" :sm="24">
@@ -80,7 +81,7 @@
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { getAction } from '@/api/manage'
import {queryMaterialCategoryTreeList} from '@/api/api'
import { getMpListShort } from "@/utils/util"
import { getMpListShort, openDownloadDialog, sheet2blob} from "@/utils/util"
import JEllipsis from '@/components/jeecg/JEllipsis'
import moment from 'moment'
import Vue from 'vue'
@@ -206,6 +207,16 @@
this.$refs.materialInOutList.show(record);
this.$refs.materialInOutList.title = "查看商品库存流水全部仓库";
this.$refs.materialInOutList.disableSubmit = false;
},
exportExcel() {
let aoa = [['条码', '名称', '规格', '型号', '颜色', '类别', '单位', '单价', '初始库存', '当前库存', '当前库存金额']]
for (let i = 0; i < this.dataSource.length; i++) {
let ds = this.dataSource[i]
let itemDevice = [ds.mBarCode, ds.name, ds.standard, ds.model, ds.color, ds.categoryName, ds.unitName,
ds.purchaseDecimal, ds.initialStock, ds.currentStock, ds.currentStockPrice]
aoa.push(itemDevice)
}
openDownloadDialog(sheet2blob(aoa), '商品库存')
}
}
}