Java导出excel带图片(希望能帮助你们节省时间)

ops/2024/9/23 7:28:21/

第一天太慌张,下班逃跑,一夜没睡好,第二天决定搞出来。
查了好多博客,感觉都挺繁琐的,好多工具类、引入类找不到。经过一上午的琢磨,终于搞定。记录一下

借鉴了这个博主的文章

excel_4">需求前端点击导出按钮,导出excel,并且带出图片

在这里插入图片描述

引入pom依赖

java">  <!-- poi处理 --><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.17</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.17</version></dependency><dependency><groupId>com.github.ulisesbocchio</groupId><artifactId>jasypt-spring-boot-starter</artifactId><version>3.0.3</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>2.1.4</version></dependency>

我使用了excel模板,放到了resources下
在这里插入图片描述

使用Get请求

java">//通过模板文件,导入模板数据,并在浏览器端下载 startTime endTime@GetMapping("/exportAddressLog")public void exportAddressLog(@RequestParam(value = "startTime") String startTime,@RequestParam(value = "endTime") String endTime,HttpServletResponse response) throws Exception {URL resource = getClass().getClassLoader().getResource("Address.xlsx");String path = resource.getPath();//文件输入流FileInputStream in = new FileInputStream(path);//创建excel流(从指定文件流)Workbook excel = WorkbookFactory.create(in);//定义数据List<AddressLogResource> excelDataList = logService.getAddressLogList(startTime, endTime);//获取第一页Sheet sheet = excel.getSheetAt(0);//填充数据for (int i = 0; i < excelDataList.size(); i++) {//模板文件从第三行开始填Row row = sheet.createRow(i+1);int columnIndex4 = 4; // 设置第4列的宽度int columnIndex3 = 4; // 设置第4列的宽度int width4 = 20; // 设置宽度值为20个字符int width3 = 40; // 设置宽度值为20个字符sheet.setColumnWidth(columnIndex3, width3 * 256);sheet.setColumnWidth(columnIndex4, width4 * 256);row.setHeight((short) (160*10));Cell cell = row.createCell(0);Cell cell1 = row.createCell(1);Cell cell2 = row.createCell(2);Cell cell3 = row.createCell(3);Cell cell4 = row.createCell(4);Cell cell5 = row.createCell(5);//第一个单元格填排名cell.setCellValue(i + 1);//第二个单元格填姓名cell1.setCellValue(excelDataList.get(i).getAddressCoordinate());//第三个单元格填分数cell2.setCellValue(excelDataList.get(i).getAddressName());cell3.setCellValue(excelDataList.get(i).getCameraSn());LocalDateTime logTime = excelDataList.get(i).getLogTime();String dateTimeStr = logTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));System.out.println("当前字符串日期时间:" + dateTimeStr);cell4.setCellValue(dateTimeStr);//插入图片String img = excelDataList.get(i).getImagePath();imgxx(img,sheet,i,5,excel);
//            cell4.setCellValue(excelDataList.get(i).getImagePath());}//设置Content-Type为appl ication/vnd.openxmLformats -officedocument. spreadsheetmL. sheetresponse.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");//设置Content-Dispositiontattachment;filename-excel.xLsx,表示将文件下载到本地,并指定文件名为excel.xLsxresponse.setHeader("Content-Disposition", "attachment;filename=excel.xlsx");// 通过输出流将Excel文件下载到客户端浏览器ServletOutputStream out = response.getOutputStream();excel.write(out);out.flush();//关闭资源out.close();excel.close();}

插入图片单独抽出的方法

