Hello everyone,
I need to add an image to a .xlsx file.
An image in .png format with a size of 150x150 pixels
I tried several methods, but without success:
Method 1. According to the published documentation here
Moreover, using the link at the bottom of the page in the Download Running Code section - I downloaded an example from github, which should be a working version
https://github.com/aspose-cells/Aspose.Cells-for-.NET/releases/download/Aspose.Cells_Vs_NPOI_HWPF_and_XWPF_v1.2/Add.Image.in.Worksheet.zip
But after unzipping this example, it turned out that no image was added. I am attaching the file generated as a result of executing the downloaded example in Method1_ResultFile.xlsx file inside attached zip archive
Method 2. Change the cell size so that it can be inserted into the image
I tried to fix the code myself so that the cell size matches the image size (150x150) but also without success.
const string imagePath =
@"C:\Users\User\Documents\logo.png";
const string outputFilePath =
@"C:\Users\User\Documents\Method2_ResultFile.xlsx";
Workbook workbook = new Workbook();
WorksheetCollection worksheets = workbook.Worksheets;
Worksheet worksheet = worksheets.Add("My Worksheet");
worksheet.Cells["C2"].Value = "Image";
worksheet.Cells.SetRowHeight(3, 150);
worksheet.Cells.SetColumnWidth(2, 150);
int index = worksheet.Pictures.Add(3, 2, 3, 2, imagePath);
Picture pic = worksheet.Pictures[index];
pic.Placement = PlacementType.FreeFloating;
workbook.Save(outputFilePath);
I am attaching the file generated as a result of executing Method 2 as Method2_ResultFile.xlsx file inside attached zip archive
Method 3. Via SetHeaderPicture
According to the code posted here
I was unable to add an image to the file
private void PrintQrImageToExcelFile(string documentFilePath)
{
const string imagePath =
@"C:\Users\User\Documents\logo.png";
if (!File.Exists(documentFilePath))
throw new FileNotFoundException("File not found", documentFilePath);
FileStream inFile;
byte[] binaryData;
inFile = new System.IO.FileStream(imagePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
binaryData = new Byte[inFile.Length];
long bytesRead = inFile.Read(binaryData, 0, (int)inFile.Length);
var workbook = new Workbook(documentFilePath);
foreach (Worksheet worksheet in workbook.Worksheets)
{
PageSetup pageSetup = worksheet.PageSetup;
pageSetup.SetHeaderPicture(1, binaryData);
pageSetup.SetHeader(1, "&G");
pageSetup.SetHeader(2, "&A");
}
const string outputFilePath = @"C:\Users\User\Documents\Method3_ResultFile.xlsx";
workbook.Save(outputFilePath);
inFile.Close();
}
Only this method works fine - but once you open file you are not able to see the image until you select “File → Print” menu to see the file in print preview mode
I am attaching the file generated as a result of executing Method 3 as Method3_ResultFile.xlsx file inside attached zip archive
Method 4. Via SetPicture
Below is the code:
private void PrintQrImageToExcelFileMethod4(string documentFilePath)
{
const string imagePath =
@"C:\Users\User\Documents\logo.png";
const string imageScript = "&G";
if (!File.Exists(documentFilePath))
throw new FileNotFoundException("File not found", documentFilePath);
if (string.IsNullOrWhiteSpace(imagePath) && !File.Exists(imagePath))
throw new FileNotFoundException("QR image path not found", nameof(imagePath));
var qrImageBytes = File.ReadAllBytes(imagePath);
Program.SetLicenseExcel();
var workbook = new Workbook(documentFilePath);
foreach (Worksheet worksheet in workbook.Worksheets)
{
worksheet.PageSetup.SetPicture(
isFirst: true,
isEven: true,
isHeader: true,
section: 0,
qrImageBytes);
worksheet.PageSetup.SetFirstPageHeader(section: 0, imageScript);
}
const string outputFilePath = @"C:\Users\User\Documents\Method4_ResultFile.xlsx";
workbook.Save(outputFilePath);
}
I am attaching the file generated as a result of executing Method 4 as Method4_ResultFile.xlsx file inside attached zip archive
Input file will be found as ExcelSample_TEST.xlsx inside attached zip archive
Please provide a working examples with explanations and comments, as a result of which I would be able to add an image in .png format with a size of 150x150 pixels to an existing .xlsx document and the image could be seen once file was open
Thank you
Aspose.zip (283.9 KB)