Free Support Forum - aspose.com

EMBED PDF document inside a cell in an excel file

I want to embed 5 PDF documents inside an Excel file in specific cells.

For example:

I want to embed ex1.pdf into an excel file called sample.xls in worksheet "sheet1"

and cell "A1"

How can I do that?

Hi,


I am a representative of Aspose.Cells team and would like to help regarding embed PDF document inside a cell in an Excel file’s worksheet. Please see the following sample code for your requirements for your reference.

Sample code:

Workbook workbook = new Workbook(“e:\test2\sample.xls”);
//Get the first worksheet.
Worksheet sheet = workbook.Worksheets[0];
//Define a string variable to store the image path.
string ImageUrl = @“e:\test\PdfIcon.jpg”;
//Get the picture into the streams.
FileStream fs = File.OpenRead(ImageUrl);
//Define a byte array.
byte[] imageData = new Byte[fs.Length];
//Obtain the picture into the array of bytes from streams.
fs.Read(imageData, 0, imageData.Length);
//Close the stream.
fs.Close();
//Get an pdf file path in a variable.
string path = @“e:\test2\ex1.pdf”;
//Get the file into the streams.
fs = File.OpenRead(path);
//Define an array of bytes.
byte[] objectData = new Byte[fs.Length];
//Store the file from streams.
fs.Read(objectData, 0, objectData.Length);
//Close the stream.
fs.Close();
sheet.OleObjects.Add(0, 0, 40, 40, imageData);
//Set embedded ole object data.
sheet.OleObjects[0].ObjectData = objectData;
sheet.OleObjects[0].FileType = OleFileType.Pdf;
//Save the excel file
workbook.Save(@“e:\test2\MyFile.xls”);

For further reference, please see the document/topic:
http://www.aspose.com/docs/display/cellsnet/Managing+OLE+Objects

Thank you.

Hi there!

This code works well for PDF file.

How can I now embed ZIP files?

Thanks

Hi,

Well, I tested the case and inserted/embedded a .zip file inside an Excel sheet's cell, the object is embedded fine but when I click on it, it shows error message in MS Excel: e.g
"Cannot start the source application..."

Sample code:

Workbook workbook = new Workbook("e:\\test2\\Book1.xls");
//Get the first worksheet.
Worksheet sheet = workbook.Worksheets[0];
//Define a string variable to store the image path.
string ImageUrl = @"e:\test\IconZip.jpg";
//Get the picture into the streams.
FileStream fs = File.OpenRead(ImageUrl);
//Define a byte array.
byte[] imageData = new Byte[fs.Length];
//Obtain the picture into the array of bytes from streams.
fs.Read(imageData, 0, imageData.Length);
//Close the stream.
fs.Close();
//Get an pdf file path in a variable.
string path = @"e:\test2\Report90043.zip";
//Get the file into the streams.
fs = File.OpenRead(path);
//Define an array of bytes.
byte[] objectData = new Byte[fs.Length];
//Store the file from streams.
fs.Read(objectData, 0, objectData.Length);
//Close the stream.
fs.Close();
sheet.OleObjects.Add(0, 0, 40, 40, imageData);
//Set embedded ole object data.
sheet.OleObjects[0].ObjectData = objectData;
sheet.OleObjects[0].FileType = OleFileType.Unknown;
//Save the excel file
workbook.Save(@"e:\test2\outMyFile.xls");


I have logged a ticket with an id "CELLSNET-41378". We will look into it to figure it out soon.

Thank you.

Hi,


We have analysed your issue further. If you embed the zip file as ole object, please
set the OleObject.SourceFullName property, MS Excel will start the source
application with file’s extension to the file.


Here is the complete sample runnable code that works fine now, see the bold line.

Sample code:

Workbook workbook = new Workbook(“e:\test2\Book1.xls”);
//Get the first worksheet.
Worksheet sheet = workbook.Worksheets[0];
//Define a string variable to store the image path.
string ImageUrl = @“e:\test\school.jpg”;
//Get the picture into the streams.
FileStream fs = File.OpenRead(ImageUrl);
//Define a byte array.
byte[] imageData = new Byte[fs.Length];
//Obtain the picture into the array of bytes from streams.
fs.Read(imageData, 0, imageData.Length);
//Close the stream.
fs.Close();
//Get an pdf file path in a variable.
string path = @“e:\test2\Report90043.zip”;
//Get the file into the streams.
fs = File.OpenRead(path);
//Define an array of bytes.
byte[] objectData = new Byte[fs.Length];
//Store the file from streams.
fs.Read(objectData, 0, objectData.Length);
//Close the stream.
fs.Close();
sheet.OleObjects.Add(0, 0, 40, 40, imageData);
//Set embedded ole object data.
sheet.OleObjects[0].ObjectData = objectData;
sheet.OleObjects[0].FileType = OleFileType.Unknown;

sheet.OleObjects[0].SourceFullName = path;

//Save the excel file
workbook.Save(@“e:\test2\outMyFile.xls”);

So, it is not an issue with our product and I have closed your ticket now.

Thank you.