java导出数据到excel
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | 1 .导入maven<br>!-- https: //mvnrepository.com/artifact/org.apache.poi/poi --><br><dependency><br> <groupId>org.apache.poi</groupId><br> <artifactId>poi</artifactId><br> <version>3.15</version><br> <scope>compile</scope><br></dependency>2.导出excel方法<br>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); } } |
分类:
java-excle
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?