Java导出excel带图片- 封装

package com.yfjgpt.disposalCompany.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Picture;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.*;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @author: guo-xz
 * @Project:
 * @Date: 2022年08月11日 09:16
 */
public class ExcelUtilImpl{

	/**
	 *  导出
	 *
	 * @param list 数据列表
	 * @param excelHeaders 表头
	 * @param keys 导出字段属性
	 * @param fileName 文件名称
	 * @param response 相应对象
	 * @return
	 */
	public static void excelImg(List<Map> list, String[] excelHeaders, String[] keys, String fileName, HttpServletResponse response){
		excelImg(list, excelHeaders, keys, null, fileName, response);
	}

	/**
	 *  导出
	 *
	 * @param list 数据列表
	 * @param excelHeaders 表头
	 * @param keys 导出字段属性
	 * @param imgs 导出图片字段属性,用逗号隔开
	 * @param fileName 文件名称
	 * @param response 相应对象
	 * @return
	 */
	public static void excelImg(List<Map> list, String[] excelHeaders, String[] keys, String imgs, String fileName, HttpServletResponse response){
		// 获取数据列表
		// 创建一个工作簿,对应文件
		XSSFWorkbook workBook = new XSSFWorkbook();
		// 创建一个sheet工作表
		XSSFSheet sheet = workBook.createSheet(fileName + DateUtil.time());
		// 设置表头单元格样式
		XSSFCellStyle headstyle = workBook.createCellStyle();
		// 设置居中
		headstyle.setAlignment(HorizontalAlignment.CENTER);
		headstyle.setVerticalAlignment(VerticalAlignment.CENTER);
		XSSFFont headFont = workBook.createFont();
		headFont.setFontHeight(14);
		headFont.setBold(true);
		headstyle.setFont(headFont);

		// 创建一般单元格样式
		XSSFCellStyle cellstyle = workBook.createCellStyle();
		cellstyle.setAlignment(HorizontalAlignment.CENTER);
		cellstyle.setVerticalAlignment(VerticalAlignment.CENTER);
		cellstyle.setWrapText(true);
		XSSFFont cellFont = workBook.createFont();
		cellFont.setFontHeight(11);
		cellstyle.setFont(cellFont);
		XSSFRow headRow = sheet.createRow(0);
		for (int i = 0; i < excelHeaders.length; i++) {
			XSSFCell cell = headRow.createCell(i);
			cell.setCellValue(excelHeaders[i]);
			cell.setCellStyle(headstyle);
			sheet.setColumnWidth(i, (20 * 256));
		}
		// 创建内容
		XSSFRow row = null;
		for (int rowIndex = 0; rowIndex < list.size(); rowIndex++) {
			row = sheet.createRow(rowIndex + 1);
			row.setHeight((short) (40 * 20));
			// 单元格
			XSSFCell cell = null;
			String s = JSONObject.toJSONString(list.get(rowIndex));
			// 转成map
			Map<String,Object> map = JSONObject.parseObject(s, HashMap.class);
			for (int j = 0; j < keys.length; j++) {
				cell = row.createCell(j);
				cell.setCellStyle(cellstyle);
				if(Func.isNotEmpty(imgs) && imgs.contains((String)map.get(keys[j]))){
					cell.setCellValue("");
					try{
						URL photoFile = new URL( (String) map.get(keys[j]));
						// 先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
						ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
						//将图片读入BufferedImage对象
						BufferedImage bufferImg = ImageIO.read(photoFile);
						// 将图片写入流中
						ImageIO.write(bufferImg, "jpg", byteArrayOut);
						// 利用HSSFPatriarch将图片写入EXCEL
						XSSFDrawing patriarch = sheet.createDrawingPatriarch();
						// 图片一导出到单元格I3-5中 列开始:8 行开始:2 列结束:9 行结束:5
						XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, j, rowIndex + 1, j, rowIndex + 1);
						anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE);
						// 插入图片内容
						Picture picture =  patriarch.createPicture(anchor, workBook.addPicture(byteArrayOut
							.toByteArray(), XSSFWorkbook.PICTURE_TYPE_JPEG));
						picture.resize(1.05,1.10);
						//将图片插入工作表
					}catch (Exception e) {
						cell.setCellValue("暂无图片");
					}
				}else{
					cell.setCellValue((String) map.get(keys[j]));
				}
			}
		}
		try {
			//最终已流的形式返回
			OutputStream out = null;
			out = response.getOutputStream();
			response.setHeader("content-type", "application/octet-stream");
			response.setContentType("application/octet-stream");
			response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName + DateUtil.time() + ".xlsx", "UTF-8"));
			workBook.write(out);
			out.flush();
			out.close();
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值