public static void main(String arg[]) throws IOException{
printExcel();
}
/**
*
* 3.7去除所有图片再插入,3.10直接插入图片,原来图片还在
* @throws IOException
*/
public static void printExcel() throws IOException{
InputStream is = new FileInputStream("C:/Users/25709/Desktop/jqbmb.xls");
HSSFWorkbook wb= new HSSFWorkbook(is);
HSSFRow row=null;
HSSFCell cell=null;
HSSFSheet sheet=wb.getSheetAt(0);
String imgPath = "D:/二维码生成/Relieved_QRCode.png";
BufferedImage bufferImg = null;
BufferedImage bufferImg1 = null;
//先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
bufferImg = ImageIO.read(new File(imgPath));
ImageIO.write(bufferImg, "png", byteArrayOut);
ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream();
bufferImg1 = ImageIO.read(new File("C:/Users/25709/Desktop/log.png"));
ImageIO.write(bufferImg1, "png", byteArrayOut1);
//画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
//anchor主要用于设置图片的属性 , (short) 6, 12, (short)6, 15是图片所占单元格(对点正方形区域),(50=x1, 80=y1, 800=x2, 200=y2)
//对点正方形区域内(x1,y1),(x2,y2)是图片的坐标
HSSFClientAnchor anchor = new HSSFClientAnchor(50, 80, 800, 200,(short) 6, 12, (short)6, 15);
anchor.setAnchorType(3);
HSSFClientAnchor anchor1 = new HSSFClientAnchor(300, 150, 600, 200,(short) 0, 0, (short) 1, 2);
anchor1.setAnchorType(3);
//插入图片
patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
patriarch.createPicture(anchor1, wb.addPicture(byteArrayOut1.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
FileOutputStream os;
os = new FileOutputStream("C:/Users/25709/Desktop/jqbmb.xls");
wb.write(os);
os.close();
}
printExcel();
}
/**
*
* 3.7去除所有图片再插入,3.10直接插入图片,原来图片还在
* @throws IOException
*/
public static void printExcel() throws IOException{
InputStream is = new FileInputStream("C:/Users/25709/Desktop/jqbmb.xls");
HSSFWorkbook wb= new HSSFWorkbook(is);
HSSFRow row=null;
HSSFCell cell=null;
HSSFSheet sheet=wb.getSheetAt(0);
String imgPath = "D:/二维码生成/Relieved_QRCode.png";
BufferedImage bufferImg = null;
BufferedImage bufferImg1 = null;
//先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
bufferImg = ImageIO.read(new File(imgPath));
ImageIO.write(bufferImg, "png", byteArrayOut);
ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream();
bufferImg1 = ImageIO.read(new File("C:/Users/25709/Desktop/log.png"));
ImageIO.write(bufferImg1, "png", byteArrayOut1);
//画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
//anchor主要用于设置图片的属性 , (short) 6, 12, (short)6, 15是图片所占单元格(对点正方形区域),(50=x1, 80=y1, 800=x2, 200=y2)
//对点正方形区域内(x1,y1),(x2,y2)是图片的坐标
HSSFClientAnchor anchor = new HSSFClientAnchor(50, 80, 800, 200,(short) 6, 12, (short)6, 15);
anchor.setAnchorType(3);
HSSFClientAnchor anchor1 = new HSSFClientAnchor(300, 150, 600, 200,(short) 0, 0, (short) 1, 2);
anchor1.setAnchorType(3);
//插入图片
patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
patriarch.createPicture(anchor1, wb.addPicture(byteArrayOut1.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
FileOutputStream os;
os = new FileOutputStream("C:/Users/25709/Desktop/jqbmb.xls");
wb.write(os);
os.close();
}