天天搞大数据大屏弄得坷坷坎坎,现在终于让我回来了。哦对顺便给了个活( ̄▽ ̄)"----让我在原有的功能上加个图片导出。之前有用若依的组件写过图片导出。我记得那个比较简单,在要导出的类中的图片字段上加上注解,工具类是会有自动处理so easy~~。不过这次是用easyexcel导出的文件,之前没接触过有点麻爪不知道从哪里开始。嗯~不急 咱先看官方文档,点进去了之后一看 万幸还真有图片导出,不过现在easyexcel的jar包空空如也(╯▔皿▔)╯,不知道具体原因是啥。
果然关键的时候还是只能靠自己(ง •_•)ง,在进行了若干次尝试之后终于得到了个办法:先写入数据然后再读取已经写入数据的文件,再将图片放在对应位置。虽然感觉麻烦了点,不过先不管啦能解决就先将就用!
下面这个是插入图片的方法,里面有说明。我就不多解释了,哪里有问题欢迎大佬们前来指正(。・∀・)ノ
/**
*
* @param imagePath 图的片绝对路径
* @param filePath 需要插入图片的excel文件绝对路径
* @param sheets excel的第几个sheet 0开始0是第一个sheet
* @param rows excel中图片位置的起始行 0开始0是第一个行
* @param column excel中图片位置的起始列 0开始0是第一个列
* @throws IOException
*/
private void exportImage( String imagePath,String filePath,int sheets,int rows,int columns) throws IOException {
// 地址为绝对路径
if (!imagePath.isEmpty()) {
File imageFile = new File(imagePath);
if (imageFile.exists()) {
System.out.println("图片路径: " + imagePath); // 打印图片路径
} else {
System.out.println("图片文件不存在: " + imagePath); // 打印文件不存在的消息
return;
}
} else {
System.out.println("图片路径为空: " + imagePath); // 打印路径为空的消息
return;
}
try (
FileInputStream inputStream = new FileInputStream(new File(filePath));
// 转换成流
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
){
XSSFWorkbook workbook1 = new XSSFWorkbook(inputStream);
//getSheetAt(int index) 方法用于获取一个特定索引位置上的工作表。索引是从 0 开始的,因此 getSheetAt(0) 获取的是第一个工作表。
XSSFSheet sheet1 = workbook1.getSheetAt(sheets);
//BufferedImage支持多种图片格式(如 JPEG、GIF、BMP 等)
BufferedImage bufferImg = ImageIO.read(new File(imagePath));
//读取图片并转为png格式写入--下边插入进excel时需要用到
ImageIO.write(bufferImg, "png", byteArrayOut);
XSSFDrawing patriarch =sheet1.createDrawingPatriarch();
if (patriarch == null) {
throw new IllegalStateException("无法创建绘图对象");
}
/**
* new XSSFClientAnchor(0, 0, 0, 0, (short) 11, 2, (short) 12, 6);
* 前4个参数是偏移量,设置为0即可 (short)rows为起始行 (short)rows+1结束行, 前到后不到 column + 6为起始列 column + 7 为结束列
* 设置好这些参数就能在指定位置插入图片
*/
XSSFClientAnchor anchor1 = new XSSFClientAnchor(0, 0, 0, 0, (short)columns, (short)rows + 6, (short)columns+1, (short)rows + 7);
anchor1.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE);
int pictureIdx = workbook1.addPicture(byteArrayOut.toByteArray(), XSSFWorkbook.PICTURE_TYPE_PNG);
patriarch.createPicture(anchor1, pictureIdx);
// if (pictureIdx >= 0) {
// patriarch.createPicture(anchor1, pictureIdx);
// } else {
// System.out.println("图片添加失败,索引无效");
// }
// ----------------------旧方法---------------------------
// byte[] bytes = IOUtils.toByteArray(imageStream);
// int pictureIdx = workbook1.addPicture(bytes, XSSFWorkbook.PICTURE_TYPE_PNG);
// imageStream.close();
// Drawing<?> drawing = sheet.createDrawingPatriarch();
// ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, 38, j + 6, 39, j + 7); // 设置图片位置,38是.zi列的列索引
// patriarch.createPicture(anchor, pictureIdx);
// pict.resize(1, 1); // 调整图片大小
// ----------------------旧方法---------------------------
FileOutputStream outputStream = new FileOutputStream(filePath);
workbook1.write(outputStream);
outputStream.flush();
} catch (IOException e) {
System.err.println("插入图片时发生错误: " + e.getMessage());
e.printStackTrace();
}
// System.out.println("图片插入成功: " + imagePath); // 打印插入成功的消息
}
再放个图说明一下用法,我这里是要在一个excel文件中同时插入多个sheet,并且每个sheet页中都需要插入多个图片,所以就做了两套循环来弄,大家可以根据实际情况自行修改。(请不要在意颜色)