实现目标
将所有订单信息导出到excel表格中
思路
1、在页面中bindtap绑定一个导出点击事件daochu()
2、先获取所有订单信息,并将数据添加到List数组中
3、传入以List数组作为参数,调用get_excel云函数
4、get_excel云函数利用node-xlsx第三方库,在云函数所在目录npm install安装node-xlsx
5、在云函数导入const xlsx = require('node-xlsx'),并设置表格第一行的列数据
6、循环List数组将数据依次添加到第二行、第三行、第四行等等。
7、云函数返回生成excel的临时地址
8、在页面js获取到该临时地址并预览excel表格
完整代码
saveexcel.js
const app = getApp()
Page({
/**
* 页面的初始数据
*/
data: {
},
daochu() {
var that = this
wx.showLoading({
title: '正在加载',
})
var List = []
wx.cloud.database().collection('yxb_nmkq_order').count().
then(async function name(res) {
console.log(res.total)
var batchTimes = Math.ceil(res.total / 20)
for (let index = 0; index < batchTimes; index++) {
console.log("第" + index + "次循环");
//异步转同步
var itemlist = await that.getitemlist(index)
List = List.concat(itemlist)
}
that.setData({
List: List
})
console.log("处理完毕", List);
wx.cloud.callFunction({
// 要调用的云函数名称
name: 'nmkq_get_excel',
// 传递给云函数的event参数
data: {
List: that.data.List
}
}).then(res => {
console.log("云函数结束");
console.log(res);
that.saveExcel(res.result.fileID)
}).catch(err => {
// handle error
wx.hideLoading({
success: (res) => {
wx.showToast({
title: '下载失败!',
icon: 'none'
})
},
})
})
})
},
getitemlist(index) {
return new Promise((rev, rej) => {
wx.cloud.database().collection('yxb_nmkq_order')
.skip(index * 20)
.limit(20)
.get({
success: function (res) {
// res.data 包含该记录的数据
console.log(res.data)
rev(res.data)
}
})
})
},
saveExcel(fileID) {
var that = this
wx.cloud.getTempFileURL({
fileList: [fileID],
success: res => {
that.setData({
fileUrl: res.fileList[0].tempFileURL
})
console.log("最后的地址" + that.data.fileUrl);
wx.downloadFile({
url: that.data.fileUrl,
success: (res) => {
that.setData({
httpfile: res.tempFilePath
})
console.log("找不到这个地址?" + that.data.httpfile);
//预览文件
wx.openDocument({
filePath: that.data.httpfile,
showMenu: true,
success: res => {
console.log("预览成功");
wx.hideLoading({
success: (res) => {},
})
},
fail: err => {
console.log(err);
}
})
},
fail: (err) => {
console.log('读取失败', err)
wx.showToast({
title: '读取失败!',
})
}
})
},
fail: err => {
console.log(err);
wx.showToast({
title: '下载失败2!',
icon: 'none'
})
}
})
},
})
saveexcel.json
{
"usingComponents": {},
"navigationBarTitleText": "数据导出excel"
}
saveexcel.wxml
<view style="width: 100%;display: flex;justify-content: center;">
<button style="width: 80%;margin: 20rpx 0;" bindtap="daochu">导出</button>
</view>
云函数写法:
云函数目录/index.js
// 云函数入口文件
const cloud = require('wx-server-sdk')
const xlsx = require('node-xlsx')
cloud.init()
// 云函数入口函数
exports.main = async (event, context) => {
console.log(event);
try {
let List = event.List
//2,定义存储数据的
let alldata = [];
let row = ['记录号', '订单号', '用户id', '商品名称', '付款价格', '手机号', '姓名', '数量', '备注', '创建时间', '状态值']; //表属性
alldata.push(row);
for (var i = 0; i < List.length; i++) {
let arr = [];
arr.push(List[i]._id);
arr.push(List[i].orderId);
arr.push(List[i].user._id);
arr.push(List[i].shop.title);
arr.push(List[i].fkprice);
arr.push(List[i].phone);
arr.push(List[i].username);
arr.push(List[i].number);
arr.push(List[i].bz);
arr.push(List[i].time));
arr.push(List[i].status);
alldata.push(arr)
}
console.log(alldata);
//3,把数据保存到excel里
var buffer = await xlsx.build([{
name: "mySheetName",
data: alldata
}]);
console.log(buffer);
//4,把excel文件保存到云存储里
return await cloud.uploadFile({
cloudPath: Date.now() + 'shuju.xlsx',
fileContent: buffer, //excel二进制文件
})
} catch (e) {
console.error(e)
return e
}
}
如果这篇文章对您有所帮助,给个小赞赞吧🌹