Export Worksheet CSS Separately in Output HTML with C++

Possible Usage Scenarios

Aspose.Cells provides the feature to export worksheet CSS separately when you convert your Excel file to HTML. Please use HtmlSaveOptions.GetExportWorksheetCSSSeparately() property for this purpose and set it to true while saving the Excel file to HTML format.

Export Worksheet CSS Separately in Output HTML

The following sample code creates an Excel file, adds some text in cell B5 in Red color and then saves it in HTML format using HtmlSaveOptions.GetExportWorksheetCSSSeparately() property. Please see the output HTML generated by the code for reference. You will find stylesheet.css inside it as an outcome of the sample code.

Sample Code

#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;

int main()
{
    Aspose::Cells::Startup();

    // Create workbook object
    Workbook wb;

    // Access first worksheet
    Worksheet ws = wb.GetWorksheets().Get(0);

    // Access cell B5 and put value inside it
    Cell cell = ws.GetCells().Get(u"B5");
    cell.PutValue(u"This is some text.");

    // Set the style of the cell - font color is Red
    Style st = cell.GetStyle();
    st.GetFont().SetColor(Color::Red());
    cell.SetStyle(st);

    // Specify html save options - export worksheet css separately
    HtmlSaveOptions opts;
    opts.SetExportWorksheetCSSSeparately(true);

    // Save the workbook in html
    wb.Save(u"outputExportWorksheetCSSSeparately.html", opts);

    std::cout << "Workbook saved successfully!" << std::endl;

    Aspose::Cells::Cleanup();
}

Export Single Sheet Workbook to HTML

When a workbook with multiple sheets is converted to HTML using Aspose.Cells, it creates an HTML file along with a folder containing CSS and multiple HTML files. When this HTML file is opened in the browser, the tabs are visible. The same behavior is required for a workbook with a single worksheet when it is converted to HTML. Earlier, no separate folder was created for single sheet workbooks, and only an HTML file was created. Such an HTML file does not show a tab when opened in the browser. MS Excel creates a proper folder and HTML for a single sheet as well, and hence the same behavior is implemented using Aspose.Cells APIs. The sample file can be downloaded from the following link for use in the sample code below:

sampleSingleSheet.xlsx

Sample Code

#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;

int main()
{
    Aspose::Cells::Startup();

    // Source directory path
    U16String srcDir(u"..\\Data\\01_SourceDirectory\\");

    // Output directory path
    U16String outDir(u"..\\Data\\02_OutputDirectory\\");

    // Path of input Excel file
    U16String inputFilePath = srcDir + u"sampleSingleSheet.xlsx";

    // Path of output HTML file
    U16String outputFilePath = outDir + u"outputSampleSingleSheet.htm";

    // Create workbook
    Workbook workbook(inputFilePath);

    // Specify HTML save options
    HtmlSaveOptions options;

    // Set optional settings
    options.SetEncoding(EncodingType::UTF8);
    options.SetExportImagesAsBase64(true);
    options.SetExportGridLines(true);
    options.SetExportSimilarBorderStyle(true);
    options.SetExportBogusRowData(true);
    options.SetExcludeUnusedStyles(true);
    options.SetExportHiddenWorksheet(true);

    // Save the workbook in HTML format with specified HTML save options
    workbook.Save(outputFilePath, options);

    std::cout << "Workbook saved successfully in HTML format!" << std::endl;

    Aspose::Cells::Cleanup();
}