react-admin-kit
Version:
A react based UI components for admin system
155 lines (147 loc) • 5.19 kB
JavaScript
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
import * as ExcelJs from 'exceljs';
import { saveAs } from 'file-saver';
import moment from 'moment';
// 默认的列宽
export var DEFAULT_COLUMN_WIDTH = 20;
function formatDateTypeData(text, format) {
if (!text) return '';
if (typeof text === 'string') {
return text;
} else {
return moment(text).format(format);
}
}
function getTextByOptions(text, col) {
if (col.valueEnum) {
var _col$valueEnum$text;
return (_col$valueEnum$text = col.valueEnum[text]) === null || _col$valueEnum$text === void 0 ? void 0 : _col$valueEnum$text.text;
}
if (col.fieldProps) {
if (typeof col.fieldProps === 'function') {
var _col$fieldProps, _options$find;
// @ts-ignore
var options = ((_col$fieldProps = col.fieldProps({}, {
current: {}
}, col)) === null || _col$fieldProps === void 0 ? void 0 : _col$fieldProps.options) || [];
// eslint-disable-next-line eqeqeq
return (_options$find = options.find(function (option) {
return option.value == text;
})) === null || _options$find === void 0 ? void 0 : _options$find.label;
} else {
var _col$fieldProps2, _options$find2;
// @ts-ignore
var _options = ((_col$fieldProps2 = col.fieldProps) === null || _col$fieldProps2 === void 0 ? void 0 : _col$fieldProps2.options) || [];
// eslint-disable-next-line eqeqeq
return (_options$find2 = _options.find(function (option) {
return option.value == text;
})) === null || _options$find2 === void 0 ? void 0 : _options$find2.label;
}
}
}
/**
* 获取导出的值
*/
export function getExportValue(record, col) {
var index = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
var text = typeof col.dataIndex === 'string' ? record[col.dataIndex] : ''; // dataIndex 如果是 react node 就不导出
if (col.renderExport) {
return col.renderExport(text, record);
}
if (col.render) {
// @ts-ignore
return col.render(text, record, index, undefined, undefined);
}
if (col.renderText) {
// @ts-ignore
return col.renderText(text, record, index);
}
if (['select', 'radio', 'radioButton', 'checkbox'].includes(col.valueType || '')) {
return getTextByOptions(text, col);
}
if (col.valueType === 'date') {
return formatDateTypeData(text, 'YYYY-MM-DD');
}
if (col.valueType === 'dateTime') {
return formatDateTypeData(text, 'YYYY-MM-DD HH:mm:ss');
}
if (col.valueType === 'dateRange') {
if (text) {
return text.map(function (itemDate) {
return formatDateTypeData(itemDate, 'YYYY-MM-DD');
}).join(' - ');
}
}
if (col.valueType === 'dateTimeRange') {
if (text) {
return text.map(function (itemDate) {
return formatDateTypeData(itemDate, 'YYYY-MM-DD HH:mm:ss');
}).join(' - ');
}
}
if (col.valueType === 'money') {
if (!text) return '';
return text;
}
if (!!text && _typeof(text) === 'object') {
return text.value;
}
return text;
}
export function saveWorkbook(workbook, fileName) {
// 导出文件
workbook.xlsx.writeBuffer().then(function (data) {
var blob = new Blob([data], {
type: ''
});
saveAs(blob, fileName);
});
}
export var exportAntTableToExcel = function exportAntTableToExcel(columns, dataSource) {
var fileName = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '默认导出';
// 创建工作簿
var workbook = new ExcelJs.Workbook();
// 添加sheet
var worksheet = workbook.addWorksheet('sheet1');
// 设置 sheet 的默认行高
worksheet.properties.defaultRowHeight = 20;
// 设置列
worksheet.columns = columns.map(function (col) {
return {
header: typeof col.title === 'string' ? col.title : '',
key: typeof col.dataIndex === 'string' ? col.dataIndex : Date.now().toString(),
width: col.width ? Number(col.width) / 5 : DEFAULT_COLUMN_WIDTH
};
});
// 设置垂直的对齐方式
worksheet.columns.forEach(function (col) {
col.alignment = {
vertical: 'middle'
};
});
console.log('导出', dataSource);
// 处理行
var rowsData = (dataSource || []).map(function (record, index) {
return columns.map(function (col) {
return getExportValue(record, col, index);
});
});
// 添加行
worksheet.addRows(rowsData);
// 处理合并问题
(dataSource || []).forEach(function (record, recordIndex) {
columns.forEach(function (col, colIdx) {
//@ts-ignore
var _ref = col.onCell ? col.onCell(record) || {} : {},
rowSpan = _ref.rowSpan;
if (rowSpan > 1) {
// 向下合并
var rowIndex = recordIndex + 2;
var colIndex = colIdx + 1;
worksheet.mergeCells(rowIndex, colIndex, rowIndex + rowSpan - 1, colIndex);
}
});
});
// 导出excel
saveWorkbook(workbook, fileName + '.xlsx');
};