whycxzp
2022-10-08 d0ec87df722298a0b09edfddeef6a46184d64554
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
package com.whyc.service;
 
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.google.common.collect.Maps;
import com.whyc.dto.ResTestReportDTO;
import com.whyc.pojo.FileParam;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
 
@Service
public class ExcelExportService {
 
    @Autowired
    private FileParamService fileParamService;
 
    @Autowired
    private BattGroupDataService battGroupDataService;
 
    public void resTestReport(String fileId, HttpServletResponse response) {
        HashMap<String, Object> map = Maps.newHashMap();
        List list = new LinkedList<>();
        ResTestReportDTO dto = new ResTestReportDTO();
        dto.setP1("excel模板");
        list.add(dto);
        //基础测试数据
        //battGroupDataService.getListWithTestTime(fileId);
        //文件所属的电池组信息
        FileParam fileParam = fileParamService.getByFileId(fileId);
 
        map.put("dto", list);
        //获取导出模板地址
        ClassPathResource classPathResource = new ClassPathResource("excel_templates/rest_test_report_single_template.xls");
        String path = classPathResource.getPath();
        TemplateExportParams templateExportParams1 = new TemplateExportParams(path);
        Workbook wb = ExcelExportUtil.exportExcel(templateExportParams1, map);
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss"));
        String fileName = "内阻计测试"+time+".xls";
        try {
            response.setContentType("application/octet-stream;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
            response.flushBuffer();
            wb.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}