Possibility of saving xlsx to html in single html file

Hi,

I have a question regarding aspose cells for java. When saving excel file as html using HtmlSaveOptions, besides the main html file, new html file is created for every sheet in workbook. Is there any way to have only one html file which will hold all the content when saving xlsx file as html through aspose ?

Regards,
Milorad

@Milorad,

Thanks for your query.

Aspose.Cells follows Ms Excel standards and specifications in rendering Excel to HTML file format, so by default it will create folder containing the resource files against worksheets in the workbook (MS Excel does the same thing). But you may still choose any of the following options to cope with it for your needs:

  1. Use MHtml file format as an output file format from Excel workbooks. It will generate single output file.

  2. Try to export every worksheet (in the workbook) to single HTML and then group these individual HTMLs to one (final) HTML by yourselves via e.g some tag control or your own code. In a loop, you may set active for each sheet and then render separate HTML file (based on every worksheet) via Aspose.Cells APIs. Please note, when exporting every worksheet to separate HTML, you would need to export image as base64 format (you will use HtmlSaveOptions class here to set the relevant attribute(s)) otherwise it will create folders.

Hope, this helps a bit.

Thanks for your answer.

For option 2) : how can I save (export) every worksheet to single html? Is there any method for that, or you are reffering to these existing htmls which appear in resource folder?

@Milorad,

See the sample (.NET) code for your reference, you may refer to it and write your code in Java accordingly. You may loop through each sheet in the workbook:
e.g
Sample code:

 //Load your sample workbook
 Workbook wb = new Workbook(“e:\test2\Book1.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;//You may activate one sheet at a time.

 //Save the workbook in HTML format with above HtmlSaveOptions
 wb.Save(“e:\test2\out1.html”, opts);

Hope, this helps a bit.

I tried it, that’s what I needed. Thank you very much!

@Milorad,

You are welcome.

Hi,

when I loop through each sheet in the workbook in order to save every sheet as single html, is there any way to have folder where images from all sheets will be placed, instead of setting ‘exportImagesAsBase64’ option? I saw that word’s and ppt’s htmlsaveoptions have setImagesFolder() method for setting folder where images will be placed, but there is no such option in htmlsaveoptions in aspose.cells.

Regards

@Milorad,

Well, you may use IStreamProvider to implement it for your requirements. See the sample (.NET) code for your reference:
e.g
Sample code:

string filePath = @"E:\Test\Files\SampleFile\";
Workbook wb = new Workbook(filePath + "a.xlsx");

Aspose.Cells.HtmlSaveOptions htmlSaveOptions = new HtmlSaveOptions(SaveFormat.Html);
htmlSaveOptions.StreamProvider = new ExportStreamProvider(@"E:\Test\Files\SampleFile\images\");
htmlSaveOptions.ExportActiveWorksheetOnly = true;

int sheetCount = wb.Worksheets.Count;
for (int i = 0; i < sheetCount; i++)
{
    wb.Worksheets.ActiveSheetIndex = i;
    string outputFile = "sheet00" + (i+1) + "_out.html";
    wb.Save(filePath + outputFile, htmlSaveOptions);
}


 public class ExportStreamProvider : IStreamProvider
{
    
    private string outputDir;
    
    public ExportStreamProvider(string dir)
    {
        outputDir = dir;
    }
    public void InitStream(StreamProviderOptions options)
    {
        string path = outputDir + Path.GetFileName(options.DefaultPath);
        options.CustomPath = path;
        Directory.CreateDirectory(Path.GetDirectoryName(path));
        options.Stream = File.Create(path);
    }

    public void CloseStream(StreamProviderOptions options)
    {
        if (options != null && options.Stream != null)
        {
            options.Stream.Close();
        }
    }
}

Hope, this helps a bit.