java导出excle导出图片

 FileOutputStream fileOut = null;
        BufferedImage bufferImg = null;
        //先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
        try {
            ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
            File path1 = new File(ResourceUtils.getURL("classpath:").getPath());
            String outPutFilePath = path1.getAbsolutePath()+"/static/custom/img/xxx.png";
            bufferImg = ImageIO.read(new File(outPutFilePath));
            ImageIO.write(bufferImg, "png", byteArrayOut);

            //画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
            HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
            //anchor主要用于设置图片的属性
            HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 255, 255,(short) 0, 0, (short) 2, 1);
            anchor.setAnchorType(3);
            //插入图片
            patriarch.createPicture(anchor, workbook.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (fileOut != null) {
                try {
                    fileOut.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Java 中将图片导出Excel 中,可以使用 Apache POI 库。下面是一个简单的示例代码: ```java import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import javax.imageio.ImageIO; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.*; public class ExcelImageExporter { public static void main(String[] args) throws Exception { // 创建工作簿和工作表 XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("Sheet1"); // 读取图片文件 File imageFile = new File("image.jpg"); BufferedImage bufferedImage = ImageIO.read(imageFile); // 将图片转换为字节数组 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ImageIO.write(bufferedImage, "jpg", byteArrayOutputStream); byte[] imageBytes = byteArrayOutputStream.toByteArray(); // 将字节数组插入到 Excel 中 int pictureIndex = workbook.addPicture(imageBytes, Workbook.PICTURE_TYPE_JPEG); CreationHelper creationHelper = workbook.getCreationHelper(); Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = creationHelper.createClientAnchor(); anchor.setCol1(0); anchor.setRow1(0); Picture picture = drawing.createPicture(anchor, pictureIndex); // 保存 Excel 文件 FileOutputStream outputStream = new FileOutputStream("output.xlsx"); workbook.write(outputStream); outputStream.close(); System.out.println("Excel 文件已生成!"); } } ``` 这段代码会读取名为 `image.jpg` 的图片文件,并将其插入到 Excel 文件中。可以根据需要修改文件名和路径。注意,这个示例代码使用的是 XSSF 格式的 Excel 文件,如果需要生成 XLS 格式的文件,可以使用 HSSF 格式的工作簿和工作表。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值