Java使用POI导出图片的Excel

文章目录

  • 依赖
  • 导包
  • 测试类
  • 打开查看
  • 结合实际项目使用

依赖

 <dependency>
            <groupId>org.apache.poigroupId>
            <artifactId>poiartifactId>
            <version>3.17version>
        dependency>
        <dependency>
            <groupId>org.apache.poigroupId>
            <artifactId>poi-ooxml-schemasartifactId>
            <version>3.17version>
        dependency>
        <dependency>
            <groupId>org.apache.poigroupId>
            <artifactId>poi-ooxmlartifactId>
            <version>3.17version>
        dependency>

导包

import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.net.URL;

测试类

public static void main(String[] args) throws Exception {

        FileOutputStream fileOut = null;

        //先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray

        String imgUrl = "https://img2.woyaogexing.com/2019/04/17/e7370782b0484237926113770074cd81!400x400.jpeg";
        URL url = new URL(imgUrl);
        //获取文件后缀名
        String suffix = imgUrl.substring(imgUrl.lastIndexOf(".") + 1);
        BufferedImage bufferImg = ImageIO.read(url);

        //以本地的方式图片、注释上面四行有效代码
        //        BufferedImage bufferImg = null;
        //        bufferImg = ImageIO.read(new File("D:/test.jpg"));

        ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();


        //这里要注意formatName要缓存后缀名
        ImageIO.write(bufferImg, suffix, byteArrayOut);

        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet1 = wb.createSheet("test picture");
        sheet1.setDefaultColumnWidth((short)20);
        sheet1.setDefaultRowHeight((short)2000);
        //画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
        HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
            for(int i =0;i<10;i++){
             //anchor主要用于设置图片的属性
             HSSFClientAnchor anchor =
                     new HSSFClientAnchor
                             (        0, //x缩放
                                     0, // y缩放
                                     1023, //最大1023
                                     255, //最大255
                                     (short) 4,  //于下下个参数进行定位 0开始
                                     i, //在第几行
                                     (short) 4, //宽度占几格 0开始
                                     i //第几列
                             );
//          anchor.setAnchorType(3);
            //插入图片
            patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));

         }
        fileOut = new FileOutputStream("D:/测试Excel.xls");
        // 写入excel文件
        wb.write(fileOut);
        System.out.println("----Excel文件已生成------");
        }

打开查看

Java使用POI导出图片的Excel_第1张图片

结合实际项目使用

// 声明一个工作薄
        HSSFWorkbook workBook = new HSSFWorkbook();
        // 生成一个表格
        HSSFSheet sheet = workBook.createSheet();
        sheet.createFreezePane(0, 1, 0, 1);
        workBook.setSheetName(0, "个人明细");
        CellStyle style = workBook.createCellStyle();
        Font font = workBook.getFontAt((short) 0);
        font.setCharSet(HSSFFont.DEFAULT_CHARSET);
        //更改默认字体大小
        font.setFontHeightInPoints((short) 12);
        font.setFontName("宋体");
        style.setFont(font);

        //默认宽高
        sheet.setDefaultColumnWidth((short)20);
        sheet.setDefaultRowHeight((short)2000);

        // 创建表格标题行 第一行
        HSSFRow titleRow = sheet.createRow(0);
        titleRow.createCell(0).setCellValue("员工姓名");
        titleRow.createCell(1).setCellValue("预计人天");
        titleRow.createCell(2).setCellValue("实际人天");
        titleRow.createCell(3).setCellValue("缺少人天");
        titleRow.createCell(4).setCellValue("员工照片");

		// 创建表格标题行 第一行
        HSSFRow titleRow = sheet.createRow(0);
        titleRow.createCell(0).setCellValue("员工姓名");
        titleRow.createCell(1).setCellValue("预计人天");
        titleRow.createCell(2).setCellValue("实际人天");
        titleRow.createCell(3).setCellValue("缺少人天");
        titleRow.createCell(4).setCellValue("员工照片");

        BufferedImage bufferImg = null;
        //先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
        ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
        try {
            bufferImg = ImageIO.read(new File("D:/test.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            ImageIO.write(bufferImg, "jpg", byteArrayOut);
        } catch (IOException e) {
            e.printStackTrace();
        }
        HSSFPatriarch patriarch = sheet.createDrawingPatriarch();

        for (int i = 0; i < list.size(); i++) {
            CheckWorkVo checkWorkVo = list.get(i);
            HSSFRow titleRowNext = sheet.createRow(i + 1);
            titleRowNext.createCell(0).setCellValue(String.valueOf(checkWorkVo.getNickName()));
            titleRowNext.createCell(1).setCellValue(String.valueOf(checkWorkVo.getPreWorkDay()));
            titleRowNext.createCell(2).setCellValue(String.valueOf(checkWorkVo.getNowWorkDay()));
            titleRowNext.createCell(3).setCellValue(String.valueOf(checkWorkVo.getLackWorkDay()));
            //画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
                //anchor主要用于设置图片的属性
            HSSFClientAnchor anchor =
                        new HSSFClientAnchor
                                (        0, //x缩放
                                   0, // y缩放
                                   1023, //最大1023
                                   255, //最大255
                                   (short) 4,  //宽度占几格 0开始
                                   i+1, //在第几行
                                   (short) 4, //宽度占几格 0开始
                                   i+1 //第几列
                                        );
                //插入图片
            patriarch.createPicture(anchor, workBook.addPicture(byteArrayOut.toByteArray(), XSSFWorkbook.PICTURE_TYPE_JPEG));
        }
        return workBook;

打开测试
Java使用POI导出图片的Excel_第2张图片

你可能感兴趣的:(java)