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

Free Support Forum - aspose.com

Convert Excel to HTML with custom image source


Currently our server is stateless. We are going to stream to converted excel in the HTTP response.
How can I configure the image source? The images will be pushed into different server https://image.xyz.com




Thanks for using Aspose APIs.

When you export your HTML, then use Inline images. Otherwise, your images will be lost. Because, images will be exported to a separate folder, so you will have only html not its images.

To get inline images, please use HtmlSaveOptions.ExportImagesAsBase64 property and set it true.

Please see the following sample code, its sample Excel file, its output HTML file and its output Pdf file. The download links of all files are given below.

The code first exports the sample Excel file as HTML with inline images, then it reloads the exported HTML and again save it in Pdf format. Both HTML and PDF matches exactly with Sample Excel file.


//Load your sample workbook
Workbook wb = new Workbook("sample.xlsx");

//Specify HtmlSaveOptions
//Export image as bytes (base 64) as inline images
//Export active worksheet only
HtmlSaveOptions opts = new HtmlSaveOptions();
opts.ExportImagesAsBase64 = true;
opts.ExportActiveWorksheetOnly = true;

//Save the workbook in HTML format with above HtmlSaveOptions
wb.Save(dirPath + "out.html", opts);

//Load the HTML again in workbook
wb = new Workbook("out.html");

//Specify PdfSaveOptions
//We want entire worksheet in a single page
PdfSaveOptions pdfopts = new PdfSaveOptions();
pdfopts.OnePagePerSheet = true;

//Save the workbook in pdf format with above PdfSaveOptions
wb.Save("out.pdf", pdfopts);

Download Links:
Sample Excel File.zip (228.9 KB)
Output HTML Generated From Sample Excel File.zip (369.0 KB)
Output Pdf Generated From Output HTML.zip (338.7 KB)

Hi Shakeel,

Thank you for the response.
I am aware with this approach. The problem with this approach is browser is not able to cache if the same image are repeated within same excel sheets.
Do you have any other alternative?



Please implement the IStreamProvider interface for your needs. Please see this Sample Code, it implements the IStreamProvider interface. Please also download the following files used inside the code or generated by the code.


class OurStreamProvider : IStreamProvider
	public MemoryStream[] mstrs;
	int idx=0;

	public void CloseStream(StreamProviderOptions options)
		//throw new NotImplementedException();

	public void InitStream(StreamProviderOptions options)
		options.Stream = mstrs[idx++];

static void Run()
	Workbook wb = new Workbook("Sample.xlsx");

	OurStreamProvider shap = new OurStreamProvider();
	shap.mstrs = new MemoryStream[] { new MemoryStream(), new MemoryStream() };

	HtmlSaveOptions opts = new HtmlSaveOptions();
	opts.StreamProvider = shap;

	wb.Save(dirPath + "out.html", opts);

	var o1 = shap.mstrs[0].ToArray();
	var o2 = shap.mstrs[1].ToArray();

	File.WriteAllBytes("img.jpg", o1);
	File.WriteAllBytes("img.png", o2);

Download LInks:
Sample Excel File.zip (766.0 KB)
Output Files Created By IStreamProvider.zip (1.5 MB)
Code that implements IStreamProvider interface.zip (505 Bytes)

Thanks for the response. I will try it out.