The following is the download address
http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22poi-ooxml%22
The code is directly on the code, and all that needs to be said is in the comments .
package com.excel; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import javax.imageio.ImageIO; //Excel 2003 import org.apache.poi.hssf.usermodel.HSSFClientAnchor; import org.apache.poi.hssf.usermodel.HSSFPatriarch; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; //Excel 2007 import org.apache.poi.xssf.usermodel.XSSFClientAnchor; import org.apache.poi.xssf.usermodel.XSSFDrawing; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class TestExcel { public static void main(String[] args) throws IOException { //To insert the picture in Excel String picPath="http://pic.nipic.com/2007-11-09/2007119122712983_2.jpg"; XSSFWorkbook xWb = new XSSFWorkbook (); TestExcel.insertImage (xWb, picPath); TestExcel.insertImage(picPath); FileOutputStream fileOut = null; // output to disk fileOut = new FileOutputStream("F:/1/excel2007x" + System.currentTimeMillis() + ".xlsx"); xWb.write (fileOut); fileOut.close(); System.out.println("well Done ! "); } //Operate the 2003 version of excel public static void insertImage(String picPath){ FileOutputStream fileOut = null; BufferedImage bufferImg = null; try { //You can also read a local existing Excel file version to correspond //HSSFWorkbook wb=new HSSFWorkbook(new FileInputStream(new File("F:/16.jpg"))); HSSFWorkbook wb=new HSSFWorkbook(); // create a workbook HSSFSheet sheet1 = wb.createSheet("test picture"); HSSFPatriarch patriarch = sheet1.createDrawingPatriarch(); for(int i=0;i<2;i++){ //new a URL object URL url = new URL(picPath); // open the link HttpURLConnection conn = (HttpURLConnection)url.openConnection(); //Set the request method to "GET" conn.setRequestMethod("GET"); //The timeout response time is 5 seconds conn.setConnectTimeout(5 * 1000); //Get image data through input stream InputStream inStream = conn.getInputStream(); // First put the read image into a ByteArrayOutputStream to generate a ByteArray ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); // The read image can be an input stream or a local File object bufferImg = ImageIO.read(inStream); //bufferImg = ImageIO.read(new File("F:/16.jpg")); ImageIO.write(bufferImg, "jpg", byteArrayOut); //The first four digits are the offsets in the two cells at the specified position, the maximum horizontal direction is 1023 and the vertical maximum is 255, the unit is unknown, it is probably a ratio HSSFClientAnchor anchor = new HSSFClientAnchor(500, 120, 1023, 255, (short) 0, 0+i*30, (short) 10, 20+i*20); anchor.setAnchorType(3); patriarch.createPicture(anchor, wb.addPicture(byteArrayOut .toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG)); byteArrayOut.close(); inStream.close(); conn.disconnect(); } fileOut = new FileOutputStream("F:/1/excel2003h" + System.currentTimeMillis() + ".xls"); // write to excel file wb.write(fileOut); fileOut.close(); } catch (IOException io) { io.printStackTrace(); System.out.println("erorr : " + io.getMessage()); } finally { if (fileOut != null) { try { fileOut.close(); } catch (IOException e) { e.printStackTrace (); } } } } //Operate excel version 2007 and above public static void insertImage(XSSFWorkbook wb,String picPath) { BufferedImage bufferImg = null; try { //new a URL object URL url = new URL(picPath); // open the link HttpURLConnection conn = (HttpURLConnection)url.openConnection(); //Set the request method to "GET" conn.setRequestMethod("GET"); //The timeout response time is 5 seconds conn.setConnectTimeout(5 * 1000); //Get image data through input stream InputStream inStream = conn.getInputStream(); // read image ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); //You can also use the file object to get what you need bufferImg = ImageIO.read(inStream); // bufferImg = ImageIO.read(new File("F:/16.jpg")); //If it is png, gif, set the corresponding value. I only tried jpg png is OK, if the set does not match the incoming picture, there will be color difference, you can try it yourself ImageIO.write(bufferImg, "jpg", byteArrayOut); // Create an excel Sheet, this is necessary, otherwise where is the picture inserted? XSSFSheet sheet=wb.createSheet("sheet0"); // Set the image position. This object corresponds to the HSSFPatriarch in version 2003. The usage is similar XSSFDrawing patriarch = sheet.createDrawingPatriarch(); //The offset is a bit disgusting. This unit is directly in tens of thousands, and if it is below 10,000, it is basically set. For unknown reasons, the HSSF of operation 2003 is normal proportion XSSFClientAnchor anchor = new XSSFClientAnchor(50*10000, 0, 100, 100, 2, 10, 12, 25); //This parameter is not very clear. There are three values of 0, 2, and 3. It seems to be related to the relative position of the picture and the cell. Setting it or not does not affect the position of the picture. //anchor.setAnchorType(2); //If it is png, gif, set the corresponding value patriarch .createPicture(anchor, wb.addPicture( byteArrayOut.toByteArray(), XSSFWorkbook.PICTURE_TYPE_JPEG)); byteArrayOut.close(); inStream.close(); conn.disconnect(); } catch (FileNotFoundException e) { System.out.println(e.getLocalizedMessage()); } catch (IOException e) { System.out.println(e.getLocalizedMessage()); } } }