# Excel导出说明

Author chenglu@expservice.com.cn

# 前端示例

导出
<template>
  <div>
    <ht-table ref="tableList" ref-table="tableList" :table-head="tableHead" :table-data="tableData">
      <ht-button @click="">导出</ht-button>
      <!-- 实际项目用到以下导出按钮
      <template slot="export">
        <ht-export-dialog :export="exportData" :find-export-fields="findExportFields" :save-export-fields="saveExportFields" />
      </template>
      -->
    </ht-table>
    
  </div>
</template>
<script>
// 根据实际项目修改 import 路径
// import {exportData, findExportFields, saveExportFields } from '@/api/oem/rms/statementMng/report';
export default {
  data() {
    return {
      // 实际项目用到以下内容
      // exportData: exportData,
      // findExportFields: findExportFields,
      // saveExportFields: saveExportFields,
       tableHead: [
        {
          label: '用户ID',
          prop: 'id',
          width: 'auto'
        },
        {
          label: '姓名',
          prop: 'name',
          width: 'auto'
        },
        {
          label: '电话号',
          prop: 'phone',
          width: 'auto'
        },
        {
          label: '地址',
          prop: 'address',
          width: 'auto'
        }
      ],
      tableData:{}
    }
  },
  mounted() {
    /**
     * @todo: 模拟查询数据
     * @author: chenglu@expservice.com.cn
     * @Date: 2023-05-26 09:36:23
     */    
    this.handleSearch();
  },
  methods:{
    /**
     * @todo: 模拟查询数据 
     * @author: chenglu@expservice.com.cn
     * @Date: 2023-05-26 09:36:02
     */    
    async handleSearch(event) {
      try {
        this.tableData = { 
          'records':
         [
           { 'id': '1001', 'name': 'A1', 'phone': '10086', 'address': '长春市', 'rownums': 1 },
           { 'id': '1002', 'name': 'A2', 'phone': '10010', 'address': '北京市', 'rownums': 2 }
         ] };
      } catch (e) {
        this.$notify.message(e, 'error');
      }
    }
  }
};
</script>

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
Expand Copy

# 后端示例

  • Controller
    /**
     * @remark:导出数据
     * @param: response
     * @param: param
     * @return: void
     * @author: CL
     * @date: 2023/5/26 9:16
     * @version: 1.0.1     * Modification History:
     * Date       Author       Version          Description
     * -----------------------------------------------------------
     * 2023/5/26  CL           v1.0.1            init
     */
    @ApiOperation(value = "导出->数据", notes = "导出数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "response", value = "response", dataTypeClass = HttpServletResponse.class),
            @ApiImplicitParam(name = "params", value = "params", dataTypeClass = String.class)
    })
    @PostMapping(value = "/exportData")
    public void exportData(HttpServletResponse response, @RequestBody Map<String, Object> param) throws IOException {
        // 判断参数是否为空
        AssertMyUtil.notNull(param, BizCode.VARIABLE_NOT_EMPTY, "param");
        // 调用导出数据方法
        csSeBaVehicleService.exportData(response, "导出Demo数据", param);
    }

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
  • Service
    /**
     * @remark:导出数据
     * @param: response
     * @param: fileName
     * @param: params
     * @return: void
     * @author: CL
     * @date: 2023/5/26 9:13
     * @version: 1.0.1
     * Modification History:
     * Date       Author       Version          Description
     * -----------------------------------------------------------
     * 2023/5/26  CL           v1.0.1            init
     */
    void exportData(HttpServletResponse response, String fileName, Map<String, Object> params) throws IOException;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  • Impl
    /**
     * @remark:导出数据
     * @param: response
     * @param: fileName
     * @param: params
     * @return: void
     * @author: CL
     * @date: 2023/5/26 9:13
     * @version: 1.0.1
     * Modification History:
     * Date       Author       Version          Description
     * -----------------------------------------------------------
     * 2023/5/26  CL           v1.0.1            init
     */
    @Override
    public void exportData(HttpServletResponse response, String fileName, Map<String, Object> params) throws IOException {
        // 导出列表表头信息
        String header = MyUtil.getValue(params.get("header"));
        // 查询条件
        String param = MyUtil.getValue(params.get("params"));
        String qc = MyUtil.getConditionsWhere(param, null);
        // 导出excel表头
        List<List<String>> headList = new ArrayList<>();
        // 导出字段信息
        List<String> headFields = excelApiService.toHeader(header, headList);
        //查询字段(导出时勾选的字段)
        String fields = ArrayMyUtil.join(headFields.toArray(), ",");
        //导出数据
        List<Map<String, Object>> dataList = csSeBaVehicleMapper.exportData(fields, qc);
        //导出方法(response:,filelame: 文件名,headFields: 导出字信息,headList:表头名,datalist: 导出数据,getTransDic(): 字典翻译)
        excelApiService.export(response, fileName, headFields, headList, dataList, getTransDic());
    }

    /** 
     * @remark:字典翻译
     * @param : 
     * @return: java.util.HashMap<java.lang.String,java.lang.String>
     * @author: yangmeng
     * @date: 2023/6/6 10:09 
     * @version: 1.0.1
     * Modification History: 
     * Date       Author          Version            Description 
     * ----------------------------------------------------------
     * 2023/6/6     yangmeng        v1.0.1             init
     */
    public getTransDic<String, String> transHashMap(){
        HashMap<String, String> transHash = new HashMap<>();
        // 设置省
        transHash.put("province", "division");
        // 设置市
        transHash.put("city", "division");
        // 设置区县
        transHash.put("county", "division");
        // 设置状态
        transHash.put("status", "dic|" + DicConstant.YXZT);
        // 设置时间
        transHash.put("createTime", "date|" + FormatConstant.SIMPLE_DATE_TIME_FORMAT);
        // 设置时间
        transHash.put("lastUpdateTime", "date|" + FormatConstant.SIMPLE_DATE_TIME_FORMAT);
        return transHash;
    }

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
  • Mapper
    /**
     * @remark:导出数据
     * @param: fields
     * @param: qc
     * @return: java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     * @author: CL
     * @date: 2023/5/26 9:04
     * @version: 1.0.1
     * Modification History:
     * Date       Author       Version          Description
     * -----------------------------------------------------------
     * 2023/5/26  CL           v1.0.1            init
     */
    @SelectProvider(VehicleSqlProvider.class)
    List<Map<String, Object>> exportData(String fields, String qc);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  • Provide
    /**
     * @remark:导出数据
     * @param: fields
     * @param: qc
     * @return: java.lang.String
     * @author: CL
     * @date: 2023/5/26 9:03
     * @version: 1.0.1
     * Modification History:
     * Date       Author       Version          Description
     * -----------------------------------------------------------
     * 2023/5/26  CL           v1.0.1            init
     */
    public static String exportData(String fields, String qc) {
        StringBuffer sql = new StringBuffer();
        sql.append(" select \n ");
        sql.append(fields);
        sql.append(" from \n ");
        sql.append("  cs_se_ba_vehicle a \n ");
        sql.append(" where ");
        sql.append(qc);
        return sql.toString();
    }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

# 版本

  • v1.0.1