You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
235 lines
6.2 KiB
235 lines
6.2 KiB
'use strict';
|
|
const fs = require('fs');
|
|
const xlsx = require('better-xlsx');
|
|
const path = require('path')
|
|
const moment = require('moment')
|
|
|
|
//递归创建目录 同步方法
|
|
async function makeDir (dir) {
|
|
if (!fs.existsSync(dir)) {
|
|
makeDir(path.dirname(dir))
|
|
fs.mkdirSync(dir, function (err) {
|
|
if (err) {
|
|
throw err
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
async function gatherSheet ({ sheet_2, data = [] }) {
|
|
|
|
// header
|
|
const headerStyle = new xlsx.Style();
|
|
headerStyle.align.h = 'center';
|
|
headerStyle.align.v = 'center';
|
|
headerStyle.border.right = 'thin';
|
|
headerStyle.border.rightColor = '#000000';
|
|
headerStyle.border.bottom = 'thin';
|
|
headerStyle.border.bottomColor = '#000000';
|
|
|
|
const headerRow1 = sheet_2.addRow();
|
|
const indexCell1 = headerRow1.addCell();
|
|
indexCell1.value = '南昌县农村公路养护管理暨用地范围内环境整治提升工程 考核汇总表'
|
|
indexCell1.style = headerStyle
|
|
indexCell1.hMerge = 14
|
|
|
|
const headerRow2 = sheet_2.addRow();
|
|
const indexCell2 = headerRow2.addCell();
|
|
indexCell2.value = '责任单位'
|
|
indexCell2.style = headerStyle
|
|
indexCell2.vMerge = 1
|
|
|
|
|
|
for (let h of ['县道', '', '', '', '乡道', '', '', '', '村道', '', '', '',]) {
|
|
const cell = headerRow2.addCell();
|
|
|
|
cell.value = h;
|
|
cell.style = headerStyle
|
|
if (h) {
|
|
cell.hMerge = 3
|
|
|
|
}
|
|
}
|
|
|
|
const header = [{
|
|
key: 'name',
|
|
title: '',
|
|
}, {
|
|
key: 'county',
|
|
title: '总里程',
|
|
}, {
|
|
key: 'countyParticipate',
|
|
title: '纳入考核里程',
|
|
}, {
|
|
key: 'countyPresent',
|
|
title: '本次考核里程',
|
|
}, {
|
|
key: 'countyDifferenceValue',
|
|
title: '实际抽取比原计划多',
|
|
}, {
|
|
key: 'township',
|
|
title: '总里程',
|
|
}, {
|
|
key: 'townshipParticipate',
|
|
title: '纳入考核里程',
|
|
}, {
|
|
key: 'townshipPresent',
|
|
title: '本次考核里程',
|
|
}, {
|
|
key: 'townshipDifferenceValue',
|
|
title: '实际抽取比原计划多',
|
|
}, {
|
|
key: 'village',
|
|
title: '总里程',
|
|
}, {
|
|
key: 'villageParticipate',
|
|
title: '纳入考核里程',
|
|
}, {
|
|
key: 'villagePresent',
|
|
title: '本次考核里程',
|
|
}, {
|
|
key: 'villageDifferenceValue',
|
|
title: '实际抽取比原计划多',
|
|
},
|
|
{
|
|
key: 'drawPeople',
|
|
title: '抽查人',
|
|
},
|
|
{
|
|
key: 'abstractTime',
|
|
title: '抽查时间',
|
|
},]
|
|
|
|
const headerRow3 = sheet_2.addRow();
|
|
for (let h of header) {
|
|
const cell = headerRow3.addCell();
|
|
cell.value = h.title;
|
|
cell.style = headerStyle
|
|
}
|
|
|
|
// data
|
|
const style = new xlsx.Style();
|
|
style.align.h = 'left';
|
|
style.align.v = 'center';
|
|
style.border.right = 'thin';
|
|
style.border.rightColor = '#000000';
|
|
style.border.bottom = 'thin';
|
|
style.border.bottomColor = '#000000';
|
|
|
|
let rowGather = sheet_2.addRow();
|
|
for (let h of header) {
|
|
if (h.key != 'drawPeople' && h.key != 'abstractTime') {
|
|
const cell = rowGather.addCell();
|
|
cell.value = h.key == 'name' ? '汇总' : data.reduce((a, b) => a + (b[h.key] || 0), 0)
|
|
cell.style = style
|
|
}
|
|
|
|
}
|
|
|
|
for (let i = 0; i < data.length; i++) {
|
|
const row = sheet_2.addRow();
|
|
for (let h of header) {
|
|
const cell = row.addCell();
|
|
if (h.key == 'abstractTime') {
|
|
cell.value = moment(data[i][h.key]).format('YYYY-MM-DD HH:mm:ss')
|
|
} else {
|
|
cell.value = data[i][h.key]
|
|
}
|
|
|
|
cell.style = style
|
|
}
|
|
}
|
|
}
|
|
|
|
async function simpleExcelDown ({ data = [], header = [], fileName = moment().format('YYYY-MM-DD HH:mm:ss'), exp, gather } = {}) {
|
|
const fileDirPath = path.join(__dirname, `../../downloadFiles`)
|
|
makeDir(fileDirPath)
|
|
const file = new xlsx.File();
|
|
const sheet_1 = file.addSheet('sheet_1');
|
|
if (gather) {
|
|
const sheet_2 = file.addSheet('sheet_2');
|
|
await gatherSheet({ sheet_2, ...gather })
|
|
}
|
|
|
|
// header
|
|
const headerStyle = new xlsx.Style();
|
|
headerStyle.align.h = 'center';
|
|
headerStyle.align.v = 'center';
|
|
headerStyle.border.right = 'thin';
|
|
headerStyle.border.rightColor = '#000000';
|
|
headerStyle.border.bottom = 'thin';
|
|
headerStyle.border.bottomColor = '#000000';
|
|
|
|
const headerRow = sheet_1.addRow();
|
|
const indexCell = headerRow.addCell();
|
|
indexCell.value = '序号'
|
|
indexCell.style = headerStyle
|
|
for (let h of header) {
|
|
const cell = headerRow.addCell();
|
|
cell.value = h.title;
|
|
cell.style = headerStyle
|
|
}
|
|
|
|
// data
|
|
const style = new xlsx.Style();
|
|
style.align.h = 'left';
|
|
style.align.v = 'center';
|
|
style.border.right = 'thin';
|
|
style.border.rightColor = '#000000';
|
|
style.border.bottom = 'thin';
|
|
style.border.bottomColor = '#000000';
|
|
for (let i = 0; i < data.length; i++) {
|
|
const row = sheet_1.addRow();
|
|
const indexCell = row.addCell();
|
|
indexCell.value = i + 1
|
|
indexCell.style = headerStyle
|
|
for (let h of header) {
|
|
const cell = row.addCell();
|
|
cell.value = data[i][h.key];
|
|
|
|
if (exp == 'patrol' || exp == 'maintenance') {
|
|
if (h.key == 'projectType') {
|
|
let type = data[i][h.key]
|
|
if (type == 'road') {
|
|
cell.value = '道路'
|
|
} else if (type == 'bridge') {
|
|
cell.value = '桥梁'
|
|
} else if (type == 'culvert') {
|
|
cell.value = '涵洞'
|
|
} else if (type == 'other') {
|
|
cell.value = '其他'
|
|
}
|
|
continue
|
|
}
|
|
|
|
if (h.key == 'reportType') {
|
|
let type = data[i][h.key]
|
|
if (type == 'patrol') {
|
|
cell.value = '巡查'
|
|
} else if (type == 'conserve') {
|
|
cell.value = '养护'
|
|
}
|
|
continue
|
|
}
|
|
}
|
|
|
|
cell.style = style
|
|
}
|
|
}
|
|
|
|
const savePath = path.join(fileDirPath, fileName)
|
|
await new Promise(function (resolve, reject) {
|
|
file.saveAs()
|
|
.pipe(fs.createWriteStream(savePath))
|
|
.on('finish', () => {
|
|
resolve()
|
|
});
|
|
})
|
|
return savePath
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
simpleExcelDown
|
|
}
|