改成导excel

This commit is contained in:
刘正航
2026-05-11 16:09:44 +08:00
parent 45bfa93e85
commit 25fd25005a
4 changed files with 107 additions and 33 deletions

View File

@@ -141,46 +141,66 @@ Page({
return [headers.join(','), ...rows].join('\n')
},
exportCSV() {
exportXLSX() {
const items = this.data.items
if (!items.length) {
wx.showToast({ title: '暂无识别结果可导出', icon: 'none' })
return
}
const csvContent = this.generateCSV()
const timestamp = new Date().toISOString().slice(0, 19).replace(/[T:]/g, '-')
const filename = `batch_detect_${timestamp}.csv`
wx.showLoading({ title: '生成文件中...' })
// 写入临时文件
const fs = wx.getFileSystemManager()
const tempPath = `${wx.env.USER_DATA_PATH}/${filename}`
const app = getApp()
const token = app.globalData.token || wx.getStorageSync('token') || ''
const baseURL = app.globalData.baseURL || 'http://127.0.0.1:5000/api'
try {
fs.writeFileSync(tempPath, csvContent, 'utf8')
wx.showModal({
title: '导出成功',
content: `CSV文件已生成是否打开查看\n文件名:${filename}`,
confirmText: '打开',
cancelText: '关闭',
success: (res) => {
if (res.confirm) {
wx.openDocument({
filePath: tempPath,
fileType: 'csv',
showMenu: true,
fail: (err) => {
console.error('打开文件失败', err)
wx.showToast({ title: '打开失败,请检查文件管理器', icon: 'none' })
}
})
}
wx.request({
url: `${baseURL}/spam/export/xlsx`,
method: 'POST',
data: { items },
header: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`
},
responseType: 'arraybuffer',
success(res) {
wx.hideLoading()
if (res.statusCode !== 200) {
wx.showToast({ title: '导出失败', icon: 'none' })
return
}
})
} catch (err) {
console.error('写入文件失败', err)
wx.showToast({ title: '导出失败', icon: 'none' })
}
const timestamp = new Date().toISOString().slice(0, 19).replace(/[T:]/g, '-')
const filename = `batch_detect_${timestamp}.xlsx`
const fs = wx.getFileSystemManager()
const tempPath = `${wx.env.USER_DATA_PATH}/${filename}`
try {
fs.writeFileSync(tempPath, res.data)
wx.openDocument({
filePath: tempPath,
fileType: 'xlsx',
showMenu: true,
success: () => {
wx.showToast({ title: '导出成功', icon: 'success' })
},
fail: (err) => {
console.error('打开文件失败', err)
wx.showToast({ title: '打开失败', icon: 'none' })
}
})
} catch (err) {
console.error('写入文件失败', err)
wx.showToast({ title: '导出失败', icon: 'none' })
}
},
fail(err) {
wx.hideLoading()
console.error('导出请求失败', err)
wx.showToast({ title: '导出失败,请检查网络', icon: 'none' })
}
})
},
copyCSVToClipboard() {