返回参数为对象,转为二进制,在生成excel表格下载

返回参数为对象,转为二进制,在生成excel表格下载
downloadFile() {
// 原对象
const list = {
Shisjine: this.data.Shisjine, // 实收金额
TotCnt: this.data.TotCnt, // 总笔数
TotAmt: this.data.TotAmt, // 总收款金额
IPAYRfndAmt: this.data.IPAYRfndAmt, // 退款金额
Shuliang: this.data.Shuliang, // 退款笔数
Quantity: this.data.Quantity, // 微信渠道收款笔数
PclupAmt: this.data.PclupAmt, // 微信渠道收款金额
ScssCnt: this.data.ScssCnt, // 支付宝渠道收款笔数
TxnAmt: this.data.TxnAmt, // 支付宝渠道收款金额
StrtNum: this.data.StrtNum, // 银联通道收款笔数
ActlPayAmt: this.data.ActlPayAmt, // 银联通道收款金额
SubAcctNum: this.data.SubAcctNum, // 本行通道收款笔数
RchrgAmt: this.data.RchrgAmt, // 本行通道收款金额
FeeAmt: this.data.FeeAmt, // 手续费
FeePrefRt: this.data.FeePrefRt, // 手续费减免
singleAmount: this.data.singleAmount, //单笔均价
};

// 属性名映射表
const propertyMap = {
  Shisjine: '实收金额', // 实收金额
  TotCnt: '总笔数', // 总笔数
  TotAmt: '总收款金额', // 总收款金额
  IPAYRfndAmt: '退款金额', // 退款金额
  Shuliang: '退款笔数', // 退款笔数
  Quantity: '微信渠道收款笔数', // 微信渠道收款笔数
  PclupAmt: '微信渠道收款金额', // 微信渠道收款金额
  ScssCnt: '支付宝渠道收款笔数', // 支付宝渠道收款笔数
  TxnAmt: '支付宝渠道收款金额', // 支付宝渠道收款金额
  StrtNum: '银联通道收款笔数', // 银联通道收款笔数
  ActlPayAmt: '银联通道收款金额', // 银联通道收款金额
  SubAcctNum: '本行通道收款笔数', // 本行通道收款笔数
  RchrgAmt: '本行通道收款金额', // 本行通道收款金额
  FeeAmt: '手续费', // 手续费
  FeePrefRt: '手续费减免', // 手续费减免
  singleAmount: '单笔均价', //单笔均价
};
// 创建新对象,将属性名转换为中文
const newList = {};
for (const key in list) {
  newList[propertyMap[key] || key] = list[key];
}

// 将对象转换为符合Excel格式的数据结构
const excelData = [
  Object.keys(newList), // Excel 表头
  Object.values(newList) // Excel 数据行
];

// 使用TextEncoder对象将数据转换为二进制数据
const textEncoder = new TextEncoder();
const excelString = excelData.map(row => row.join('\t')).join('\n');
const arrayBuffer = textEncoder.encode(excelString).buffer;

// 将二进制数据写入文件,并保存为Excel文件
const filePath = `${wx.env.USER_DATA_PATH}/汇总导出.xls`; // 文件路径

wx.getFileSystemManager().writeFile({
  filePath: filePath,
  data: arrayBuffer,
  success: function () {
    wx.openDocument({ // 打开文档
      filePath: filePath, //拿上面存入的文件路径
      showMenu: true, // 显示右上角菜单
      success: function (res) {
        console.log("打开文件", res);
      },
    })
    console.log('Excel文件写入成功', filePath);
    // 在这里可以继续处理Excel文件,例如保存或下载
  },
  fail: function (error) {
    console.error('写入文件失败', error);
  }
});

},

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值