一、excel文件批量導(dǎo)入數(shù)據(jù)到數(shù)據(jù)庫(kù)
1.創(chuàng)建node.js函數(shù)upload,點(diǎn)擊upload右鍵外部終端中安裝類庫(kù)
npm install node-xlsx
2.云函數(shù)代碼文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-532112.html
const cloud = require('wx-server-sdk')
cloud.init({
env:'云環(huán)境'
})
var xlsx = require('node-xlsx');
const db = cloud.database()
exports.main = async(event, context) => {
let fileID=event.fileID
//1,通過(guò)fileID下載云存儲(chǔ)里的excel文件
const res = await cloud.downloadFile({
fileID: fileID,
})
console.log('下載的文件',res);
const file_xlsx = res.fileContent
var files = xlsx.parse(file_xlsx);
console.log('獲得內(nèi)容表格數(shù)組',files[0].data);
function addfile(i){
db.collection("product_new").add({
data:{
product_name: files[0].data[i][0], //商品名
product_classify: files[0].data[i][1], //分類
product_price: files[0].data[i][2], //價(jià)格
product_size: files[0].data[i][3], //規(guī)格
bank:files[0].data[i][4], //品牌
festival:files[0].data[i][5], //節(jié)日
product_stock:files[0].data[i][6], //庫(kù)存量
product_seles:files[0].data[i][7], //銷售量
product_origin:files[0].data[i][8], //產(chǎn)地
product_store:files[0].data[i][9], //存儲(chǔ)方式
product_warranty:files[0].data[i][10], //保質(zhì)期
openid:event.openid,
product_time:event.times,
}
}).then(res=>{
i++
if(i==files[0].data.length){
//循環(huán)結(jié)束刪除上傳的文件不占用云存儲(chǔ)
cloud.deleteFile({
fileList:[fileID],
success(res){
return console.log(res,'刪除文件')
},
})
}else{
addfile(i)
}
})
}
addfile(1)
}
js代碼文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-532112.html
// 上傳表格點(diǎn)擊事件
chooseExcel(){
var that=this
wx.chooseMessageFile({
count: 1,
type:'file',
success(res){
var path=res.tempFiles[0].path;
console.log("選擇excel文件成功",path)
that.uploadExcel(path);
}
})
},
// 上傳表格
uploadExcel(path){
var that=this
wx.cloud.uploadFile({
cloudPath:new Date().getTime()+'.xls',
filePath:path, //文件路徑
success:res=>{
console.log("上傳成功",res.fileID)
that.jiexi(res.fileID)
},
fail:err=>{
console.log("上傳失敗",err)
}
})
},
// 解析并上傳數(shù)據(jù)庫(kù)
jiexi(fileId){
wx.cloud.callFunction({
name:"upload",
data:{
fileID:fileId,
openid:this.data.openid,
times:this.data.times
},
success(res){
console.log("解析并上傳成功",res)
wx.showModal({
title: '溫馨提示',
content: '商品信息上傳成功',
success: function (res) {
if (res.confirm) {//這里是點(diǎn)擊了確定以后
console.log('用戶點(diǎn)擊確定')
} else {//這里是點(diǎn)擊了取消以后
console.log('用戶點(diǎn)擊取消')
}
}
})
},
fail(res){
console.log("解析失敗",res)
}
})
},
到了這里,關(guān)于微信小程序 -- 數(shù)據(jù)庫(kù)數(shù)據(jù)excel文件批量導(dǎo)入的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!