2021年8月31日 | Leave a comment 问题: 解决: package com.telsafe.crccdemo.rest; import org.apache.poi.ss.usermodel.CellType; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.view.document.AbstractXlsView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; @Controller @RequestMapping("export") public class ExportController { final UserService userService; public ExportController(final UserService userService) { this.userService = userService; } @PostMapping("users") ModelAndView users(@RequestParam(value = "path", required = false) String path) { final var list = .../// new ArrayList<UserVo> return new ModelAndView(new UserXlsView(), Map.of("list", list)); } static class UserXlsView extends AbstractXlsView { @Override protected void buildExcelDocument(final Map<String, Object> map, final org.apache.poi.ss.usermodel.Workbook workbook, final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse) throws Exception { Objects.requireNonNull(workbook); var sheet = workbook.createSheet(); final List<UserVo> users = (List<UserVo>) map.get("list"); for (int i = 0; i < users.size(); ++i) { final var user = users.get(i); final var row = sheet.createRow(i); var indexCell = row.createCell(0, CellType.NUMERIC); indexCell.setCellValue(user.getIndex()); row.createCell(1, CellType.STRING).setCellValue(user.getName()); row.createCell(2, CellType.STRING).setCellValue(user.getCompanyName()); row.createCell(3, CellType.STRING).setCellValue(user.getDepartmentName()); row.createCell(4, CellType.STRING).setCellValue(user.getPositionName()); } } } } 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 package com.telsafe.crccdemo.rest; import org.apache.poi.ss.usermodel.CellType;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.view.document.AbstractXlsView; import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.util.List;import java.util.Map;import java.util.Objects;import java.util.stream.Collectors; @Controller@RequestMapping("export")public class ExportController { final UserService userService; public ExportController(final UserService userService) { this.userService = userService; } @PostMapping("users") ModelAndView users(@RequestParam(value = "path", required = false) String path) { final var list = .../// new ArrayList<UserVo> return new ModelAndView(new UserXlsView(), Map.of("list", list)); } static class UserXlsView extends AbstractXlsView { @Override protected void buildExcelDocument(final Map<String, Object> map, final org.apache.poi.ss.usermodel.Workbook workbook, final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse) throws Exception { Objects.requireNonNull(workbook); var sheet = workbook.createSheet(); final List<UserVo> users = (List<UserVo>) map.get("list"); for (int i = 0; i < users.size(); ++i) { final var user = users.get(i); final var row = sheet.createRow(i); var indexCell = row.createCell(0, CellType.NUMERIC); indexCell.setCellValue(user.getIndex()); row.createCell(1, CellType.STRING).setCellValue(user.getName()); row.createCell(2, CellType.STRING).setCellValue(user.getCompanyName()); row.createCell(3, CellType.STRING).setCellValue(user.getDepartmentName()); row.createCell(4, CellType.STRING).setCellValue(user.getPositionName()); } } }} 参考: