需求:app前端导出excel,不需要后台参与
方案1:用付费插件(Android读取excel或导出数据到Excel表 - DCloud 插件市场)实操确实可以用,可以设置各种属性,比较齐全
方案2:免费,缺点是有些属性目前不能适配,后续要研究一下,下面写下方案2的流程和代码
一、在comon创建exportExcel.js
// exportExcel.js 可以直接复制
let imgCount = 1;//图片计数器
/**
* 导出Excel
* @param {Object} fileName 文件名 会自动拼接时间戳防止重名
* @param {Object} data 数据内容 格式说明见下
* @param {Object} callback 导出成功的回调方法 返回文件保存路径
*
* 二维数组 其中外层数组中的每个元素为数组 对应一行 内层数组中的每个元素为object对象 对应每个单元格 属性如下:
* type 类型 text为文字 img为图片 默认文字
* width 单元格宽度 请带单位 默认300px
* height 单元格高度 请带单位 默认25px
* color 文字颜色 默认黑色
* fontSize 字号 请带单位 默认16px
* textAlign 文字对齐方式 默认left
* imgWidth 仅type为img时需要 图片宽度 无需带单位 默认25
* imgHeight 仅type为img时需要 图片高度 无需带单位 默认25
* content 单元格内容 type为img时为图片路径 仅支持base64
* colspan 跨列 默认1
* rowspan 跨行 默认1
*
* 示例:
* [
[{
content: '姓名',
color: 'blue',
type: 'text',
width: '200px',
height: '25px',
fontSize: '16px'
}, {
content: '性别',
color: 'blue',
type: 'text',
width: '200px',
height: '25px',
fontSize: '16px'
}, {
content: '头像',
color: 'blue',
type: 'text',
width: '200px',
height: '25px',
fontSize: '16px'
}],
[{
content: '张三',
color: 'blue',
type: 'text',
width: '200px',
height: '25px',
fontSize: '16px',
colspan: 2,
rowspan:2
}, {
content: 'base64图片',
type: 'img',
width: '200px',
height: '25px',
imgWidth: 25,
imgHeight: 25
}],
[{
content: '123',
color: 'blue',
type: 'text',
width: '200px',
height: '25px',
fontSize: '16px'
}]
]
*/
let doExport = function (fileName,data,callback){
imgCount = 1;
let date = new Date();
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
let hour = date.getHours();
let minute = date.getMinutes();
let second = date.getSeconds();
if(month < 10){
month = '0' + month;
}
if(day < 10){
day = '0' + day;
}
if(hour <