java 导出excel 并将图片地址转图片

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.zeus.model.*;
import com.zeus.model.common.PageResult;
import com.zeus.service.CommentsService;
import com.zeus.utils.function.EasyExcelCallable;
import com.zeus.utils.listener.CommentListener;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


public static void main(String[] args) {
        String url = "C:\\活动\\1.xls";

        // 数据处理
        List<CommentExcel> dueList = new ArrayList<>();

        // 查询留言数据
        EasyExcelCallable<CommentExcel> callable = dataList -> {
            System.out.println("]]]");
            if(CollectionUtil.isEmpty(dataList)) {
                return;
            }
            for (CommentExcel commentExcel : dataList) {
                CommentV2Param.Comment comment = JSONUtil.toBean(commentExcel.getComment(), CommentV2Param.Comment.class);
                if(Objects.isNull(comment) || CollectionUtil.isEmpty(comment.getPicData())) {
                    continue;
                }
                //TODO 导出后将 此列内容 复制到 txt 文件中 在重新复制回来就 会以图片得形式展示
                String table = "<table><img src={src} width=200></table>";
                String img = comment.getPicData().get(0).getUrl();
                if(StringUtils.isBlank(img)) {
                    continue;
                }
                CommentExcel dueComment = new CommentExcel();
                dueComment.setPhone(commentExcel.getPhone());
                dueComment.setComment(table.replace("{src}", img));
                dueComment.setTime(commentExcel.getTime());
                dueList.add(dueComment);
            }

            System.out.println("==");

            // 写法1 JDK8+
            // since: 3.0.0-beta1
            String fileName = "C:\\活动\\2.xls";
            // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
            // 如果这里想使用03 则 传入excelType参数即可
            EasyExcel.write(fileName, CommentExcel.class).sheet("模板").doWrite(dueList);
        };
        ExcelReader excelReader = EasyExcel.read(url, CommentExcel.class, new CommentListener(callable)).build();
        ReadSheet readSheet = EasyExcel.readSheet(0).build();
        excelReader.read(readSheet);
        // 这里千万别忘记关闭,读的时候会创建临时文件,到时磁盘会崩的
        excelReader.finish();
    }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值