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

相关文章

python使用库爬取m3u8文件的示例

《python使用库爬取m3u8文件的示例》本文主要介绍了python使用库爬取m3u8文件的示例,可以使用requests、m3u8、ffmpeg等库,实现获取、解析、下载视频片段并合并等步骤,具有... 目录一、准备工作二、获取m3u8文件内容三、解析m3u8文件四、下载视频片段五、合并视频片段六、错误

javax.net.ssl.SSLHandshakeException:异常原因及解决方案

《javax.net.ssl.SSLHandshakeException:异常原因及解决方案》javax.net.ssl.SSLHandshakeException是一个SSL握手异常,通常在建立SS... 目录报错原因在程序中绕过服务器的安全验证注意点最后多说一句报错原因一般出现这种问题是因为目标服务器

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再

spring中的ImportSelector接口示例详解

《spring中的ImportSelector接口示例详解》Spring的ImportSelector接口用于动态选择配置类,实现条件化和模块化配置,关键方法selectImports根据注解信息返回... 目录一、核心作用二、关键方法三、扩展功能四、使用示例五、工作原理六、应用场景七、自定义实现Impor

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解