Today, I have been asked to develop a code which will insert an image into the excel sheet in Java.
Earlier we have developed code which will populate the data in MS excel using POI.
At first attempt, I have tried to do it with POI 2.5, but went in vain.
After my (re)search in the internet, I found a code which is using POI3.1-beta2.jar.
http://www.ibiblio.org/maven/org.apache.poi/jars/- to download the POI3.1-beta2.jar
http://poi.apache.org/apidocs/index.html - API to refer.
http://poi.apache.org/apidocs/index.html - API to refer.
Code:
import org.apache.poi.hssf.usermodel.*;
import java.io.*;
public class InsertImageExcel {
public static void main(String[] args) {
int col=1,row=1;
HSSFWorkbook wb=new HSSFWorkbook();
HSSFSheet testsheet=wb.createSheet("test");
System.out.println("The work book is created");
try {
FileOutputStream fos=new FileOutputStream("C:/sample.xls");
System.out.println("File sample.xls is created");
FileInputStream fis=new FileInputStream("C:/home.jpg");
ByteArrayOutputStream img_bytes=new ByteArrayOutputStream();
int b;
while((b=fis.read())!=-1)
img_bytes.write(b);
fis.close();
int colnew = 10; // last col
int rownew = 25; // last row
// This will embed the picture from (1,1) cell to (10,10) cell of excel sheet.
HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,0,0,(short)col,row,(short)colnew,rownew);
int index=wb.addPicture(img_bytes.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG);
HSSFSheet sheet=wb.getSheet("test");
HSSFPatriarch patriarch=sheet.createDrawingPatriarch();
patriarch.createPicture(anchor,index);
anchor.setAnchorType(2);
wb.write(fos);
System.out.println("Writing data to the xls file");
fos.close();
System.out.println("File closed");
} catch(IOException ioe) {
ioe.printStackTrace();
}
}
}
import java.io.*;
public class InsertImageExcel {
public static void main(String[] args) {
int col=1,row=1;
HSSFWorkbook wb=new HSSFWorkbook();
HSSFSheet testsheet=wb.createSheet("test");
System.out.println("The work book is created");
try {
FileOutputStream fos=new FileOutputStream("C:/sample.xls");
System.out.println("File sample.xls is created");
FileInputStream fis=new FileInputStream("C:/home.jpg");
ByteArrayOutputStream img_bytes=new ByteArrayOutputStream();
int b;
while((b=fis.read())!=-1)
img_bytes.write(b);
fis.close();
int colnew = 10; // last col
int rownew = 25; // last row
// This will embed the picture from (1,1) cell to (10,10) cell of excel sheet.
HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,0,0,(short)col,row,(short)colnew,rownew);
int index=wb.addPicture(img_bytes.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG);
HSSFSheet sheet=wb.getSheet("test");
HSSFPatriarch patriarch=sheet.createDrawingPatriarch();
patriarch.createPicture(anchor,index);
anchor.setAnchorType(2);
wb.write(fos);
System.out.println("Writing data to the xls file");
fos.close();
System.out.println("File closed");
} catch(IOException ioe) {
ioe.printStackTrace();
}
}
}
1 comment:
Try this Excel Java component it can insert image in your excel file based on cell reference and you don;t have to do the coding yourself and if you still want code for that purpose you can email their support team for the code.
Post a Comment