Embedding HTML files in Excel

Hi,

We need to embed html file in excel dynamically using Aspose.cells. We tried the code mentioned at url http://www.aspose.com/demos/.net-components/aspose.cells/csharp/quick-start/drawing-objects/inserting-ole-object.aspx

But it is given for embed xls into xls.We need to embed html in xlsx file.

Please guide us on same.

Hi,

Thanks for your posting and using Aspose.Cells for .NET.

I have looked into this issue and found, it is quite a complex thing. Because when you embed html file as ole object, it does not get opened by Ms-Excel and it throws error.

However, I have devised a workaround with which you can embed any html files as Ole object and Ms-Excel will be able to open it.

First, we will copy a source html embedded ole object from a source workbook into destination workbook, then we will change its object data property.

Please see the code below. I have attached the following input and output files generated by the code.

1 - source.xlsx (this file contains the html ole object which we created manually using Ms-Excel)
2 - target.html (we want to embed this html as an ole object)
3 - output.xlsx (this is the final output xlsx file in which we have embedded target.html as an ole object).

C#


string filePath = @“F:\Shak-Data-RW\Downloads\source.xlsx”;


//First copy ole object from source workbook into destination workbook

Workbook srcwb = new Workbook(filePath);


//Access the source ole from first worksheet

OleObject srcole = srcwb.Worksheets[0].OleObjects[0];


//Create your new workbook

Workbook workbook = new Workbook();


//Access its first worksheet

Worksheet worksheet = workbook.Worksheets[0];


//Add the source ole object in destination worksheet and access it as destination ole object

OleObject htmlObj = worksheet.Shapes.AddCopy(srcole, srcole.UpperLeftRow, srcole.Top, srcole.UpperLeftColumn, srcole.Left) as OleObject;


//Now change the object data to your desired html

htmlObj.ObjectData = File.ReadAllBytes(“target.htm”);


//Save the workbook

workbook.Save(“output.xlsx”);

Hi,


Another way is to link the HTML file with the Ole Object in the Excel sheet. See the sample code below:

//Instantiate a new Workbook.
Workbook workbook = new Workbook();
//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 excel file path in a variable.
string path = @“d:\myhtml.html”;
//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();
//Add an Ole object into the worksheet with the image
//shown in MS Excel.
sheet.OleObjects.Add(14, 3, 200, 220, imageData);
//Set embedded ole object data.
sheet.OleObjects[0].ObjectData = objectData;
sheet.OleObjects[0].FileType = OleFileType.Unknown;
sheet.OleObjects[0].SetNativeSourceFullName(@“D:\myhtml.html”);
//Save the excel file
workbook.Save(@“e:\test2\testinHtmlOle.xls”);