Free Support Forum - aspose.com

Multiple Worksheet into single html

When I do an html conversion of the excel workbook containing multiple sheets, it generates a new folder with bunch of files such as .css file .mso and .htm file for each sheet etc. So is there a way (or a workaround) to produce a single html file in case of workbook with multiple sheets ? without any sub-folder?

I am using like…

Aspose.Cells.Workbook excel = new Aspose.Cells.Workbook(filename);
Aspose.Cells.HtmlSaveOptions options = new Aspose.Cells.HtmlSaveOptions();

@shreyb2,

Thanks for your query.

Well, Aspose.Cells follows MS Excel standards and specifications in rendering HTML files. Such folders and files are also created when you convert an MS Excel workbook to HTML in MS Excel manually, you may confirm this. You have two options and you may choose any the following approaches to accomplish the task:

  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 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) otherwise it will create folders. See the sample code for your reference:
    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.

If I am working on active sheet only then how can i convert other excel sheets…means how to loop active sheets from all available sheet?

@shreyb2,

See the sample code segment for your reference:
e.g
Sample code:

  var wb = new Workbook("e:\\test2\\Book1.xlsx");
         
   // Save worksheets to separate html files 
            for (int i = 0; i < wb.Worksheets.Count; i++)
            {
                wb.Worksheets.ActiveSheetIndex = i;

                var options = new HtmlSaveOptions
                {
                    ExportActiveWorksheetOnly = true,
                   ExportImagesAsBase64 = true
                };

                wb.Save(string.Format(@"e:\test2\out1sheet1{0}.html", i), options);
            }

Please note, you should have a valid license and you should set the license before using Aspose.Cells APIs least setting/activating sheets might not work.

My requirement is to regenerate excel back from those html’s…so If I get different html’s for each sheet how would I reconvert back into xlsx as a single file?

@shreyb2,

First of all, if you still need to have Excel file from HTML back, it is better you save the whole workbook (containing worksheets) to HTML file using your original code. So, you could re-load the HTML file and save it back to MS Excel file format.

Anyways, if you also create single HTML file based on each active sheet (as suggested in the previous reply), you can still revert HTML file back to Excel file. The procedure would be like following:

  1. Create HTML files based on each active sheet in the workbook.
  2. Convert each HTML file back to individual Excel file(s).
  3. Now merge or combine Excel files into final (single) workbook (Excel file) and save it.
    See the documents for your reference:
    https://docs.aspose.com/display/cellsnet/Merge+Files
    https://docs.aspose.com/display/cellsnet/Combine+Multiple+Workbooks+into+a+Single+Workbook

Thanks alot for your help.