1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
| import React, { Component } from "react";
| import {
| Table,
| Tag,
| Form,
| Icon,
| Button,
| Input,
| Radio,
| Select,
| message,
| Collapse,
| } from "antd";
|
| import { excelList } from "@/api/excel";
| const { Panel } = Collapse;
| const columns = [
| {
| title: "Id",
| dataIndex: "id",
| key: "id",
| width: 200,
| align: "center",
| },
| {
| title: "Title",
| dataIndex: "title",
| key: "title",
| width: 200,
| align: "center",
| },
| {
| title: "Author",
| key: "author",
| dataIndex: "author",
| width: 100,
| align: "center",
| render: (author) => <Tag key={author}>{author}</Tag>,
| },
| {
| title: "Readings",
| dataIndex: "readings",
| key: "readings",
| width: 195,
| align: "center",
| },
| {
| title: "Date",
| dataIndex: "date",
| key: "date",
| width: 195,
| align: "center",
| },
| ];
| class Excel extends Component {
| _isMounted = false; // 这个变量是用来标志当前组件是否挂载
| state = {
| list: [],
| filename: "excel-file",
| autoWidth: true,
| bookType: "xlsx",
| downloadLoading: false,
| selectedRows: [],
| selectedRowKeys: [],
| };
| fetchData = () => {
| excelList().then((response) => {
| const list = response.data.data.items;
| if (this._isMounted) {
| this.setState({ list });
| }
| });
| };
| componentDidMount() {
| this._isMounted = true;
| this.fetchData();
| }
| componentWillUnmount() {
| this._isMounted = false;
| }
| onSelectChange = (selectedRowKeys, selectedRows) => {
| this.setState({ selectedRows, selectedRowKeys });
| };
| handleDownload = (type) => {
| if (type === "selected" && this.state.selectedRowKeys.length === 0) {
| message.error("至少选择一项进行导出");
| return;
| }
| this.setState({
| downloadLoading: true,
| });
| import("@/lib/Export2Excel").then((excel) => {
| const tHeader = ["Id", "Title", "Author", "Readings", "Date"];
| const filterVal = ["id", "title", "author", "readings", "date"];
| const list = type === "all" ? this.state.list : this.state.selectedRows;
| const data = this.formatJson(filterVal, list);
| excel.export_json_to_excel({
| header: tHeader,
| data,
| filename: this.state.filename,
| autoWidth: this.state.autoWidth,
| bookType: this.state.bookType,
| });
| this.setState({
| selectedRowKeys: [], // 导出完成后将多选框清空
| downloadLoading: false,
| });
| });
| };
| formatJson(filterVal, jsonData) {
| return jsonData.map(v => filterVal.map(j => v[j]))
| }
| filenameChange = (e) => {
| this.setState({
| filename: e.target.value,
| });
| };
| autoWidthChange = (e) => {
| this.setState({
| autoWidth: e.target.value,
| });
| };
| bookTypeChange = (value) => {
| this.setState({
| bookType: value,
| });
| };
| render() {
| const { selectedRowKeys } = this.state;
| const rowSelection = {
| selectedRowKeys,
| onChange: this.onSelectChange,
| };
| return (
| <div className="app-container">
| <Collapse defaultActiveKey={["1"]}>
| <Panel header="导出选项" key="1">
| <Form layout="inline">
| <Form.Item label="文件名:">
| <Input
| style={{ width: "250px" }}
| prefix={
| <Icon type="file" style={{ color: "rgba(0,0,0,.25)" }} />
| }
| placeholder="请输入文件名(默认excel-file)"
| onChange={this.filenameChange}
| />
| </Form.Item>
| <Form.Item label="单元格宽度是否自适应:">
| <Radio.Group
| onChange={this.autoWidthChange}
| value={this.state.autoWidth}
| >
| <Radio value={true}>是</Radio>
| <Radio value={false}>否</Radio>
| </Radio.Group>
| </Form.Item>
| <Form.Item label="文件类型:">
| <Select
| defaultValue="xlsx"
| style={{ width: 120 }}
| onChange={this.bookTypeChange}
| >
| <Select.Option value="xlsx">xlsx</Select.Option>
| <Select.Option value="csv">csv</Select.Option>
| <Select.Option value="txt">txt</Select.Option>
| </Select>
| </Form.Item>
| <Form.Item>
| <Button
| type="primary"
| icon="file-excel"
| onClick={this.handleDownload.bind(null, "all")}
| >
| 全部导出
| </Button>
| </Form.Item>
| <Form.Item>
| <Button
| type="primary"
| icon="file-excel"
| onClick={this.handleDownload.bind(null, "selected")}
| >
| 导出已选择项
| </Button>
| </Form.Item>
| </Form>
| </Panel>
| </Collapse>
| <br />
| <Table
| bordered
| columns={columns}
| rowKey={(record) => record.id}
| dataSource={this.state.list}
| pagination={false}
| rowSelection={rowSelection}
| loading={this.state.downloadLoading}
| />
| </div>
| );
| }
| }
|
| export default Excel;
|
|