使用POI导出数据和图片,

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


	@Override
	public 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();
							}

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值