自动化立体仓库 - WCS系统
#
taisheng
6 天以前 4453cdecbc6e7a925ae76e3223418654f1cf0b18
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.zy.common.web;
 
import com.alibaba.excel.EasyExcel;
import com.core.common.R;
import com.zy.common.utils.excel.EnInitData;
import com.zy.common.utils.excel.EnInitDataListener;
import com.zy.common.utils.excel.ExcelUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
 
/**
 * todo
 * Created by vincent on 2019-11-25
 */
@Controller
@RequestMapping("excel/")
public class FileController {
 
    /**
     * 文件上传
     */
    @PostMapping("upload")
    @ResponseBody
    public R upload(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), EnInitData.class, new EnInitDataListener()).sheet().doRead();
        return R.ok();
    }
 
    /**
     * 文件下载(失败了会返回一个有部分数据的Excel)
     */
    @GetMapping("download")
    public void download(HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("测试", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), EnInitData.class).sheet("模板").doWrite(ExcelUtils.data());
    }
 
}