一个方法,需要传入 有参数的实体类,赋值到xls里面,和一个有多张图片的base64字段的字符串,

@Overridepublic HSSFWorkbook getHSSFWorkbook(List<Entity> entity, String imgbase64, HSSFWorkbook wb) {// 第一步,创建一个HSSFWorkbook,对应一个Excel文件if (wb == null) {wb = new HSSFWorkbook();}// 表头字体HSSFFont headerFont = wb.createFont();headerFont.setFontName("微软雅黑");headerFont.setFontHeightInPoints((short) 14);headerFont.setBoldweight(Font.BOLDWEIGHT_NORMAL);headerFont.setColor(HSSFColor.BLACK.index);// 正文字体Font contextFont = wb.createFont();contextFont.setFontName("微软雅黑");contextFont.setFontHeightInPoints((short) 12);contextFont.setBoldweight(Font.BOLDWEIGHT_NORMAL);contextFont.setColor(HSSFColor.BLACK.index);// 超链接字体Font linkFont = wb.createFont();linkFont.setFontName("微软雅黑");linkFont.setColor(IndexedColors.BLUE.getIndex());linkFont.setUnderline(Font.U_SINGLE);linkFont.setFontHeightInPoints((short) 12);linkFont.setBoldweight(Font.BOLDWEIGHT_NORMAL);// 表头样式,左右上下居中String color = "DCF0F0";int r = Integer.parseInt((color.substring(0, 2)), 16);int g = Integer.parseInt((color.substring(2, 4)), 16);int b = Integer.parseInt((color.substring(4, 6)), 16);HSSFPalette paletee = wb.getCustomPalette();paletee.setColorAtIndex((short) 9, (byte) r, (byte) g, (byte) b);CellStyle headerStyle = wb.createCellStyle();headerStyle.setFont(headerFont);headerStyle.setFillForegroundColor((short) 9);headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);headerStyle.setAlignment(CellStyle.ALIGN_CENTER);// 左右居中headerStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);// 上下居中headerStyle.setWrapText(true);// 自动换行headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框String color_ = "EBEBEB";int r_ = Integer.parseInt((color_.substring(0, 2)), 16);int g_ = Integer.parseInt((color_.substring(2, 4)), 16);int b_ = Integer.parseInt((color_.substring(4, 6)), 16);HSSFPalette paletee_ = wb.getCustomPalette();paletee_.setColorAtIndex((short) 10, (byte) r_, (byte) g_, (byte) b_);CellStyle headerStyle_ = wb.createCellStyle();headerStyle_.setFont(headerFont);headerStyle_.setFillForegroundColor((short) 10);headerStyle_.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);headerStyle_.setAlignment(CellStyle.ALIGN_CENTER);// 左右居中headerStyle_.setVerticalAlignment(CellStyle.VERTICAL_CENTER);// 上下居中headerStyle_.setWrapText(true);// 自动换行headerStyle_.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框headerStyle_.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框headerStyle_.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框headerStyle_.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框// 单元格样式(可编辑),左右上下居中 边框CellStyle commonStyle = wb.createCellStyle();commonStyle.setFont(contextFont);commonStyle.setAlignment(CellStyle.ALIGN_CENTER);// 左右居中commonStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);// 上下居中commonStyle.setLocked(false);commonStyle.setWrapText(true);// 自动换行commonStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框commonStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框commonStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框commonStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框CellStyle commonStylea = wb.createCellStyle();commonStylea.setFont(contextFont);commonStylea.setAlignment(CellStyle.ALIGN_CENTER);// 左右居中commonStylea.setVerticalAlignment(CellStyle.VERTICAL_CENTER);// 上下居中commonStylea.setLocked(false);commonStylea.setWrapText(true);// 自动换行commonStylea.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框commonStylea.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框commonStylea.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框commonStylea.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框HSSFDataFormat format = wb.createDataFormat();commonStylea.setDataFormat(format.getFormat("0.00"));// 单元格样式(不可编辑),左右上下居中 边框CellStyle commonStyle1 = wb.createCellStyle();commonStyle1.setFont(contextFont);commonStyle1.setAlignment(CellStyle.ALIGN_CENTER);// 左右居中commonStyle1.setVerticalAlignment(CellStyle.VERTICAL_CENTER);// 上下居中commonStyle1.setWrapText(true);// 自动换行commonStyle1.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框commonStyle1.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框commonStyle1.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框commonStyle1.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框// 单元格样式(不可编辑),左右上下居中 无边框CellStyle commonStyle2 = wb.createCellStyle();commonStyle2.setFont(contextFont);commonStyle2.setAlignment(CellStyle.ALIGN_CENTER);// 左右居中commonStyle2.setVerticalAlignment(CellStyle.VERTICAL_CENTER);// 上下居中commonStyle2.setWrapText(true);// 自动换行// 单元格样式(可编辑),左右上下居中 无边框CellStyle commonStyle3 = wb.createCellStyle();commonStyle3.setFont(contextFont);commonStyle3.setLocked(false);commonStyle3.setAlignment(CellStyle.ALIGN_CENTER);// 左右居中commonStyle3.setVerticalAlignment(CellStyle.VERTICAL_CENTER);// 上下居中commonStyle3.setWrapText(true);// 自动换行CellStyle commonStyle3a = wb.createCellStyle();commonStyle3a.setFont(contextFont);commonStyle3a.setLocked(false);commonStyle3a.setAlignment(CellStyle.ALIGN_CENTER);// 左右居中commonStyle3a.setVerticalAlignment(CellStyle.VERTICAL_CENTER);// 上下居中commonStyle3a.setWrapText(true);// 自动换行commonStyle3a.setDataFormat(format.getFormat("0.00"));CellStyle commonStyle4 = wb.createCellStyle();commonStyle4.setFont(linkFont);commonStyle4.setAlignment(CellStyle.ALIGN_CENTER);// 左右居中commonStyle4.setVerticalAlignment(CellStyle.VERTICAL_CENTER);// 上下居中commonStyle4.setWrapText(true);// 自动换行commonStyle4.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框commonStyle4.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框commonStyle4.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框commonStyle4.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框HSSFSheet sheet = wb.createSheet("xls数据导出例子");sheet.protectSheet("juan123456");sheet.setColumnWidth(0, 6000);sheet.setColumnWidth(1, 14000);sheet.setColumnWidth(2, 6000);sheet.setColumnWidth(3, 10000);// 添加行HSSFRow row = sheet.createRow(0);HSSFCell cell = null;cell = row.createCell(0);cell.setCellValue("名称");cell.setCellStyle(headerStyle);cell = row.createCell(1);cell.setCellValue(entity.get(0).getMC());cell.setCellStyle(commonStyle1);cell = row.createCell(2);cell.setCellValue("ID");cell.setCellStyle(headerStyle);cell = row.createCell(3);cell.setCellValue(entity.get(0).getID());cell.setCellStyle(commonStyle1);row = sheet.createRow(1);cell = row.createCell(0);cell.setCellValue("元素");cell.setCellStyle(headerStyle);cell = row.createCell(1);cell.setCellValue(entity.get(0).getDT());cell.setCellStyle(commonStyle1);cell = row.createCell(2);cell.setCellValue("天气");cell.setCellStyle(headerStyle);cell = row.createCell(3);cell.setCellValue(entity.get(0).getTQ());cell.setCellStyle(commonStyle1);row = sheet.createRow(2);cell = row.createCell(0);cell.setCellValue("类型");cell.setCellStyle(headerStyle);cell = row.createCell(1);cell.setCellValue(entity.get(0).getLX());cell.setCellStyle(commonStyle1);cell = row.createCell(2);cell.setCellValue("人员");cell.setCellStyle(headerStyle);cell = row.createCell(3);cell.setCellValue(entity.get(0).getRXM());cell.setCellStyle(commonStyle1);row = sheet.createRow(3);row.setHeight((short) 2000);cell = row.createCell(0);cell.setCellValue("描述");cell.setCellStyle(headerStyle);cell = row.createCell(1);cell.setCellValue(entity.get(0).getMS());cell.setCellStyle(commonStyle);cell = row.createCell(2);cell.setCellStyle(commonStyle);cell = row.createCell(3);cell.setCellStyle(commonStyle);// 合并单元格sheet.addMergedRegion(new CellRangeAddress(5, 5, 1, 3));row = sheet.createRow(4);row.setHeight((short) 2000);cell = row.createCell(0);cell.setCellValue("方案");cell.setCellStyle(headerStyle);cell = row.createCell(1);cell.setCellValue(entity.get(0).getFA());cell.setCellStyle(commonStyle);cell = row.createCell(2);cell.setCellStyle(commonStyle);cell = row.createCell(3);cell.setCellStyle(commonStyle);// 合并单元格sheet.addMergedRegion(new CellRangeAddress(6, 6, 1, 3));//画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)HSSFPatriarch patriarch = sheet.createDrawingPatriarch();   //缺陷图片if(("").equals(imgbase64) || null != imgbase64){com.alibaba.fastjson.JSONArray imgArrayJson=com.alibaba.fastjson.JSONArray.parseArray(imgbase64);for(int i=0;i<imgArrayJson.size();i++){//图片的名字String nameImg=com.alibaba.fastjson.JSONObject.parseObject(com.alibaba.fastjson.JSONObject.toJSONString(imgArrayJson.get(i))).getString("name");//图片的base64编码,但是需要,分割String code=com.alibaba.fastjson.JSONObject.parseObject(com.alibaba.fastjson.JSONObject.toJSONString(imgArrayJson.get(i))).getString("code");//图片的分割,0坐标是格式String[] imgbase64A=code.split(",");//图片的base64编码,String imgbase64Str = imgbase64A[1];byte[] bytes1 = Base64.decode(imgbase64Str);//anchor主要用于设置图片的属性/*** HSSFClientAnchor用于创建一个新的端锚,并设置锚的左下和右下坐标,用于图片插入,画线等操作。HSSFClientAnchor(int dx1, int dy1, int dx2, int dy2, short col1, int row1, short col2, int row2)dx1 dy1 起始单元格中的x,y坐标.dx2 dy2 结束单元格中的x,y坐标col1,row1 指定起始的单元格,下标从0开始col2,row2 指定结束的单元格 ,下标从0开始*/HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 255, 255,(short) (i+1), 7, (short) (i+2), 8);   anchor.setAnchorType(3);   //插入图片  patriarch.createPicture(anchor, wb.addPicture(bytes1, HSSFWorkbook.PICTURE_TYPE_JPEG)); }}row = sheet.createRow(5);row.setHeight((short) 2000);cell = row.createCell(0);cell.setCellValue("图片");cell.setCellStyle(headerStyle);cell = row.createCell(1);cell.setCellValue("");cell.setCellStyle(commonStyle);cell = row.createCell(2);cell.setCellStyle(commonStyle);cell = row.createCell(3);cell.setCellStyle(commonStyle);// 合并单元格sheet.addMergedRegion(new CellRangeAddress(7, 7, 1, 3));return wb;}

调用的时候,通过调用服务接口 ,把查询到的数据List, 图片base64,放进去。如下

                         HSSFWorkbook wb = service.getHSSFWorkbook(entityList,imgjson, null);inputStream = service.getInputStream(wb);inputStream = excelService.getInputStream(wb);while ((length = inputStream.read(buffer)) != -1) {zipOut.write(buffer, 0, length);}if (inputStream != null) {inputStream.close();}

前端的话就正常文件下载,就可以了

使用POI导出数据和图片,相关推荐

  1. 使用POI导出数据到excel代码

    使用POI导出数据到excel代码 POM文件 <!-- 导出excel --> <dependency><groupId>org.apache.poi</g ...

  2. Java使用poi导出数据到excel(包括xls和xlsx两种格式)并通过浏览器下载

    情景:将数据导出到excel是java开发常用的功能,数据量不大的时候,xls和xlsx两种格式的文件都行,但是数据量太大的时候就有区别了,xls格式的文件一个sheet页最多只能存六万多条数据,而x ...

  3. java中使用poi导出ppt(图片和表格)

    java使用POI导出PPT(超简单方法,包含图片和表格) 在做项目中遇到一个需求,将职员的信息导出成一个形式固定的ppt文档,poi有许多方法可以实现,因为我是一名Java小白,于是便想用最简单的方 ...

  4. POI导出数据内存溢出问题

    POI之前的版本不支持大数据量处理,如果数据过多则经常报OOM错误,有时候调整JVM大小效果也不是太好.3.8版本的POI新出来了SXSSFWorkbook,可以支持大数据量的操作,只是SXSSFWo ...

  5. poi导出数据文件名错误_POI导出Excel报错“扩展名与文件的格式不匹配”

    下面是我用POI导出Excel的实例: 依赖的jar包 org.apache.poi poi 4.0.1 工具类 public class ExportExcel { // 显示的导出表的标题 pri ...

  6. POI导出数据至Excel,cpu飙升 cpu占用很高,原因排查

    零 干货满满 1测试环境开启GC日志 -Xloggc:gc.log -XX:+PrintGCTimeStamps -XX:+PrintGCDetails -XX:+HeapDumpBeforeFull ...

  7. POI 导出Excel 带图片导出 使用XSSFWorkbook

    参考链接:POI导出图片到Excel不生效 (2007以上版本)_rj_han的博客-CSDN博客 //数据源 查询库 List<FormMt> mtList = formMtMapper ...

  8. Java使用POi导出Excel(包含图片)

    Java使用poi组件导出excel报表,能导出excel报表的还可以使用jxl组件,但jxl想对于poi功能有限,jxl应该不能载excel插入浮动层图片,poi能很好的实现输出excel各种功能, ...

  9. poi导出数据到word,带图片且图片数量不确定(能确定数量范围,这里是3-20张)

    注:有更好解决方法,参考:https://blog.csdn.net/m0_49605579/article/details/122583318 1.导入依赖 maven版: <dependen ...

最新文章

  1. 36晋级12第五场:冯志刚决胜入围(节目视频)
  2. 董彬教授:用深度神经网络学习偏微分方程及其数值求解的离散格式
  3. .NET base与this
  4. 解决java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed
  5. STL——萃取机制(Traits)
  6. 《Sibelius 脚本程序设计》连载(十四) - 2.1 注释、语句、语句块
  7. 配置Windows 2008 R2 防火墙允许远程访问SQL Server 2008 R2 更改端口 连接字符串 IP+逗号+端口号...
  8. Redis持久化_Redis事务_Redis删除策略
  9. LeetCode 152. 乘积最大子序列(DP)
  10. Linux开机启动过程(9):进入内核入口点之前最后的准备工作
  11. JAR包的JDK版本查看与设置
  12. 6款主流PDF编辑器测试,快来看看哪一款最适合你吧
  13. 新浪云 php,php在新浪云中使用imagick的方法
  14. android版本内存卡,版本等级繁多 教你如何挑选手机内存卡
  15. 百度未授权使用地图API,可能是因为您提供的密钥不是有效的百度LBS开放平台密钥,或此密钥未对本应用的百度地图JavaScriptAPI授权。您可以访问如下网址了解如何获取有效的密钥
  16. 工程流体力学笔记暂记43 (收缩喷管中的流动)
  17. golang:%v,%+v,%#v区别
  18. [ARM] ARM处理器的7种工作模式和2种工作状态
  19. firefox firbug 的“阻挡”
  20. android google 分屏 多窗口 按home键界面错乱故障分析(一)分屏的整个流程分析

热门文章

  1. 基于知识图谱的表示学习——Trans系列算法介绍(一)
  2. 中国计量大学现代科技学院第四届“中竞杯”程序设计校赛(同步赛) F.爬塔(DP)
  3. 带你了解微信代运营公众号到底怎么做
  4. mx250显卡天梯图_MX250和150差别大吗?MX250和MX150区别对比 (全文)
  5. 网页游戏老手村《梦幻西游网页版》项目开发经验分享
  6. 财报解读:硬件支撑思科增长,云平台何时能突围?
  7. 克制授信,蚂蚁集团将严控年轻人额度
  8. 和李兄之《定风波· 冬峦轻寒桂落香》一首
  9. 美团技术团队当家运营:美美正式出道啦(含福利)
  10. 求解线性方程组的方法Matlab程序