We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Issue with adding image as background to excel file

Hi Team,


I am trying to add an image (.png file) to excel file (all sheets).
But looks like that image is added to each cell. I want that image to be added only once
on each sheet of excel file. Below is the code I used. Can you please help me in this regard.

package com.test;

import java.io.File;
import java.io.FileInputStream;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
public class AddWaterMarkAsImageToExcelFile {
public static void main(String[] args)throws Exception {
Workbook workbook = new Workbook(“d:\images\Test.xlsx”);
int sheets=workbook.getWorksheets().getCount();
System.out.println(“no of sheets------>”+sheets);
File file = new File(“d:\images\Demo.png”);
byte[] imageData = new byte[(int)file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(imageData);
Worksheet sheet = null;
for(int count=0;count<sheets;count++){
sheet=workbook.getWorksheets().get(count);
//Set the background image for the sheet.
sheet.setBackground(imageData);
}
//Save the excel file
workbook.save(“d:\images\Demo.xlsx”);
System.out.println(“end”);
}

}

I am attaching the excel files for your reference.

Hi,


Thanks for providing us template file and sample code.

Well, this is MS Excel’s behavior which does set the background image like that. I think you cannot set image as background only once in MS Excel. If you have other thought, kindly create a sample file with your desired background image set in MS Excel manually, save the file and post us here, we will check how to do it via Aspose.Cells APIs.

Thank you.