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

相关文章

vue使用docxtemplater导出word

《vue使用docxtemplater导出word》docxtemplater是一种邮件合并工具,以编程方式使用并处理条件、循环,并且可以扩展以插入任何内容,下面我们来看看如何使用docxtempl... 目录docxtemplatervue使用docxtemplater导出word安装常用语法 封装导出方

Linux换行符的使用方法详解

《Linux换行符的使用方法详解》本文介绍了Linux中常用的换行符LF及其在文件中的表示,展示了如何使用sed命令替换换行符,并列举了与换行符处理相关的Linux命令,通过代码讲解的非常详细,需要的... 目录简介检测文件中的换行符使用 cat -A 查看换行符使用 od -c 检查字符换行符格式转换将

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

使用Jackson进行JSON生成与解析的新手指南

《使用Jackson进行JSON生成与解析的新手指南》这篇文章主要为大家详细介绍了如何使用Jackson进行JSON生成与解析处理,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 核心依赖2. 基础用法2.1 对象转 jsON(序列化)2.2 JSON 转对象(反序列化)3.

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.