xyc
2026-08-14 1f4a6cc6045f47861c3e8d49a782afc3e11e3843
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
package com.trafficaudit.reportexport.controller;
 
import com.trafficaudit.common.Result;
import com.trafficaudit.reportexport.service.ReportExportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import javax.annotation.Resource;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
 
@Slf4j
@RestController
@RequestMapping("/api/report")
public class ReportExportController {
 
    @Resource
    private ReportExportService reportService;
 
    /** 生成_货运量分市州明细.xlsx */
    @GetMapping("/export/cityDetail")
    public ResponseEntity<byte[]> exportCityDetail(@RequestParam("period") String period) throws Exception {
        return buildResponse(reportService.exportCityDetail(period), "生成_货运量分市州明细.xlsx");
    }
 
    /** 生成_货运量排名.xlsx */
    @GetMapping("/export/freightRank")
    public ResponseEntity<byte[]> exportFreightRank(@RequestParam("period") String period) throws Exception {
        return buildResponse(reportService.exportFreightRank(period), "生成_货运量排名.xlsx");
    }
 
    /** 生成_周转量排名.xlsx */
    @GetMapping("/export/turnoverRank")
    public ResponseEntity<byte[]> exportTurnoverRank(@RequestParam("period") String period) throws Exception {
        return buildResponse(reportService.exportTurnoverRank(period), "生成_周转量排名.xlsx");
    }
 
    private ResponseEntity<byte[]> buildResponse(byte[] data, String fileName) throws Exception {
        String encoded = URLEncoder.encode(fileName, "UTF-8").replace("+", "%20");
        return ResponseEntity.ok()
            .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename*=UTF-8''" + encoded)
            .contentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
            .body(data);
    }
}