首页>代码>springboot整合easyexcel实现excel文件上传解析,下载导出简单实例>/springboot-easyexcel/src/main/java/com/simon/springbooteasyexcel/controller/ExcelController.java
package com.simon.springbooteasyexcel.controller; import com.simon.springbooteasyexcel.common.EasyExcelUtils; import com.simon.springbooteasyexcel.model.ExportModel; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.BufferedInputStream; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; /** * @author Simon */ @RestController @RequestMapping("/importAndExport") public class ExcelController { @PostMapping("/imp") public Object importExcel(@RequestParam(name = "file", required = true) MultipartFile excl) { if (!excl.isEmpty()) { try { InputStream is = new BufferedInputStream(excl.getInputStream()); List<Object> list = EasyExcelUtils.readLessThan1000Row(is); for (Object obj : list) { System.out.println(obj); } } catch (Exception e) { e.printStackTrace(); } } return "上传解析成功!"; } @GetMapping("/exp") public void exportExcel(HttpServletRequest request, HttpServletResponse response) { String fileName; try { List<ExportModel> dataList = new ArrayList<>(); for (int i = 0; i <= 3; i++) { dataList.add(new ExportModel(i, "Mr.kk" + i, 18 + i)); } fileName = new String("excel导出".getBytes(), "UTF-8"); EasyExcelUtils.writeExcelOneSheet(response, dataList, null, fileName); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } }
最近下载更多