最近温故了下Servlet知识,以下做一些导出Excel,返回Json的演示
以下的这些返回原理其实都是在控制返回的的ContentType,告诉客户端我返回的内容,返回xml等其他思路大同小异
返回Json
导出Excel示例,此处使用Poi Excel组件
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { List<TempUser> list = new ArrayList<>(); list.add(new TempUser(){{CarNo="D1";SMTime="20230201-m";Name="taomg";}}); list.add(new TempUser(){{CarNo="D2";SMTime="20230202-m";Name="TOM";}}); list.add(new TempUser(){{CarNo="D3";SMTime="20230203-m";Name="jack";}}); String filepath="F:\\_OResp\\HrMobile\\ManGLCommon\\HrMHomeQWAdmin\\Template\\banche.xlsx"; Workbook workbook; FileInputStream fileInputStream = new FileInputStream(filepath); workbook = new XSSFWorkbook(fileInputStream);//2007,2003 HSSFWorkbook Sheet sheet =workbook.getSheetAt(0); Row row ; Cell cell; for(int i=0;i<list.size();i++){ row =sheet.createRow(i+1); cell =row.createCell(0); cell.setCellValue(String.valueOf(i)); cell =row.createCell(1); cell.setCellValue(list.get(i).CarNo); cell =row.createCell(2); cell.setCellValue(list.get(i).SMTime); cell =row.createCell(3); cell.setCellValue(list.get(i).Name); } response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Encoding","utf-8"); response.setHeader("Content-Disposition","attachment; filename="+ URLEncoder.encode("测试excel2023.xlsx", StandardCharsets.UTF_8)); ServletOutputStream servletOutputStream = response.getOutputStream(); workbook.write(servletOutputStream); }
标签:返回,list,Excel,Json,SMTime,new,Servlet From: https://www.cnblogs.com/life512/p/17095289.html