java借助poi将图片导出到excel中;
byte[] bytes1 = null;
ByteArrayInputStream bais1 = null;
BufferedImage bi1=null;
BASE64Decoder decoder = new BASE64Decoder();
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
bytes1 = decoder.decodeBuffer(vo.getFile1().replace(" ","+"));//转换正规的图片空格转为+
bais1 = new ByteArrayInputStream(bytes1);
bi1 =ImageIO.read(bais1);
ImageIO.write(bi1, "png", byteArrayOut);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("tddcqst"); //sheet的名字
//画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
//导出多张图片的话就要写多个 多个调整到指定位置比较蛋疼
(0, 0, 1023,255,(short) 1, 1, (short)7, 14);
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 1023,255,(short) 1, 1, (short)7, 14);
anchor.setAnchorType(6);
patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
FileOutputStream fileOut = new FileOutputStream(path); //path导出到那个地址
// 写入excel文件
wb.write(fileOut);
public class ExcelImageTest {
public static void main(String[] args) {
FileOutputStream fileOut = null;
BufferedImage bufferImg = null;
//先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
try {
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
bufferImg = ImageIO.read(new File("C:/pic/地方电厂购电量比较图1559118923845.png"));
// bufferImg = ImageIO.read(new File("D:/地方电厂购电量比较图1559193092537.png"));
ImageIO.write(bufferImg, "jpg", byteArrayOut);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("test picture");
//画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
//anchor主要用于设置图片的属性
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 1023,255,(short) 1, 1, (short)7, 14);
anchor.setAnchorType(3);
HSSFClientAnchor anchor2 = new HSSFClientAnchor(0, 0, 1023,255,(short) 1, 17, (short)7, 31);
HSSFClientAnchor anchor3 = new HSSFClientAnchor(0, 0, 1023,255,(short) 1, 34, (short)7, 48);
HSSFClientAnchor anchor4 = new HSSFClientAnchor(0, 0, 1023,255,(short) 9, 1, (short)15, 14);
HSSFClientAnchor anchor5 = new HSSFClientAnchor(0, 0, 1023,255,(short) 9, 17, (short)15, 31);
HSSFClientAnchor anchor6 = new HSSFClientAnchor(0, 0, 1023,255,(short) 9, 34, (short)15, 48);
//插入图片
patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
patriarch.createPicture(anchor2, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
patriarch.createPicture(anchor3, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
patriarch.createPicture(anchor4, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
patriarch.createPicture(anchor5, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
patriarch.createPicture(anchor6, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
fileOut = new FileOutputStream("D:/测试Excel179.xls");
// 写入excel文件
wb.write(fileOut);
System.out.println("----Excle文件已生成------");
} catch (Exception e) {
e.printStackTrace();
}finally{
if(fileOut != null){
try {
fileOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 1023,255,(short) 1, 1, (short)7, 14);
1,1 中间两位参数X=1,Y=1
7,14最后两位参数 X=7,Y=14