Hi,
I am trying to export an excel file with an image to an HTML, but with no success.
I created a new Excel file and added a picture from the path http://us.toluna.com/Images/Truste/seal_eusafe_en_45.jpg
I then tried to run the following code:
Workbook wb = new Workbook(new MemoryStream(@“E:/test.xls”, new LoadOptions(LoadFormat.Excel97To2003));
Worksheet ws = wb.Worksheets[0];
MemoryStream ms = new MemoryStream();
wb.Save(ms, SaveFormat.Html);
ms.Seek(0, SeekOrigin.Begin);
and write the ms MemoryStream to a file, but the HTML that is created have a bad image.
Looking at the source code, the path to the images are in the user local temp directory and not from the web.
Could you please help on this? its very urgent.
If this could not be done, is there a way to convert the excel file to html and to embed the images in it somehow?
I attache the xls file that I used and the result html (Please rename the out.zip to out.html since I couldn’t upload an HTML file to this post)
Thanks,
Amir
Hi Amir,
Try the following code, I have tested it with Aspose.Cells for .Net v 5.3.1.4. The Output HTML contains the image[Extract the attached archive to see the Output HTML].
Workbook wb = new Workbook(@“c:\test.xls”, new LoadOptions(LoadFormat.Excel97To2003));
Worksheet ws = wb.Worksheets[0];
HtmlSaveOptions saveOptionsCell = HtmlSaveOptions();
saveOptionsCell.SaveFormat = SaveFormat.Html;
MemoryStream ms = new MemoryStream();
ms.Seek(0, SeekOrigin.Begin);
wb.Save(ms, saveOptionsCell);
FileStream fs = File.OpenWrite(@“C:\test.html”);
ms.WriteTo(fs);
fs.Flush();
fs.Close();
Hi,
I have tested the file you attached and I have the same result as I did.
Please note that if you open your HTML file for edit, you will note the tag
![]()
which is a reference to the local image on your temp directory.
If you will empty this directory, you won't see the image.
My intent is to send the html to someone by mail, and that he would see the image correctlly, but since the reference is to the local temp direcoty, the other side that will open the HTML won't see the image....
Hi Amir,
An image in a HTML file will always be a reference to an external File. You need to couple the image and HTML file so they can be carried together. Imagine saving a web page for offline browsing, when you save HTML, a folder is created with all objects that are referenced in HTML tags.
Through Aspose.Cells for .Net, we can achieve the same effect by embedding the image in the worksheet as an OLE object.
Below is my source code, also attached is an archive of my output [containing test.html file and test_files folder]. Please verify them.
Workbook wb = new Workbook();
Worksheet ws = wb.Worksheets[0];
string ImageUrl = @“C:\seal_eusafe_en_45.jpg”;
FileStream ifs = File.OpenRead(ImageUrl);
byte[] imageData = new Byte[ifs.Length];
ifs.Read(imageData, 0, imageData.Length);
ifs.Close();
ifs = File.OpenRead(@“c:\test.xls”);
byte[] objectData = new Byte[ifs.Length];
ifs.Read(objectData, 0, objectData.Length);
ifs.Close();
int objectIndex = ws.OleObjects.Add(4, 3, 20, 60, imageData);
ws.OleObjects[objectIndex].ObjectData = objectData;
HtmlSaveOptions saveOptionsCell = new HtmlSaveOptions(SaveFormat.Html);
wb.Save(@“C:\test.html”, saveOptionsCell);