java">//插入图片private String imgxx(String img, Sheet sheet,int i,int j,Workbook excel){URL photoFile = null;try {photoFile = new URL(img);// 先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArrayByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();//将图片读入BufferedImage对象BufferedImage bufferImg = ImageIO.read(photoFile);// 将图片写入流中ImageIO.write(bufferImg, "jpg", byteArrayOut);// 利用HSSFPatriarch将图片写入EXCELDrawing<?> patriarch = sheet.createDrawingPatriarch();// 图片一导出到单元格I3-5中 列开始:8 行开始:2 列结束:9 行结束:5XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, j, i + 1, j, i + 1);anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE);// 插入图片内容Picture picture =  patriarch.createPicture(anchor, excel.addPicture(byteArrayOut.toByteArray(), XSSFWorkbook.PICTURE_TYPE_JPEG));picture.resize(1,1);} catch (MalformedURLException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}return null;}

导出的图片
在这里插入图片描述
前端不能使用ajax访问,直接使用 location.href= url
在这里插入图片描述
在这里插入图片描述
OK…收工


http://www.ppmy.cn/ops/44664.html

相关文章

note-网络是怎样连接的4 接入网和网络运营商

助记提要 网络包从用户传输到互联网的过程信号的调制方式ADSL使用多个频率的合成波传输信号分离器的作用电话线的特点光纤的构造光纤的原理单模光纤和多模光纤光纤接入网的两种接入方式PPP拨号上网过程ADSL和FTTH使用PPPoE的方式PPPoE的规则隧道其他接入认证方式 PPPoA和DHCP网…

Linux--线程的认识(一)

线程的概念 线程&#xff08;Thread&#xff09;是操作系统中进行程序执行的最小单位&#xff0c;也是程序调度和分派的基本单位。它通常被包含在进程之中&#xff0c;是进程中的实际运作单位。一个线程指的是进程中一个单一顺序的控制流&#xff0c;一个进程中可以并发多个线…

网络信息安全

目录 引言 网络信息安全的基本概念 定义 主要目标 网络信息安全的范围 主要威胁 恶意软件 黑客攻击 拒绝服务攻击 社交工程 内部威胁 常用技术和防护措施 加密技术 防火墙 入侵检测和防御系统 访问控制 多因素认证 安全审计和监控 安全培训和意识提升 未来发…

LabVIEW通过以太网控制PLC程序开发

在使用LabVIEW通过以太网控制PLC程序开发时&#xff0c;需要综合考虑硬件、软件和通信协议的协调工作。以下是详细步骤、注意事项、重点和难点分析&#xff0c;以及几种实现方式及其特点的概述。 实现步骤 确定硬件和软件环境&#xff1a; 确定PLC型号和品牌&#xff08;如西门…

【机器学习】基于tensorflow实现你的第一个DNN网络

博客导读&#xff1a; 《AI—工程篇》 AI智能体研发之路-工程篇&#xff08;一&#xff09;&#xff1a;Docker助力AI智能体开发提效 AI智能体研发之路-工程篇&#xff08;二&#xff09;&#xff1a;Dify智能体开发平台一键部署 AI智能体研发之路-工程篇&#xff08;三&am…

理解多线程看这一篇就够了

一、基本概念与关系 程序 程序是含有指令和数据的文件&#xff0c;静态地存储在磁盘等存储设备上。它是软件的实体&#xff0c;但未被激活。 进程 进程是程序的一次执行过程&#xff0c;是系统运行程序的基本单位。当程序被操作系统加载并执行时&#xff0c;就成为一个进程&a…

C语言序列化和反序列化--TPL(一)

TPL TPL说明网站 C语言中高效的序列化 您可以使用tpl快速轻松地存储和重新加载C数据。Tpl是一个用于序列化C数据的库。数据以自然二进制形式存储。该API很小&#xff0c;并试图保持“不碍事”。Tpl可以序列化许多C数据类型&#xff0c;包括结构。Tpl与文件、内存缓冲区和文件…

MySQL 使用 binlog 找回已删除数据

背景&#xff1a;使用 delete 删除数据少添加了条件 mysqlbinlog --base64-outputdecode-rows -v --start-datetime"2024-05-27 11:48:48" --stop-datetime"2024-05-27 12:59:52" binlog文件 > 转后sql 使用其他方式&#xff0c;转换的SQL并没有sql语…