java导出数据到excel

1.导入maven
!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
<scope>compile</scope>
</dependency>
2.导出excel方法
public void exportExcel(HttpServletResponse response, Searchable search){ //查出检验项目 Master master = masterService.findOne(search); //查出检验项目的结果集合 List<Result> list = findListWithSort(search); //创建HSSFWorkbook对象(excel的文档对象) HSSFWorkbook wb = new HSSFWorkbook(); //建立新的sheet对象(excel的表单) HSSFSheet sheet = wb.createSheet("检验结果表"); //在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个 HSSFRow row1 = sheet.createRow(0); //创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个 HSSFCell cell = row1.createCell(0); //设置单元格内容 cell.setCellValue(master.getItemName() + "检验报告单"); //合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列 sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 5)); //在sheet里创建第二行 HSSFRow row2 = sheet.createRow(1); /*HSSFCell cell1 = row2.createCell(0); HSSFCell cell2 = row2.createCell(1); HSSFCell cell3 = row2.createCell(2); HSSFCell cell4 = row2.createCell(3); HSSFCell cell5 = row2.createCell(4); HSSFCell cell6 = row2.createCell(5); //设置列宽 sheet.setColumnWidth( cell1.getColumnIndex(),256 * 25); sheet.setColumnWidth( cell2.getColumnIndex(),256 * 20); sheet.setColumnWidth( cell3.getColumnIndex(),256 * 20); sheet.setColumnWidth( cell4.getColumnIndex(),256 * 20); sheet.setColumnWidth( cell5.getColumnIndex(),256 * 20); sheet.setColumnWidth( cell6.getColumnIndex(),256 * 20);*/ //设置缺省列宽 sheet.setDefaultColumnWidth(20); //设置缺省列高 sheet.setDefaultRowHeightInPoints(30); //创建单元格并设置单元格内容 row2.createCell(0).setCellValue("报告日期"); HSSFCellStyle style = wb.createCellStyle(); // 实例化样式对象 row2.createCell(1).setCellValue("报告项目名称"); // 垂直居中 style.setAlignment(HorizontalAlignment.CENTER); //将样式应用到行 cell.setCellStyle(style); row2.createCell(2).setCellValue("结果"); row2.createCell(3).setCellValue("单位"); row2.createCell(4).setCellValue("异常"); row2.createCell(5).setCellValue("正常参考值"); for (int i = 0; i < list.size(); i++) { HSSFRow rows = sheet.createRow(i + 2); rows.createCell(0).setCellValue(DateUtil.formatDateTime(list.get(i).getResultDateTime())); rows.createCell(1).setCellValue(list.get(i).getReportItemName()); rows.createCell(2).setCellValue(list.get(i).getResult()); rows.createCell(3).setCellValue(list.get(i).getUnits()); rows.createCell(4).setCellValue(list.get(i).getNormalIndicator()); rows.createCell(5).setCellValue(list.get(i).getReferenceRange()); } try { String fileName = new String((master.getItemName() + ".xls").getBytes(), "ISO8859-1"); response.setContentType("application/octet-stream;charset=ISO8859-1"); response.setHeader("Content-Disposition", "attachment;filename=" + fileName); response.addHeader("Pargam", "no-cache"); response.addHeader("Cache-Control", "no-cache"); OutputStream os = response.getOutputStream(); wb.write(os); os.flush(); os.close(); } catch (Exception var8) { throw new ServiceException("导出excel发生异常", var8); } }

  

原文地址:https://www.cnblogs.com/418836844qqcom/p/10983770.html