/*
* @Author: Jeffrey Wang
* @Desc: 整理强大的 SheetJS 功能,依赖 XLSX.js 和 FileSaver
* @Version: v1.6
* @Date: 2018-03-24 09:54:17
* @Last Modified by: Jeffrey Wang
* @Last Modified ~: 2019-10-03 23:12:00
*/
if (typeof layui === 'undefined' && typeof jQuery === 'undefined') {
console.error('非layui调用请先加载jQuery')
}
if (typeof jQuery !== 'undefined') {
$ = jQuery
}
LAY_EXCEL = {
/**
* 兼容老版本的导出函数
* @param {[type]} data [description]
* @param {[type]} filename [description]
* @param {[type]} type [description]
* @return {[type]} [description]
*/
downloadExl: function(data, filename, type) {
type = type ? type : 'xlsx';
this.exportExcel({sheet1: data}, filename+'.'+type, type, null);
},
/**
* 导出Excel并弹出下载框,具体使用方法和范围请参考文档
* @param data object
* @param {[type]} filename [description]
* @param {[type]} type [description]
* @param {[type]} opt [description]
* @return {[type]} [description]
*/
exportExcel : function(data, filename, type, opt) {
type = type ? type : 'xlsx';
filename = filename ? filename : '导出数据.'+type;
// 创建一个 XLSX 对象
var wb = XLSX.utils.book_new();
// 1. 定义excel对的基本属性
var Props = {
Title: filename,
Subject: 'Export From web browser',
Author: "excel.wj2015.com",
Manager: '',
Company: '',
Category: '',
Keywords: '',
Comments: '',
LastAuthor: '',
CreatedData: new Date(),
};
opt && opt.Props && (Props = $.extend(Props, opt.Props));
// 默认进行压缩
wb.compression = opt ? opt.compression : true
if(wb.compression !== false) {
wb.compression = true
}
wb.Props = Props;
// 特殊属性实现,比如合并单元格
var wbExtend = {
'!merges': null
,'!margins': null
,'!cols': null
,'!rows': null
,'!protect': null
,'!autofilter': null
};
opt && opt.extend && (wbExtend = $.extend(wbExtend, opt.extend));
// 清理空配置
for (var key in wbExtend) {
if (!wbExtend.hasOwnProperty(key)) {
continue;
}
if (!wbExtend[key]) {
delete wbExtend[key];
}
}
// 判断 data 如果是 sheet 级别数据,自动加 sheet1
if ($.isArray(data)) {
data = {sheet1: data};
}
for(var sheet_name in data) {
if (!data.hasOwnProperty(sheet_name)) {
continue;
}
var content = data[sheet_name];
// 2. 设置sheet名称
wb.SheetNames.push(sheet_name);
// 3. 分配工作表对象到 sheet
var is_aoa = false;
if (content.length && content[0] && $.isArray(content[0])) {
is_aoa = true;
}
if (is_aoa) {
ws = XLSX.utils.aoa_to_sheet(content);
} else {
var option = {};
if (content.length) {
option.headers = content.unshift();
option.skipHeader = true;
// 分离并重组样式
var splitRes = this.splitContent(content);
}
var ws = XLSX.utils.json_to_sheet(content, option);
// 特殊属性,支持单独设置某个sheet的属性
if (wbExtend[sheet_name]) {
$.extend(ws, wbExtend[sheet_name]);
} else {
$.extend(ws, wbExtend);
}
// 合并样式
if (typeof splitRes !== 'undefined') {
this.mergeCellOpt(ws, splitRes.style);
}
}
wb.Sheets[sheet_name] = ws;
};
// 4. 输出工作表
var wbout = XLSX.write(wb, {bookType: type, type: 'binary', cellStyles: true, compression: wb.compression});
// 5. 跨浏览器支持,采用 FileSaver 三方库
saveAs(new Blob([this.s2ab(wbout)], {type: "application/octet-stream"}), filename);
},
/**
* 分离内容和样式
* @param {[type]} content [description]
* @return {[type]} [description]
*/
splitContent: function(content) {
var styleContent = {};
// 扫描每个单元格,如果是对象则等表格转换完毕后分离出来重新赋值
for (var line = 0; line < content.length; line++) {
var lineData = content[line];
var rowIndex = 0;
for (var row in lineData) {
if (!lineData.hasOwnProperty(row)) {
continue;
}
var rowData = lineData[row];
if (typeof rowData === 'object') {
// typeof null == object
if (rowData !== null) {
styleContent[this.numToTitle(rowIndex+1)+(parseInt(line)+1)] = rowData;
} else {
lineData[row] = '';
}
} else {
// JeffreyWang 2019-03-10针对 0 的hack处理
if (rowData === 0) {
rowData = {
v: '0',
s: {
alignment: {
horizontal: 'right'
}
}
}
}
styleContent[this.numToTitle(rowIndex+1)+(parseInt(line)+1)] = rowData;
}
rowIndex++;
}
}
return {
content: content,
style: styleContent
};
},
/**
* 合并内容和样式
* @param {[type]} ws [description]
* @param {[type]} style [description]
* @return {[type]} [description]
*/
mergeCellOpt: function(ws, style) {
for (var row in style) {
if (!style.hasOwnProperty(row)) {
continue;
}
var rowOpt = style[row];
if (ws[row]) {
// 其他属性做一个初始化
var otherOpt = ['t', 'w', 'f', 'r', 'h', 'c', 'z', 'l', 's'];
for (var i = 0; i < otherOpt.length; i++) {
ws[row][otherOpt[i]] = ws[row][otherOpt[i]];
}
$.extend(ws[row], rowOpt);
}
}
},
/**
* 将table转换为JSON数据
* @param dom
*/
tableToJson: function(dom) {
dom = $(dom)
var head = []
dom.find('thead > tr').each(function () {
var line = []
$(this).find('td,th').each(function () {
line.push($(this).text())
})
head.push(line)
})
var body = [];
dom.find('tbody > tr').each(function () {
var line = []
$(this).find('td').each(function () {
line.push($(this).text())
})
body.push(line)
})
return {
head: head,
body: body
}
},
// 测试代码:
// for(i=1;i<100;i++){var change = layui.excel.numToTitle(i);console.log(i, change, layui.excel.titleToNum(change));}
// numsToTitle备忘录提效
numsTitleCache: {},
// titleToTitle 备忘录提效
titleNumsCache: {},
/**
* 将数字(从一开始)转换为 A、B、C...AA、AB
* @param {[int]} num [description]
* @return {[type]} [description]
*/
numToTitle: function(num) {
if (this.numsTitleCache[num]) {
return this.numsTitleCache[num];
}
var ans = '';
if (num > 26) {
// 要注意小心 26 的倍数导致的无限递归问题
var dec = num % 26;
ans = this.numToTitle((num - dec)/26) + this.numToTitle(dec?dec:26);
this.numsTitleCache[num] = ans;
this.titleNumsCache[ans] = num;
return ans;
} else {
// A 的 ascii 为 0,顺位相加
ans = String.fromCharCode(64 + num);
this.numsTitleCache[num] = ans;
this.titleNumsCache[ans] = num;
return ans;
}
},
/**
* 将A、B、AA、ABC转换为 1、2、3形式的数字
* @param {[type]} title [description]
* @return {number} [description]
*/
titleToNum: function(title) {
if (this.titleNumsCache[title]) {
return this.titleNumsCache[title];
}
var len = title.length;
var total = 0;
for (var index in title) {
if (!title.hasOwnProperty(index)) {
continue;
}
var char = title[index];
var code = char.charCodeAt() - 64;
total += code * Math.pow(26, len - index - 1);
}
this.numsTitleCac
layui数据表格导出Excel插件
需积分: 50 58 浏览量
2020-07-20
18:03:58
上传
评论
收藏 393KB RAR 举报
走向人生巅峰的雲瑞
- 粉丝: 3
- 资源: 8
最新资源
- 高等数学第一章第二节数列的极限
- Python 版冒泡排序算法源代码
- tensorflow-gpu-2.7.2-cp38-cp38-manylinux2010-x86-64.whl
- tensorflow-2.7.3-cp39-cp39-manylinux2010-x86-64.whl
- tensorflow-2.7.2-cp39-cp39-manylinux2010-x86-64.whl
- Python版本快速排序源代码
- Python 语言版的快速排序算法实现
- 450815388207377安卓_base.apk
- 超微主板 X9DRE-TF+ bios 支持 nvme启动
- 基于Python通过下载气象数据和插值拟合离散数据曲线实现对寒潮过程的能量分析
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