Mac OS 10.14.4
java version “1.8.0_181”
Java™ SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot™ 64-Bit Server VM (build 25.181-b13, mixed mode)
Aspose: aspose-cells-19.7
Read Images From orignal Excel File, then New excel file and add images to sheet
The images from orignal files are translucent, but after added to new file ,they become opaque
excels.zip (283.1 KB)
ImageInfo.java
public class ImageInfo implements Serializable {
private int index;
private String name;
private boolean hidden;
private int zPosition;
private byte[] data;
private float[] position;
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public boolean isHidden() {
return hidden;
}
public void setHidden(boolean hidden) {
this.hidden = hidden;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public byte[] getData() {
return data;
}
public void setData(byte[] data) {
this.data = data;
}
public float[] getPosition() {
return position;
}
public void setPosition(float[] position) {
this.position = position;
}
public int getzPosition() {
return zPosition;
}
public void setzPosition(int zPosition) {
this.zPosition = zPosition;
}
}
Read File
Workbook workbook = new Workbook(****FilePath****);
WorksheetCollection sheetCollection = workbook.getWorksheets();
Worksheet worksheet = sheetCollection.get(0);
ArrayList<ImageInfo> imageInfos = new ArrayList<>();
PictureCollection pictureCollection = worksheet.getPictures();
int pictureCount = pictureCollection.getCount();
for (int picIndex = 0; picIndex < pictureCount; picIndex++) {
Picture picture = pictureCollection.get(picIndex);
ImageInfo imageInfo = new ImageInfo();
imageInfo.setIndex(picIndex);
imageInfo.setData(picture.getData());
imageInfo.setName(picture.getName());
float left = picture.getLeftToCorner();
float top = picture.getTopToCorner();
float width = picture.getWidth();
float height = picture.getHeight();
imageInfo.setPosition(new float[]{top, left, width, height});
imageInfo.setHidden(picture.isHidden());
imageInfo.setzPosition(picture.getZOrderPosition());
imageInfos.add(imageInfo);
}
Export File
Write Images to New Excel
Workbook workbook = new Workbook();
WorksheetCollection worksheets = workbook.getWorksheets();
Worksheet worksheet = worksheets.get(0);
for (ImageInfo imageInfo : imageInfos) {
int index = worksheet.getPictures().add(1, 1, new ByteArrayInputStream(imageInfo.getData()));
Picture picture = worksheet.getPictures().get(index);
if (imageInfo.getName()!=null){
picture.setName(imageInfo.getName());
}
picture.setTopToCorner((int) imageInfo.getPosition()[0]);
picture.setLeftToCorner((int) imageInfo.getPosition()[1]);
picture.setWidth((int) imageInfo.getPosition()[2]);
picture.setHeight((int) imageInfo.getPosition()[3]);
picture.setHidden(imageInfo.isHidden());
picture.setZOrderPosition(imageInfo.getzPosition());
}