ReportTable.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import React, { useMemo } from 'react';
  2. import { Button, Table, Tooltip } from 'antd';
  3. import * as XLSX from 'xlsx';
  4. const ReportTable = ({ data, month }) => {
  5. // 将 data 对象解析为表格的 dataSource
  6. const dataSource = useMemo(() => {
  7. if (!data) return [];
  8. return data.filter(
  9. item => item.unsubmittedReports.length > 0 || item.lateSubmissions.length > 0
  10. );
  11. }, [data]);
  12. const exportToExcel = () => {
  13. const worksData1 = dataSource
  14. .filter(item => item.unsubmittedReports.length > 0 || item.lateSubmissions.length > 0)
  15. .map(item => ({
  16. 工号: item.userId,
  17. 姓名: item.name,
  18. 迟交次数: item.lateSubmissions.length,
  19. 漏交次数: item.unsubmittedReports.length,
  20. 请假次数: item.takingLeaveReports.length,
  21. }));
  22. const worksheet1 = XLSX.utils.json_to_sheet(worksData1);
  23. const worksData2 = dataSource.map(item => ({
  24. 工号: item.userId,
  25. 姓名: item.name,
  26. 迟交: item.lateSubmissions.join(','),
  27. 漏交: item.unsubmittedReports.join(','),
  28. 请假: item.takingLeaveReports.join(','),
  29. 离职时间: item.resignationDate,
  30. 入职时间: item.hiredDate,
  31. }));
  32. const worksheet2 = XLSX.utils.json_to_sheet(worksData2);
  33. const workbook = XLSX.utils.book_new();
  34. XLSX.utils.book_append_sheet(workbook, worksheet1, '总览');
  35. XLSX.utils.book_append_sheet(workbook, worksheet2, '详情');
  36. const excelBuffer = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });
  37. const data = new Blob([excelBuffer], {
  38. type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  39. });
  40. const url = URL.createObjectURL(data);
  41. const link = document.createElement('a');
  42. link.href = url;
  43. link.setAttribute('download', `${month}月考勤数据.xlsx`);
  44. document.body.appendChild(link);
  45. link.click();
  46. document.body.removeChild(link);
  47. };
  48. // 表格列配置
  49. const columns = [
  50. {
  51. title: '工号',
  52. dataIndex: 'userId',
  53. key: 'userId',
  54. },
  55. {
  56. title: '名称',
  57. dataIndex: 'name',
  58. key: 'name',
  59. },
  60. {
  61. title: '漏交',
  62. dataIndex: 'unsubmittedReports',
  63. key: 'unsubmittedReports',
  64. render: unsubmittedReports => (
  65. <Tooltip title={unsubmittedReports.join(',')}>
  66. <a>{unsubmittedReports.length}</a>
  67. </Tooltip>
  68. ),
  69. },
  70. {
  71. title: '迟交',
  72. dataIndex: 'lateSubmissions',
  73. key: 'lateSubmissions',
  74. render: lateSubmissions => (
  75. <Tooltip title={lateSubmissions.join(',')}>
  76. <a>{lateSubmissions.length}</a>
  77. </Tooltip>
  78. ),
  79. },
  80. {
  81. title: '请假',
  82. dataIndex: 'takingLeaveReports',
  83. key: 'takingLeaveReports',
  84. render: takingLeaveReports => (
  85. <Tooltip title={takingLeaveReports.join(',')}>
  86. <a>{takingLeaveReports.length}</a>
  87. </Tooltip>
  88. ),
  89. },
  90. {
  91. title: '离职日期',
  92. dataIndex: 'resignationDate',
  93. key: 'resignationDate',
  94. },
  95. {
  96. title: '入职日期',
  97. dataIndex: 'hiredDate',
  98. key: 'hiredDate',
  99. },
  100. {
  101. title: '操作',
  102. render: () => <a></a>,
  103. },
  104. ];
  105. return (
  106. <Table
  107. dataSource={dataSource}
  108. footer={() => (
  109. <Button onClick={exportToExcel} type="primary">
  110. 导出报表
  111. </Button>
  112. )}
  113. columns={columns}
  114. />
  115. );
  116. };
  117. export default ReportTable;