poi导出Excel
查询使用的是mybaits-plus
1.添加jar包
poi
poi-ooxml
poi-ooxml-schemas
poi-scratchpad
poi-contrib
2.前台传递过来的是一个String类型的字符串是勾选的id
3.Controller层的代码
private static final String XLSX = "xlsx";
@RequestMapping(value = "/export", method = RequestMethod.GET) public String export(HttpServletResponse response, String feedBackIds) { String[] ids = feedBackIds.split(","); try { response.reset(); response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); response.setHeader("Content-Type", "application/force-download"); response.setHeader("Content-Tpye", "application/vnd.ms-excel"); String fileName = "xxx表."+ XLSX; response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); ServletOutputStream outputStream = response.getOutputStream(); feedBackQueryService.export(outputStream, ids); return "成功"; } catch (IOException e) { e.printStackTrace(); return "失败"; } }
4.service层代码
public void export(ServletOutputStream outputStream, String[] feedBackIds) { try { Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet(); CellStyle style1 = workbook.createCellStyle(); // 设置为文本格式,防止变成科学计数法 DataFormat format = workbook.createDataFormat(); style1.setDataFormat(format.getFormat("@")); Row row = sheet.createRow(0); row.createCell(0).setCellValue("a"); row.createCell(1).setCellValue("b"); row.createCell(2).setCellValue("c"); row.createCell(3).setCellValue("d"); row.createCell(4).setCellValue("e"); row.createCell(5).setCellValue("f"); row.createCell(6).setCellValue("g"); row.createCell(7).setCellValue("h"); row.createCell(8).setCellValue("i"); row.createCell(9).setCellValue("j"); row.createCell(10).setCellValue("k"); row.createCell(11).setCellValue("l"); List<YjxCjFkInfoDO> yjxCjFkInfoDOS = baseMapper.selectList(new QueryWrapper<YjxCjFkInfoDO>().in("id", feedBackIds)); List<YjxCjFkInfoVO> yjxCjFkInfoVOS = convertVO(yjxCjFkInfoDOS); int j = 0; for (YjxCjFkInfoVO yjxCjFkInfoVO : yjxCjFkInfoVOS) { j++; Row salaryRow = sheet.createRow(j); translateSalary(salaryRow, yjxCjFkInfoVO); } workbook.write(outputStream); outputStream.flush(); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } } private boolean translateSalary(Row createRow, YjxCjFkInfoVO yjxCjFkInfoVO) { if (yjxCjFkInfoVO == null) { return true; } createRow.createCell(0).setCellValue(yjxCjFkInfoVO.getId()); createRow.createCell(1).setCellValue(yjxCjFkInfoVO.getCompanyname()); createRow.createCell(2).setCellValue(yjxCjFkInfoVO.getCompanycode()); createRow.createCell(3).setCellValue(yjxCjFkInfoVO.getUsercode()); createRow.createCell(4).setCellValue(yjxCjFkInfoVO.getUsername()); createRow.createCell(5).setCellValue(yjxCjFkInfoVO.getFeedbacklatitudetext()); createRow.createCell(6).setCellValue(yjxCjFkInfoVO.getRiskcase()); createRow.createCell(7).setCellValue(yjxCjFkInfoVo.getRiskcase()); createRow.createCell(8).setCellValue(nextOption(yjxCjFkInfoVO.getNextoperation())); createRow.createCell(9).setCellValue(yjxCjFkInfoVO.getFeedbackcontent()); createRow.createCell(10).setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(yjxCjFkInfoVO.getFeedbacktime())); createRow.createCell(11).setCellValue(yjxCjFkInfoVO.getPagesearchcode()); return false; }
或参考:https://blog.csdn.net/liberty12345678/article/details/82109553
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构