当前位置: 首页 > news >正文

uniapp app 导出excel 表格

直接复制运行   

<template><view><button @click="tableToExcel">导出一个表来看</button><view>{{ successTip }}</view></view>
</template><script>export default {data() {return {successTip: ''}},methods: {uuid() {return 'xxx-xxx-xxx'.replace(/[xy]/g, c => {var r = Math.random() * 16 | 0,v = c == 'x' ? r : (r & 0x3 | 0x8)return v.toString(16)})},tableToExcel() {// 要导出的json数据const jsonData = [{name: '科比',phone: '123456',email: '科比@163.com'},{name: '科比',phone: '123456',email: '科比@163.com'},{name: '科比',phone: '123456',email: '科比@163.com'},{name: '科比',phone: '123456',email: '科比@163.com'},]// 列标题let worksheet = 'sht1'let str ='<tr><td style="text-align: center">姓名</td><td style="text-align: center">电话</td><td style="text-align: center">邮箱</td></tr>'// 循环遍历,每行加入tr标签,每个单元格加td标签for (let i = 0; i < jsonData.length; i++) {str += '<tr>'for (let item in jsonData[i]) {// 增加\t为了不让表格显示科学计数法或者其他格式str += `<td>${jsonData[i][item] + '\t'}</td>`}str += '</tr>'}// 下载的表格模板数据let template = `<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml encoding="UTF-8"><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>${worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>${str}</table></body></html>`// 下载模板// #ifdef APP-PLUSthis.appExportFile(template)// #endif// 下载模板// #ifdef MP-WEIXINthis.wxExportFile(template)// #endif},// 导出文件到手机 fileData:要写入到文件的数据,返回参数为文档路径appExportFile(fileData, documentName = '项目Excel文件') {// PUBLIC_DOCUMENTS: 程序公用文档目录常量plus.io.requestFileSystem(plus.io.PUBLIC_DOCUMENTS, fs => {let rootObj = fs.root,fullPath = rootObj.fullPathconsole.log('开始导出数据********')// 创建文件夹rootObj.getDirectory(documentName, {create: true}, dirEntry => {// 创建文件,防止重名let fileName = 'excel' + this.uuid() + '.xlsx'dirEntry.getFile(fileName, {create: true}, fileEntry => {fileEntry.createWriter(writer => {writer.onwritestart = res => console.log('正在导出')//  /storage/emulated/0指的就是系统路径let pathStr = fullPath.replace('/storage/emulated/0', '')// 成功导出数据writer.onwrite = res => {// 文件路径let filePath = res.target.fileNameuni.hideLoading()setTimeout(() => {console.log('成功导出')this.successTip = `文件位置:${filePath}`uni.openDocument({filePath,success: res => console.log('打开文档成功'),fail: err => console.log(err)})}, 500)}// 写入内容writer.write(fileData)}, err => console.log(err.message))})})})},wxExportFile(template, documentName = '项目Excel文件') {const fs = wx.getFileSystemManager()// 创建文件名字, 防止重名let filePath = wx.env.USER_DATA_PATH + '/' + (documentName + this.uuid()) + '.xls'fs.writeFile({filePath,data: template,encoding: 'utf8',success: res => {wx.openDocument({filePath,showMenu: true, //是否显示右上角菜单success: res => console.log('打开文档成功。文件位置', filePath),fail: err => console.log(err)})},fail: err => console.info(err)})}}}
</script>

  这里用到小程序和app段的api

wx.getFileSystemManager()
获取全局唯一的文件管理器

返回值
FileSystemManager
 

wx.openDocument 预览文件
wx.openDocument({filePath,showMenu:true, //是否右上角菜单,这样就可以转发给好友了success: res => console.log('打开文档成功。文件位置', filePath),fail: err => console.log(err)})

uniapp 微信小程序 /APP json数据导出excel文件_uniapp导出excel文件_初遇你时动了情的博客-CSDN博客 

 

http://www.lryc.cn/news/183026.html

相关文章:

  • 【RabbitMQ】常用消息模型详解
  • 图像拼接后丢失数据,转tiff报错rasterfile failed: an unknown
  • Nginx之日志模块解读
  • latex方程组编写,一种可以保证方程编号自适应的方法
  • 深度学习基础 2D卷积(1)
  • OpenCV DNN C++ 使用 YOLO 模型推理
  • 第八章 Linux文件系统权限
  • XXL-JOB源码梳理——一文理清XXL-JOB实现方案
  • java做个qq机器人
  • 前端 | AjaxAxios模块
  • 高效的ProtoBuf
  • 删除SQL记录
  • 数据结构--》探索数据结构中的字符串结构与算法
  • 云安全之等级保护详解
  • VUE状态持久化,储存动态路由
  • 微信小程序代驾系统源码(含未编译前端,二开无忧) v2.5
  • 1797_GNU pdf阅读器evince
  • 网络-跨域解决
  • git提交代码的流程
  • 【SpringBoot】配置文件详解
  • 一文讲懂-五险一金
  • 判断三条边是否构成三角形(Python实现)
  • The directory ‘*‘ or its parent directory is not owned by the current user
  • leetcode做题笔记162. 寻找峰值
  • nginx负载转发源请求http/https:X-Forwarded-Proto及nginx中的转发报头
  • Docker compose插件安装
  • 【数据结构与算法】树、二叉树的概念及结构(详解)
  • 函数指针数组指针(指向函数指针数组的指针)
  • 经典算法-----汉诺塔问题
  • 博客之站项目测试报告