最近做微信小程序相關(guān)項(xiàng)目需要將數(shù)據(jù)導(dǎo)出為excel形式,在網(wǎng)上查了許多資料來(lái)實(shí)現(xiàn)這個(gè)功能,以下是我使用的方法,特此記錄一下,以便之后使用。
解決方法:使用sheetJS代碼插件實(shí)現(xiàn)
github地址:https://github.com/SheetJS/sheetjs
下載地址:https://cdn.sheetjs.com/
代碼如下,具體使用根據(jù)情況操作:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-798699.html
// 數(shù)據(jù)
let data=[
{
id:1,
name:'a'
},{
id:2,
name:'b'
},{
id:3,
name:'c'
}]
let sheet = []
// 表頭
let title = ['序號(hào)', '姓名']
sheet.push(title)
// 數(shù)據(jù)
data.forEach(item => {
let rowcontent = []
rowcontent.push(item.id)
rowcontent.push(item.name)
sheet.push(rowcontent)
})
// XLSX插件使用
var ws = XLSX.utils.aoa_to_sheet(sheet);
var wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "導(dǎo)出信息");
var fileData = XLSX.write(wb, {
bookType: "xlsx",
type: 'base64'
});
// 保存的本地地址
console.log(wx.env.USER_DATA_PATH)
let filePath = `${wx.env.USER_DATA_PATH}/導(dǎo)出信息.xlsx`
// 寫文件
const fs = wx.getFileSystemManager()
fs.writeFile({
filePath: filePath,
data: fileData,
encoding: 'base64',
success(res) {
console.log(res)
const sysInfo = wx.getSystemInfoSync()
// 導(dǎo)出
if (sysInfo.platform.toLowerCase().indexOf('windows') >= 0) {
// 電腦PC端導(dǎo)出
wx.saveFileToDisk({
filePath: filePath,
success(res) {
console.log(res)
},
fail(res) {
console.error(res)
util.tips("導(dǎo)出失敗")
}
})
} else {
// 手機(jī)端導(dǎo)出
// 打開(kāi)文檔
wx.openDocument({
filePath: filePath,
success: function (res) {
console.log('打開(kāi)文檔成功')
},
fail: console.error
})
}
},
fail(res) {
console.error(res)
if (res.errMsg.indexOf('locked')) {
wx.showModal({
title: '提示',
content: '文檔已打開(kāi),請(qǐng)先關(guān)閉',
})
}
}
})
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-798699.html
到了這里,關(guān)于前端實(shí)現(xiàn)微信小程序JSON數(shù)據(jù)導(dǎo)出Excel表的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!