修改excel的样式,可以在转换数据之后进行修改
如: 需要导入xlsx 以及 xlsx-style依赖 以及 两个文件 Blob.js 和 Export2Excel.js
导入这两个js之后,修改下面的函数就可以进行自定义的样式修改了。
export function exportJsonToExceln (column, list, defaultTitle ,ranges, headnums) {
/* 表头 */
const tHeader = []
const filterVal = []
/* 获取列宽 */
const wpx = [];
const tTitle = [];
let tTitleName ='';
if (defaultTitle.indexOf('-') != -1 && defaultTitle.indexOf('-') == 5) {
tTitleName = defaultTitle.substring(defaultTitle.indexOf('-')+1)
}else {
tTitleName = defaultTitle
}
tTitle.push(tTitleName);
for (var i =0;i < column.length; ++i) {
tHeader.push(column[i].title);
filterVal.push(column[i].key);
var widthToWpx={wpx:''};
if(undefined===column[i].width){
/* 如果没有设置长度默认 */
widthToWpx.wpx=column[i].title.length*15+10
}else{
widthToWpx.wpx=column[i].width*1.5
}
wpx.push(widthToWpx)
}
/* 转换数据格式 */
const data =formatJson(filterVal, list)
data.unshift(tHeader);
/* 分析报表头部信息 */
if (otherInfo[0] !=1) {
for (var i =1;i < otherInfo[0];i++) {
data.unshift(otherInfo[i+1])
}
}
// 将导出内容标题放在数组的头部
data.unshift(tTitle);
// 针对从后台返回数据为null,由于null在导出的excel中是未定义的,所以不能设置样式,在这里去空
for (let i=otherInfo[0] +1;i < data.length - 2; i++) {
let dataj =data[i]
for (let j=0;j < dataj.length; j++ ){
if (dataj[j] ===null ||dataj[j] ===undefined) {
dataj[j] =''
}else if (!isNaN(dataj[j])) {
dataj[j] =dataj[j].toString()
}
}
}
/* 创建workbook对象,并且转换数据 */
var wb =new Workbook(),ws =sheet_from_array_of_arrays(data);
/* 设置单元格宽,单位为px */
ws['!cols']=wpx;
// 编辑excel格式,需要在此处添加对应的规则
ws['!merges'] = otherInfo[1];
let rawnums = column.length;
let colnums = list.length - otherInfo[0];
if (otherInfo[0] ==1) {
colnums =colnums -1
}
ws['A1'].s = {
font: {sz:20,bold:true,},
alignment: {
horizontal:"center",vertical:"center",wrap_text:true
}
};
// ws[num2] 是导出excel格式话的重点,也就是excel中各单元格的坐标
for(var i =1 ;i <=rawnums ;i++) {
let num =i +64;
let num2 =String.fromCharCode(num) + (otherInfo[0] +1);
if (ws[num2] !=undefined) {
ws[num2].s = {
font: {sz:10,bold:true,},
alignment: {
horizontal:"center",vertical:"center",wrap_text:true
},
border:{
top:{style:'thin'},left:{style:'thin'},bottom:{style:'thin'},right:{style:'thin'}
}
};
}
}
for(var i =1 ;i <=rawnums ;i++) {
let num =i +64;
for (var j =1;j <=colnums;j++) {
let num2 =String.fromCharCode(num) + (j + otherInfo[0] +1);
if (ws[num2] !=undefined) {
if (isNaN(ws[num2].v)) {
ws[num2].s = {
font: {sz:10,bold:false,},
alignment: {horizontal:"left",vertical:"center",wrap_text:true},
border: {top: {style:'thin'},left: {style:'thin'},bottom: {style:'thin'},right: {style:'thin'}}
};
}else {
ws[num2].s = {
font: {sz:10,bold:false,},
alignment: {horizontal:"right",vertical:"center",wrap_text:true},
border: {top: {style:'thin'},left: {style:'thin'},bottom: {style:'thin'},right: {style:'thin'}}
};
}
}
}
}
var ws_name ="Sheet1";
wb.SheetNames.push(ws_name);
wb.Sheets[ws_name] =ws;
var title = defaultTitle ||'列表';
var wbout =XLSX.write(wb, {bookType:'xlsx',bookSST:false,type:'binary'});
/* 创建二进制对象写入转换好的字节流*/
saveAs(new Blob([s2ab(wbout)], {type:"application/octet-stream"}),title +".xlsx");
}
下面是导出excel的样式,更多样式,读者可以自己尝试。。
谢谢!!!!