Use EasyExcel to add pictures and text in the same cell, and do formatting

 The format requirements are as follows:

 Requirement: In a cell, there is a picture on the left and text on the right, and there is a gap between the text and the picture.

Idea: add a picture in a cell and set the position of the picture, then add text and set the position of the text.

After viewing the EasyExcel document, I found that
adding a picture is to fill the current cell by default, but you can set the top, bottom, left, and right margins .
The default is to add text from left to right without indentation; but you can set the indentation of the first line if you select the text to be displayed on the left .

Then knowing so much, you can operate;

First set the format on the line that needs to be operated on in the entity class:

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.ContentStyle;
import lombok.Data;


@Data
public class TestExcel {

    @ExcelProperty(value = "单元格标题", index=3)
    @ColumnWidth(70)//单元格宽度为70
    @ContentRowHeight(100)//高度为100
    @ContentStyle(
    horizontalAlignment = HorizontalAlignmentEnum.LEFT, //水平居左
    verticalAlignment = VerticalAlignmentEnum.CENTER, //垂直居中
    wrapped = BooleanEnum.TRUE)//主要是为了导出时直接换行
    private WriteCellData<Void> writeCellData;
}

Then operate on this cell:

public class WriteCellUtil{
    /**
     * 单元格内填充图片及文字
     * 将图片信息放入到WriteCellData中(单元格)
     * @param imagePath 图片链接 https://....jpg
     * @param content 添加的文字
     * @param indent 首行缩进
     * @param top 图片上边距
     * @param bottom 图片下边距
     * @param left 图片左边距
     * @param right 图片右边距
     * @return
     */
    public static WriteCellData fillImage(String imagePath, String content, Short indent, Integer top, Integer bottom, Integer left, Integer right ){
        WriteCellData<Void> writeCellData = new WriteCellData<>();
        //设置文本内容为String
        writeCellData.setType(CellDataTypeEnum.STRING);
        //填充文本内容
        writeCellData.setStringValue(content);
        //获取单元格样式类
        WriteCellStyle writeCellStyle = new WriteCellStyle();
        //设置当前单元格首航缩进距离  TODO short类型
        writeCellStyle.setIndent(indent);
        //将样式放入到单元格内
        writeCellData.setWriteCellStyle(writeCellStyle);


        //获取图片集合,一个单元格可以放置多张图片,我这里只放了一张,放了多张需要自行调整样式
        List<ImageData> imageDataList = new ArrayList<>();
        //放置图片的类,只能放置byte数组格式
        ImageData imageData = new ImageData();
        try {
            //将图片的https地址转为数组
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            URL url = new URL(imagePath);
            byte[] bytes = new byte[1024];
            InputStream inputStream = url.openStream();
            int len = 0;
            while ((len = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);
            }
            inputStream.close();

            //将数组放入
            imageData.setImage(outputStream.toByteArray());
            //设置图片类型为jpeg,其他类型可自行设置
            imageData.setImageType(ImageData.ImageType.PICTURE_TYPE_JPEG);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //设置图片相对于单元格的上下左右距离
        imageData.setTop(top);
        imageData.setRight(right);
        imageData.setBottom(bottom);
        imageData.setLeft(left);

        //相对于当前单元格往又移动一格,这里我全部设置为0了,作用就是占用当前单元格上下左右几个单元格,想了解自行测试
        imageData.setRelativeFirstRowIndex(0);
        imageData.setRelativeFirstColumnIndex(0);
        imageData.setRelativeLastRowIndex(0);
        imageData.setRelativeLastColumnIndex(0);

        //将图片信息放入集合
        imageDataList.add(imageData);

        //将图片集合放入单元格内
        writeCellData.setImageDataList(imageDataList);
        //返回当前单元格
        return writeCellData;
    }
}

 Then write the export method:

@RestController
public class TestController{

    @Resource
    private HttpServletResponse response;

    @GetMapping("/export")
    public viod exprot(){
        List<TextExcel> excelList = new ArrayList<>();

        TextExcel testExcel = new TextExcel();

        //以下为测试数据,根据实际情况自行修改
        //注:String.valueOf((char)10) 是 Excel 能识别的换行
        String imagePath = "http://i0.hdslb.com/bfs/article/cf439f5a994c6de6a0de8db1d8a1189a4ea1670c.jpg";
        String content = "第一行内容"+(String.valueOf((char)10))+"第二行内容第二行内容第二行内容第二行内容第二行内容第二行内容第二行内容";
        testExcel .setWriteCellData(WriteCellUtil.fillImage(imagePath,content,(short)8, 10, 10,15,330));

        excelList.add(testExcel );


        try {
            Export.setExcelRespProp(response, "测试Exce");
            EasyExcel.write(response.getOutputStream(), TestExcel.class)
                    .head(TestExcel.class)
                    .excelType(ExcelTypeEnum.XLSX)
                    .sheet("测试Exce")
                    .doWrite(excelList);
        } catch (IOException e) {
            throw new RRException("导出失败",407);
        }
    }
}

Guess you like

Origin blog.csdn.net/cccsssrrr/article/details/128137167
Recommended