我必须打印一个 10000 行的 excel。有什么方法可以使用 excel4node 批量打印 json 数组?
eg:MongoDB insertMany操作
我必须打印一个 10000 行的 excel。有什么方法可以使用 excel4node 批量打印 json 数组?
eg:MongoDB insertMany操作
为此,您可以创建一个以 json 作为输入的函数,并在函数内部对其进行迭代。因此,如果这是您想要的,您将能够将您的操作视为单一操作。
我从 npm 作者那里找到了一个要点。似乎不可能做到这一点
https://gist.github.com/natergj/46bc984855c4478f50d890353727514f
这是一个例子:
const xl = require('excel4node');
// Create workbook and add worksheet
const wb = new xl.Workbook();
const ws = wb.addWorksheet('Data', {
disableRowSpansOptimization: true,
});
// Generate some fake data for testing
const data = [];
const rowCount = 50000;
let row = 1;
while (row <= rowCount) {
data.push({
zone_name: `zone_name_${row}`,
circle_name: `circle_name_${row}`,
div_town_name: `div_town_name_${row}`,
tender_name: `tender_name_${row}`,
tkc_name: `tkc_name_${row}`,
work_name: `work_name_${row}`,
material_name: `material_name_${row}`,
insert_dttm: new Date(),
app_fname: `app_fname_${row}`,
ins_fname: `ins_fname_${row}`,
});
row += 1;
}
const startRow = 4;
// Add data to worksheet
if (data.length) {
data.forEach((item, i) => {
const currentRow = i + startRow;
const newtarget = 'newtarget';
const newprogress = 'newprogress';
ws.cell(currentRow, 1).number(i).style({
alignment: {
vertical: ['center'],
horizontal: ['left']
},
font: {
color: '000000',
size: 12
},
border: {
bottom: {
style: 'thin',
color: '000000'
},
right: {
style: 'thin',
color: '000000'
},
left: {
style: 'thin',
color: '000000'
},
top: {
style: 'thin',
color: '000000'
}
}
});
ws.cell(currentRow, 2).string(item.zone_name).style({
alignment: {
vertical: ['center'],
horizontal: ['left']
},
font: {
color: '000000',
size: 12
},
border: {
bottom: {
style: 'thin',
color: '000000'
},
right: {
style: 'thin',
color: '000000'
},
left: {
style: 'thin',
color: '000000'
},
top: {
style: 'thin',
color: '000000'
}
}
});
ws.cell(currentRow, 3).string(item.circle_name).style({
alignment: {
vertical: ['center'],
horizontal: ['left']
},
font: {
color: '000000',
size: 12
},
border: {
bottom: {
style: 'thin',
color: '000000'
},
right: {
style: 'thin',
color: '000000'
},
left: {
style: 'thin',
color: '000000'
},
top: {
style: 'thin',
color: '000000'
}
}
});
ws.cell(currentRow, 4).string(item.div_town_name).style({
alignment: {
vertical: ['center'],
horizontal: ['left']
},
font: {
color: '000000',
size: 12
},
border: {
bottom: {
style: 'thin',
color: '000000'
},
right: {
style: 'thin',
color: '000000'
},
left: {
style: 'thin',
color: '000000'
},
top: {
style: 'thin',
color: '000000'
}
}
});
ws.cell(currentRow, 5).string(item.tender_name).style({
alignment: {
vertical: ['center'],
horizontal: ['left']
},
font: {
color: '000000',
size: 12
},
border: {
bottom: {
style: 'thin',
color: '000000'
},
right: {
style: 'thin',
color: '000000'
},
left: {
style: 'thin',
color: '000000'
},
top: {
style: 'thin',
color: '000000'
}
}
});
ws.cell(currentRow, 6).string(item.tkc_name).style({
alignment: {
vertical: ['center'],
horizontal: ['left']
},
font: {
color: '000000',
size: 12
},
border: {
bottom: {
style: 'thin',
color: '000000'
},
right: {
style: 'thin',
color: '000000'
},
left: {
style: 'thin',
color: '000000'
},
top: {
style: 'thin',
color: '000000'
}
}
});
ws.cell(currentRow, 7).string(item.work_name).style({
alignment: {
vertical: ['center'],
horizontal: ['left']
},
font: {
color: '000000',
size: 12
},
border: {
bottom: {
style: 'thin',
color: '000000'
},
right: {
style: 'thin',
color: '000000'
},
left: {
style: 'thin',
color: '000000'
},
top: {
style: 'thin',
color: '000000'
}
}
});
ws.cell(currentRow, 8).string(item.material_name).style({
alignment: {
vertical: ['center'],
horizontal: ['left']
},
font: {
color: '000000',
size: 12
},
border: {
bottom: {
style: 'thin',
color: '000000'
},
right: {
style: 'thin',
color: '000000'
},
left: {
style: 'thin',
color: '000000'
},
top: {
style: 'thin',
color: '000000'
}
}
});
ws.cell(currentRow, 9).string(newtarget).style({
alignment: {
vertical: ['center'],
horizontal: ['left']
},
font: {
color: '000000',
size: 12
},
border: {
bottom: {
style: 'thin',
color: '000000'
},
right: {
style: 'thin',
color: '000000'
},
left: {
style: 'thin',
color: '000000'
},
top: {
style: 'thin',
color: '000000'
}
}
});
ws.cell(currentRow, 10).string(newprogress).style({
alignment: {
vertical: ['center'],
horizontal: ['left']
},
font: {
color: '000000',
size: 12
},
border: {
bottom: {
style: 'thin',
color: '000000'
},
right: {
style: 'thin',
color: '000000'
},
left: {
style: 'thin',
color: '000000'
},
top: {
style: 'thin',
color: '000000'
}
}
});
ws.cell(currentRow, 11).date(item.insert_dttm).style({
alignment: {
vertical: ['center'],
horizontal: ['left']
},
font: {
color: '000000',
size: 12
},
border: {
bottom: {
style: 'thin',
color: '000000'
},
right: {
style: 'thin',
color: '000000'
},
left: {
style: 'thin',
color: '000000'
},
top: {
style: 'thin',
color: '000000'
}
}
});
ws.cell(currentRow, 12).string(item.app_fname).style({
alignment: {
vertical: ['center'],
horizontal: ['left']
},
font: {
color: '000000',
size: 12
},
border: {
bottom: {
style: 'thin',
color: '000000'
},
right: {
style: 'thin',
color: '000000'
},
left: {
style: 'thin',
color: '000000'
},
top: {
style: 'thin',
color: '000000'
}
}
});
ws.cell(currentRow, 13).string(item.ins_fname).style({
alignment: {
vertical: ['center'],
horizontal: ['left']
},
font: {
color: '000000',
size: 12
},
border: {
bottom: {
style: 'thin',
color: '000000'
},
right: {
style: 'thin',
color: '000000'
},
left: {
style: 'thin',
color: '000000'
},
top: {
style: 'thin',
color: '000000'
}
}
});
});
}
// Write workbook once all data has been added to worksheet
wb.write('Overall_Progress_Report.xlsx');
如您所见,他迭代了 json 数组。