使用EasyExcel在同一单元格内添加图片和文字,并作格式排版

 格式要求如下:

 需求:在一个单元格内,左侧是图片,右侧是文字,文字和图片之间有间隙。

思路:在一个单元格内添加图片并设置图片位置,然后添加文字,设置文字位置。

经查看EasyExcel文档发现:
添加图片默认是填充完当前单元格,但可设置上下左右的边距
添加文字默认是常规从左往右,无缩进;但选择文字居左显示可设置首行缩进

然后知道这么多就可以进行操作了;

先在实体类需要操作的行上设置格式:

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;
}

然后对这个单元格进行操作:

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;
    }
}

 然后编写导出方法即可:

@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);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/cccsssrrr/article/details/128137167
今日推荐