java使用SXSSFWorkbook生成具有图片与文字的Excel表格

2024-03-29 15:38

本文主要是介绍java使用SXSSFWorkbook生成具有图片与文字的Excel表格,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在这里是一个Maven工程,在pom.xml中引入 poi依赖

        <dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.9</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.9</version></dependency>

例子中的情景是从数据库查出了许多记录,记录的是地理信息。记录有几个字段记录的图片保存的绝对路径。根据这些字段的内容生成图片。例如picOneAddr。

记录分为不同的类型,比如楼房,桥梁等。将每种类型生成一个sheet进行分开保存。

具体导出表格的一个大方法如下:

    public String exoprtExcel(final String userId) {//第一步:查询数据--这一步读者自行实现自己的数据查询List<PointInfo> points = null;points = this.dao.getAllCollect(userId);final Map<String, List<PointInfo>> pointMap = new HashMap<>();for (final PointInfo pointInfo : points) {final String pt = pointInfo.getPointType();if (pointMap.containsKey(pt)) {final List<PointInfo> subList = pointMap.get(pt);subList.add(pointInfo);} else {final List<PointInfo> subList = new ArrayList<>();subList.add(pointInfo);pointMap.put(pt, subList);}}//第二步:生成工作簿final SXSSFWorkbook wb = new SXSSFWorkbook();// 对每一种类型生成一个sheetfor (final Map.Entry<String, List<PointInfo>> entry : pointMap.entrySet()) {final List<PointInfo> pts = entry.getValue();// 获取每种类型的名字--作为sheet显示名称--如果不需要分sheet可忽略String typeName = "";if (this.dao.getTypeByTypeCode(pts.get(0).getPointType()) != null) {typeName = this.dao.getTypeByTypeCode(pts.get(0).getPointType()).getPointTypeName();}final Sheet sheet = wb.createSheet(typeName);//生成用于插入图片的容器--这个方法返回的类型在老api中不同final Drawing patriarch = sheet.createDrawingPatriarch();// 为sheet1生成第一行,用于放表头信息final Row row = sheet.createRow(0);// 第一行的第一个单元格的值Cell cell = row.createCell((short) 0);cell.setCellValue("详细地址");cell = row.createCell((short) 1);cell.setCellValue("经度");cell = row.createCell((short) 2);cell.setCellValue("纬度");cell = row.createCell((short) 3);for (int i = 0; i < pts.size(); i++) {final Row each = sheet.createRow(i + 1);Cell infoCell = each.createCell((short) 0);infoCell.setCellValue(pts.get(i).getAddrDetail());infoCell = each.createCell((short) 1);infoCell.setCellValue(pts.get(i).getX());infoCell = each.createCell((short) 2);infoCell.setCellValue(pts.get(i).getY());infoCell = each.createCell((short) 3);//查询获取图片路径信息--该步读者自定义PointPic pic = this.dao.getPicInfoByPointId(pts.get(i).getId());try {if (pic != null) {for (int k = 0; k < 6; k++) {//因为有六张图片,所以循环6次final short colNum = (short) (4+k);infoCell = each.createCell(colNum);BufferedImage img = null;switch (k) {case 0:if (!StringUtils.isEmpty(pic.getPicOneAddr())) {File imgFile = new File(pic.getPicOneAddr());img = ImageIO.read(imgFile);imgFile = null;}break;case 1:if (!StringUtils.isEmpty(pic.getPicTwoAddr())) {File imgFile = new File(pic.getPicTwoAddr());img = ImageIO.read(imgFile);imgFile = null;}break;case 2:if (!StringUtils.isEmpty(pic.getPicThreeAddr())) {File imgFile = new File(pic.getPicThreeAddr());img = ImageIO.read(imgFile);imgFile = null;}break;case 3:if (!StringUtils.isEmpty(pic.getPicFourAddr())) {File imgFile = new File(pic.getPicFourAddr());img = ImageIO.read(imgFile);imgFile = null;}break;case 4:if (!StringUtils.isEmpty(pic.getPicFiveAddr())) {File imgFile = new File(pic.getPicFiveAddr());img = ImageIO.read(imgFile);imgFile = null;}break;case 5:if (!StringUtils.isEmpty(pic.getPicSixAddr())) {File imgFile = new File(pic.getPicSixAddr());img = ImageIO.read(imgFile);imgFile = null;}break;}ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();ImageIO.write(img, "jpg", byteArrayOut);img = null;//设置每张图片插入位置final XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, colNum,i + 1, (short) (colNum + 1), i + 2);//参数为图片插入在表格的坐标,可以自行查看api研究参数anchor.setAnchorType(0);// 插入图片patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));byteArrayOut.close();byteArrayOut = null;}pic = null;}} catch (final Exception e) {e.printStackTrace();}}}final ByteArrayOutputStream os = new ByteArrayOutputStream();try {wb.write(os);} catch (final IOException e) {e.printStackTrace();}final byte[] content = os.toByteArray();final String url = Var.BASE_URL+ File.separator + "output.xls";//读者自定义路径final File file = new File(url);// Excel文件生成后存储的位置。OutputStream fos = null;try {fos = new FileOutputStream(file);fos.write(content);os.close();fos.close();} catch (final Exception e) {e.printStackTrace();}return url;//文件保存成功,返回url供前端使用--例如下载}

这篇关于java使用SXSSFWorkbook生成具有图片与文字的Excel表格的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/858980

相关文章

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Apache Ignite 与 Spring Boot 集成详细指南

《ApacheIgnite与SpringBoot集成详细指南》ApacheIgnite官方指南详解如何通过SpringBootStarter扩展实现自动配置,支持厚/轻客户端模式,简化Ign... 目录 一、背景:为什么需要这个集成? 二、两种集成方式(对应两种客户端模型) 三、方式一:自动配置 Thick

使用Python构建智能BAT文件生成器的完美解决方案

《使用Python构建智能BAT文件生成器的完美解决方案》这篇文章主要为大家详细介绍了如何使用wxPython构建一个智能的BAT文件生成器,它不仅能够为Python脚本生成启动脚本,还提供了完整的文... 目录引言运行效果图项目背景与需求分析核心需求技术选型核心功能实现1. 数据库设计2. 界面布局设计3

使用IDEA部署Docker应用指南分享

《使用IDEA部署Docker应用指南分享》本文介绍了使用IDEA部署Docker应用的四步流程:创建Dockerfile、配置IDEADocker连接、设置运行调试环境、构建运行镜像,并强调需准备本... 目录一、创建 dockerfile 配置文件二、配置 IDEA 的 Docker 连接三、配置 Do

Spring WebClient从入门到精通

《SpringWebClient从入门到精通》本文详解SpringWebClient非阻塞响应式特性及优势,涵盖核心API、实战应用与性能优化,对比RestTemplate,为微服务通信提供高效解决... 目录一、WebClient 概述1.1 为什么选择 WebClient?1.2 WebClient 与

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

Java.lang.InterruptedException被中止异常的原因及解决方案

《Java.lang.InterruptedException被中止异常的原因及解决方案》Java.lang.InterruptedException是线程被中断时抛出的异常,用于协作停止执行,常见于... 目录报错问题报错原因解决方法Java.lang.InterruptedException 是 Jav

Python进行JSON和Excel文件转换处理指南

《Python进行JSON和Excel文件转换处理指南》在数据交换与系统集成中,JSON与Excel是两种极为常见的数据格式,本文将介绍如何使用Python实现将JSON转换为格式化的Excel文件,... 目录将 jsON 导入为格式化 Excel将 Excel 导出为结构化 JSON处理嵌套 JSON:

深入浅出SpringBoot WebSocket构建实时应用全面指南

《深入浅出SpringBootWebSocket构建实时应用全面指南》WebSocket是一种在单个TCP连接上进行全双工通信的协议,这篇文章主要为大家详细介绍了SpringBoot如何集成WebS... 目录前言为什么需要 WebSocketWebSocket 是什么Spring Boot 如何简化 We

java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)

《java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)》:本文主要介绍java中pdf模版填充表单踩坑的相关资料,OpenPDF、iText、PDFBox是三... 目录准备Pdf模版方法1:itextpdf7填充表单(1)加入依赖(2)代码(3)遇到的问题方法2:pd