总结一下前端excel导出
if (cell.tagName === ‘TH’ && !cellObject.s) {
cellObject.s = {
font: { bold: true },//字体
fill:{
fgColor:{rgb:‘c6c6c6’},bgColor:{rgb:‘c6c6c6’}
},//背景色
alignment:{
vertical:‘center’,
horizontal:‘center’,
wrapText:false
},
border:{
top:{style:'thin',color:{rgb:'000000'}},
bottom:{style:'thin',color:{rgb:'000000'}},
left:{style:'thin',color:{rgb:'000000'}},
right:{style:'thin',color:{rgb:'000000'}},
}//边框
};
}
//数据样式
if (cell.tagName === 'TD' && !cellObject.s) {
cellObject.s = {
alignment:{
vertical:'center',
horizontal:'center',
wrapText:false
},
border:{
top:{style:'thin',color:{rgb:'000000'}},
bottom:{style:'thin',color:{rgb:'000000'}},
left:{style:'thin',color:{rgb:'000000'}},
right:{style:'thin',color:{rgb:'000000'}},
}
};
}
样式添加成功之后,合并单元格会遇到合并的单元格添加边框无效的问题
一直没找到解决的办法,最后求教大佬给了思路
逻辑就是根据合并单元格的配置,sheet的配置将所有合并掉的单个单元格都加上border配置。
var worksheet = cells.reduce(function (sheet, row, rowIndex) {
// iterate over all row cells
row.forEach(function (cell, columnIndex) {
lastColumn = Math.max(lastColumn, columnIndex);
// convert the row and column indices to a XLSX index
var ref = (0, _encodeCell.encodeCell)({
c: columnIndex,
r: rowIndex
});
// only save actual cells and convert them to XLSX-Cell objects
if (cell) {
sheet[ref] = (0, _cellToObject2.default)(cell, typeHandlers);
} else {
sheet[ref] = { t: 's', v: '',s:{
font: { bold: true },
fill:{
fgColor:{rgb:'c6c6c6'},bgColor:{rgb:'c6c6c6'}
},
alignment:{
vertical:'center',
horizontal:'center',
wrapText:false
},
border:{
top:{style:'thin',color:{rgb:'000000'}},
bottom:{style:'thin',color:{rgb:'000000'}},
left:{style:'thin',color:{rgb:'000000'}},
right:{style:'thin',color:{rgb:'000000'}},
}
}};
}
});
return sheet;
}, {});
还有导出日期格式设置、输入框input导出设置
***/ function(module, exports) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
/**
* Generates a cell object for an input field cell.
*
* @param {HTMLTableCellElement} cell - The cell.
*
* @returns {object} - A cell object of the cell or `null` if the cell doesn't
* fulfill the criteria of an input field cell.
*/
//输入框value值
exports.default = function (cell) {
var input = cell.querySelector('input');
var span = cell.querySelector('span[class="td_unit"]');
if(input!=null&&span!=null){
if (input) return { t: 's', v: input.value+''+span.innerText};
}
input = cell.querySelector('select');
if (input) return { t: 's', v: input.options[input.selectedIndex].textContent };
return null;
};
/***/ },
具体根据自己需求修改。
纠结了两天才解决掉,记录一下。