Browse Source

抽查导出50%

dev
wenlele 1 year ago
parent
commit
775ca75540
  1. 9
      api/app/lib/controllers/report/index.js
  2. 112
      api/utils/xlsxDownload.js

9
api/app/lib/controllers/report/index.js

@ -3,6 +3,8 @@ const { QueryTypes } = require('sequelize');
const moment = require('moment');
const xlsxDownload = require('../../../../utils/xlsxDownload.js');
const fs = require('fs');
const XLSX = require('xlsx')
const path = require('path')
async function reportList (ctx) {
try {
const models = ctx.fs.dc.models;
@ -1159,7 +1161,7 @@ async function exportSpotRode (ctx) {
}
let exportData = listRes.map(({ dataValues: item }) => {
let road = item.road.dataValues
let road = item.road && item.road.dataValues || {}
return {
level:
judgeLevel(road.routeCode)
@ -1176,14 +1178,15 @@ async function exportSpotRode (ctx) {
const fileName = `${moment(previewRes.date).format('YYYY年MM月DD日HH时mm分')}道路抽查记录` + '.csv'
const filePath = await xlsxDownload.simpleExcelDown({
data: exportData, header, fileName: fileName
data: exportData, header, fileName: fileName, gather:{data: exportData, header}
})
const fileData = fs.readFileSync(filePath);
ctx.status = 200;
ctx.set('Content-Type', 'application/x-xls');
ctx.set('Content-disposition', 'attachment; filename=' + encodeURI(fileName));
ctx.body = fileData;
ctx.body = fileData
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;

112
api/utils/xlsxDownload.js

@ -16,11 +16,119 @@ async function makeDir (dir) {
}
}
async function simpleExcelDown ({ data = [], header = [], fileName = moment().format('YYYY-MM-DD HH:mm:ss'), exp } = {}) {
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 = 12
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: '',
title: '',
},{
key: 'county',
title: '总里程',
}, {
key: 'countyParticipate',
title: '纳入考核里程',
}, {
key: 'countyPresent',
title: '本次考核里程',
}, {
key: 'county',
title: '实际抽取比原计划多',
}, {
key: 'township',
title: '总里程',
}, {
key: 'townshipParticipate',
title: '纳入考核里程',
}, {
key: 'townshipPresent',
title: '本次考核里程',
}, {
key: 'township',
title: '实际抽取比原计划多',
}, {
key: 'village',
title: '总里程',
}, {
key: 'villageParticipate',
title: '纳入考核里程',
}, {
key: 'villagePresent',
title: '本次考核里程',
}, {
key: 'village',
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';
// for (let i = 0; i < data.length; i++) {
// const row = sheet_2.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];
// 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();
@ -99,6 +207,8 @@ async function simpleExcelDown ({ data = [], header = [], fileName = moment().fo
return savePath
}
module.exports = {
simpleExcelDown
}
Loading…
Cancel
Save