Java poi exports Excel and downloads it to the client. The specific content is as follows
Maven configuration, including dependencies for other file formats, is posted
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-excelant</artifactId> <version>3.12</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>3.12</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.8</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>3.8</version> </dependency>
Service layer
@Override public void export(Long sblsh, String excelName, OutputStream out) { try { // The first step is to create a webbook, corresponding to an Excel file HSSFWorkbook wb = new HSSFWorkbook(); // Generate a table HSSFSheet sheet = wb.createSheet(excelName); // The third step is to add the 0th row of the table header in the sheet HSSFRow row = sheet.createRow(0); // The fourth step is to create a cell and set the value table header to set the table header to center HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // Create a centered format HSSFCell cell = row.createCell(0); cell.setCellStyle(style); Byte kjzz = qyjbxxMapper.getKjzz(sblsh); List<A> record = this.selectBySblsh(sblsh); this.insertData(wb, sheet, row, record, out); } } catch (Exception e) { logger.info(e.getMessage()); } } /** * Import data into a table* @param wb execl file* @param sheet table* @param row Table row* @param record Data to be exported* @param out Output stream*/ private void insertData(HSSFWorkbook wb,HSSFSheet sheet,HSSFRow row,List<A> record, OutputStream out){ try { row = sheet.createRow(1); for(int i=0;i<title.length;i++){ row.createCell(i).setCellValue(title[i]); } for(int i=0;i<record.size();i++){ row = sheet.createRow(i+2); A data = record.get(i); row.createCell(0).setCellValue(data.getHc()); row.createCell(1).setCellValue(data.getXm()); BigDecimal je = data.getJe(); if(je!=null){ row.createCell(2).setCellValue(je.doubleValue()); } } //Merge cells, the first 2 digits represent the beginning and end row, and the last 2 digits represent the beginning and end column CellRangeAddress region = new CellRangeAddress(0,0,0,title.length-1); sheet.addMergedRegion(region); wb.write(out); out.flush(); out.close(); wb.close(); } catch (Exception e) { logger.info(e.getMessage()); } } Controller
@RequestMapping("/export") public void export(Long sblsh, HttpServletRequest request, HttpServletResponse response){ response.setContentType("octets/stream"); String excelName = "filename"; try { response.addHeader("Content-Disposition", "attachment;filename="+new String(excelName.getBytes("gb2312"), "ISO8859-1" )+".xls"); OutputStream out = response.getOutputStream(); aService.export(sblsh,excelName,out); } catch (Exception e) { e.printStackTrace(); } }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.