C++: Convert Excel to HTML

Converting Excel to HTML makes it easier to embed your spreadsheet data on a website and also ensures that other people can view the document online in their browsers without opening Excel. In this article, you will learn how to convert Excel to HTML using Spire.XLS for C++.

Install Spire.XLS for C++

There are two ways to integrate Spire.XLS for C++ into your application. One way is to install it through NuGet, and the other way is to download the package from our website and copy the libraries into your program. Installation via NuGet is simpler and more recommended. You can find more details by visiting the following link.

Integrate Spire.XLS for C++ in a C++ Application

Convert an Entire Excel Workbook to HTML in C++

The Workbook->SaveToHtml() method provided by Spire.XLS for C++ allows you to convert the whole workbook to HTML. The following are the detailed steps.

  • Create a Workbook object.
  • Load an Excel document using Workbook->LoadFromFile() method.
  • Save the entire Excel workbook as an HTML file using Workbook->SaveToHtml() method.
  • C++
#include "Spire.Xls.o.h";

using namespace Spire::Xls;

int main() {

	//Specify input file path and name
	std::wstring data_path = L"Data\\";
	std::wstring inputFile = data_path + L"input.xlsx";

	//Specify output file path and name
	std::wstring outputPath = L"Output\\";
	std::wstring outputFile = outputPath + L"ToHtml.html";
	
	//Create a Workbook object
	Workbook* workbook = new Workbook();

	//Load an Excel document from disk
	workbook->LoadFromFile(inputFile.c_str());

	//Save the whole Excel workbook to Html
	workbook->SaveToHtml(outputFile.c_str());
	workbook->Dispose();
}

C++: Convert Excel to HTML

Convert a Specified Worksheet to HTML with Images Embedded in C++

If you want the images in a worksheet to be embedded into the HTML code while conversion, you can set the parameter of HTMLOptions->SetImageEmbedded() method to true. The following are the detailed steps.

  • Create a Workbook object.
  • Load an Excel document using Workbook->LoadFromFile() method.
  • Get a specific worksheet using Workbook->GetWorksheets()->Get() method.
  • Create an HTMLOptions instance and enable image embedding using HTMLOptions->SetImageEmbedded(true) method.
  • Save the worksheet to HTML with image embedded using Worksheet->SaveToHtml(LPCWSTR_S fileName, HTMLOptions* saveOption) method.
  • C++
#include "Spire.Xls.o.h";

using namespace Spire::Xls;

int main() {

	//Specify input file path and name
	std::wstring data_path = L"Data\\";
	std::wstring inputFile = data_path + L"sample.xlsx";

	//Specify output file path and name
	std::wstring outputPath = L"Output\\";
	std::wstring outputFile = outputPath + L"ExcelToHtml.html";
	
	//Create a Workbook object
	Workbook* workbook = new Workbook();

	//Load an Excel document from disk
	workbook->LoadFromFile(inputFile.c_str());

	//Get the first worksheet
	Worksheet* sheet = workbook->GetWorksheets()->Get(0);

	//Set embedded image as true
	HTMLOptions* options = new HTMLOptions();
	options->SetImageEmbedded(true);

	//Save the worksheet to HTML
	sheet->SaveToHtml(outputFile.c_str(), options);
	workbook->Dispose();
} 

C++: Convert Excel to HTML

Convert a Specified Worksheet to HTML Stream in C++

Spire.XLS for C++ also allows you to convert a worksheet to HTML stream using Worksheet->SaveToHtml(Stream* stream, HTMLOptions* saveOption) method. The following are the detailed steps.

  • Create a Workbook object.
  • Load an Excel document using Workbook->LoadFromFile() method.
  • Get a specific worksheet using Workbook->GetWorksheets()->Get() method.
  • Create an HTMLOptions instance and enable image embedding using HTMLOptions->SetImageEmbedded(true) method.
  • Create a stream and save the worksheet to HTML stream with image embedded using Worksheet->SaveToHtml(Stream* stream, HTMLOptions* saveOption) method.
  • C++
#include "Spire.Xls.o.h";

using namespace Spire::Xls;

int main() {

	//Specify input file path and name
	std::wstring data_path = L"Data\\";
	std::wstring inputFile = data_path + L"sample.xlsx";

	//Specify output file path and name
	std::wstring outputPath = L"Output\\";
	std::wstring outputFile = outputPath + L"ToHtmlStream.html";
	
	//Create a Workbook object
	Workbook* workbook = new Workbook();

	//Load an Excel document from disk
	workbook->LoadFromFile(inputFile.c_str());

	//Get the first worksheet
	Worksheet* sheet = workbook->GetWorksheets()->Get(0);

	//Set embedded image as true
	HTMLOptions* options = new HTMLOptions();
	options->SetImageEmbedded(true);

	//Create a stream
	Stream* stream = new Stream();

	//Save worksheet to html stream
	sheet->SaveToHtml(stream, options);
	workbook->Dispose();

	stream->Save(outputFile.c_str());
}

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.