123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- 'use strict';
- const Excel = require('exceljs');
- const utils = {
- // 定义格式: 工作表
- // [ {sheetName:''} , {} ]
- // 获取excel
- getExcel(config) {
- if (!config) {
- return;
- }
- const workbook = new Excel.Workbook();
- config.forEach((con, index) => {
- const ws1 = workbook.addWorksheet(con.sheetName || 'sheet' + index, con.sheetOptions);
- con.sheetHeader.forEach(header => {
- const row = ws1.addRow([ header.headerName ]);
- Object.keys(header.headerConfig).forEach(key => {
- row[key] = header.headerConfig[key];
- });
- });
- ws1.mergeCells(con.sheetKey[0].letter + '1:'
- + con.sheetKey[con.sheetKey.length - 1].letter + '1');
- ws1.addRow(con.sheetKey.map(item => item.label));
- con.sheetData.forEach((data, index) => {
- const rowData = con.sheetKey.map((item, i) => {
- ws1.getColumn(i + 1).width = item.width;
- if (item.key == 'num') {
- return index + 1;
- }
- // console.log('====================', item)
- // if (item.key == 'birthday') {
- // const myDate = new Date();
- // const year = myDate.getFullYear();
- // const month = (myDate.getMonth() + 1) > 9 ? (myDate.getMonth() + 1) : '0' + (myDate.getMonth() + 1);
- // const day = myDate.getDate() > 9 ? (myDate.getDate()) : '0' + (myDate.getDate());
- // return item.value = ;
- // }
- return this.getValue(data, item) || '';
- });
- ws1.addRow(rowData);
- });
- const tableKeyHeader = 1;
- const tableFirst = 1;
- this.rowCenter(ws1, tableFirst,
- con.sheetData.length + con.sheetHeader.length + tableKeyHeader);
- });
- return workbook;
- },
- rowCenter(arg_ws, arg_start, arg_end) {
- for (let i = arg_start; i <= arg_end; i++) {
- arg_ws.findRow(i).alignment = { vertical: 'middle', horizontal: 'center' };
- arg_ws.findRow(i).eachCell(function(cell, index) {
- if (i == 1) {
- cell.font = { bold: true };
- } else {
- cell.font = { bold: false };
- }
- cell.border = {
- top: { style: 'thin' },
- left: { style: 'thin' },
- bottom: { style: 'thin' },
- right: { style: 'thin' },
- };
- });
- }
- },
- getValue(data, config) {
- if (config.key.indexOf('.') != -1) { // 存在子属性
- const props = config.key.split('.');
- return props.reduce((first, second) => {
- if (first) {
- return first[second];
- }
- return null;
- }, data);
- } // 不存在
- return data[config.key];
- },
- formatName(name) {
- if (name === null || name === undefined) {
- return ' ';
- }
- if (name.length < 2 || name.length >= 10) {
- return ' ';
- } else if (name.length === 2) {
- return name[0] + '*';
- }
- // 如果名字长度大于2位,保留首尾汉字,中间用星号替换
- const firstChar = name[0]; // 获取首位汉字
- const lastChar = name[name.length - 1]; // 获取末位汉字
- const middleStars = '*'.repeat(name.length - 2); // 生成中间的星号字符串
- return firstChar + middleStars + lastChar;
- },
- formatPhone(phone) {
- if (phone === null || phone === undefined) {
- return ' ';
- }
- if (phone.length < 8 || phone.length >= 30) {
- // 长度小于2位,返回空格
- return ' ';
- } else if (phone.length === 8) {
- const firstChar = phone[0]; // 获取首位
- const secondChar = phone[1];
- const thirdChar = phone[2];
- const lastChar = phone[phone.length - 1];
- const last2Char = phone[phone.length - 2];
- const middleStars = '*'.repeat(phone.length - 5); // 生成中间的星号字符串
- return firstChar + secondChar + thirdChar + middleStars + last2Char + lastChar;
- }
- const firstChar = phone[0]; // 获取首位
- const secondChar = phone[1];
- const thirdChar = phone[2];
- const lastChar = phone[phone.length - 1]; // 获取末位
- const last2Char = phone[phone.length - 2];
- const last3Char = phone[phone.length - 3];
- const last4Char = phone[phone.length - 4];
- const middleStars = '*'.repeat(phone.length - 7); // 生成中间的星号字符串
- return firstChar + secondChar + thirdChar + middleStars + last4Char + last3Char + last2Char + lastChar;
- },
- formatIdCard(idCard) {
- if (idCard === null || idCard === undefined) {
- return ' ';
- }
- if (idCard.length === 15 || idCard.length === 18) {
- const firstChar = idCard[0]; // 获取首位
- const secondChar = idCard[1]; // 获取第二位
- const thirdChar = idCard[2]; // 获取第三位
- const first4Char = idCard[3]; // 获取第三位
- const first5Char = idCard[4]; // 获取第三位
- const first6Char = idCard[5]; // 获取第三位
- const lastChar = idCard[idCard.length - 1]; // 获取末位
- const last2Char = idCard[idCard.length - 2];
- const middleStars = '*'.repeat(idCard.length - 8); // 生成中间的星号字符串
- return firstChar + secondChar + thirdChar + first4Char + first5Char + first6Char + middleStars + last2Char + lastChar;
- }
- return ' ';
- },
- };
- module.exports = utils;
|