king
2025-01-24 e1cee96b38805bcccf48e7bcb9d296f2bc54c720
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
import React, { Component } from 'react'
import { notification } from 'antd'
import * as XLSX from 'sheetjs-style'
import { DownloadOutlined } from '@ant-design/icons'
import moment from 'moment'
 
// import './index.scss'
 
class ExcelOutColumns extends Component {
  /**
   * @description 导出字段集
   */
  actionTrigger = () => {
    const { data, setting } = this.props
 
    if (data.length === 0) {
      notification.warning({
        top: 92,
        message: '字段集不可为空!',
        duration: 5
      })
      return
    }
 
    let columns = [{
      Column: 'tb',
      Text: '表名'
    }, {
      Column: 'label',
      Text: '名称'
    }, {
      Column: 'field',
      Text: '字段'
    }, {
      Column: 'datatype',
      Text: '数据类型'
    }]
    
    let table = []
    let _header = []
    let _topRow = {}
    let colwidth = []
    let tbName = setting.tableName || ''
 
    if (window.GLOB.externalDatabase !== null) {
      tbName = tbName.replace(/@db@/ig, window.GLOB.externalDatabase)
    }
 
    columns.forEach(col => {
      _header.push(col.Column)
      _topRow[col.Column] = col.Text
      colwidth.push({wch: 25})
    })
 
    table.push(_topRow)
 
    data.forEach((item) => {
      let _row = {}
 
      columns.forEach((col) => {
        if (col.Column === 'tb') {
          _row[col.Column] = tbName
        } else {
          _row[col.Column] = item[col.Column]
        }
      })
 
      table.push(_row)
    })
 
    const ws = XLSX.utils.json_to_sheet(table, {header: _header, skipHeader: true})
 
    ws['!cols'] = colwidth
    ws['!rows'] = Array(table.length).fill({hpx: 18})
 
    const wb = XLSX.utils.book_new()
    XLSX.utils.book_append_sheet(wb, ws, 'Sheet1')
 
    XLSX.writeFile(wb, `${tbName}${moment().format('YYYYMMDDHHmmss')}.xlsx`)
  }
 
  render() {
    return (<DownloadOutlined className="columns-out" title="下载" onClick={this.actionTrigger}/>)
  }
}
 
export default ExcelOutColumns