easyExcel 指定行列导出图片
关于导出图片参考
指定位置导出图片主要是通过设置writeCellData设置行列位置,writeCellData.setRowIndex(12); writeCellData.setColumnIndex(0); 这样可以指定导出的图片位置是在第13行第一列的单元格。
private static WriteCellData handleImage(String path) { WriteCellData<Void> writeCellData = new WriteCellData<>(); // 设置样式 WriteCellStyle writeCellStyle = new WriteCellStyle(); //向左对齐 writeCellStyle.setVerticalAlignment(VerticalAlignment.TOP); writeCellData.setWriteCellStyle(writeCellStyle); writeCellData.setType(CellDataTypeEnum.STRING); // 设置需要填充该字段的坐标 row=12,index=0 writeCellData.setRowIndex(12); writeCellData.setColumnIndex(0); if(StringUtils.isEmpty(path)){ return writeCellData; } File file = new File(path); if (!file.exists()) { writeCellData.setStringValue("图片不存在"); return writeCellData; } try { // 定义图片数据 ImageData imageData = new ImageData(); imageData.setImage(FileUtils.fileToByte(file));// 设置图片字节 imageData.setImageType(ImageData.ImageType.PICTURE_TYPE_EMF);//设置图片后缀类型 // 设置图片 上下左右边距 imageData.setTop(5); imageData.setRight(5); imageData.setBottom(5); imageData.setLeft(5); imageData.setRelativeLastColumnIndex(6); // 最后设置图片数据 writeCellData.setImageDataList(Arrays.asList(imageData)); } catch (Exception e) { e.printStackTrace(); } return writeCellData; }
最后
//直接填充WriteCellData excelWriter.fill("image", handleImage(path)), writeSheet);
如果最后还导出不了图片,请参考上面的官网文档。
下一篇:
从零开始——Dev-C++调试方法