Friday, 05 May 2023 05:20

C++: Convert Excel to Images

Excel is a popular spreadsheet software widely used for data analysis, financial management, budgeting, etc. However, when you need to embed Excel files into other files or share them with others, Excel format may have compatibility issues, and in such a case converting Excel to image is an alternative option. In this article, you will learn how to convert an Excel worksheet or a specific cell range to an image in C++ 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 Worksheet to an Image in C++

Spire.XLS for C++ offers the Worksheet->ToImage() method to convert a specific worksheet to an image, and then you can save the image in PNG, JPG or BMP format using Image->Save() method. The following are the detailed steps.

  • Create a Workbook object.
  • Load a sample Excel document using Workbook->LoadFromFile() method.
  • Get a specified worksheet using Workbook->GetWorksheets()->Get() method.
  • Convert the worksheet to an image using Worksheet->ToImage() method.
  • Save the image as a PNG file using Image->Save() method.
  • C++
#include "Spire.Xls.o.h";

using namespace Spire::Xls;
using namespace std;

int main() {

	//Specify the input and output file paths
	wstring inputFile = L"Data\\Planner.xlsx";
	wstring outputFile = L"Output\\SheetToImage";

	//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);

	//Convert the worksheet to an image
	Image* image = sheet->ToImage(sheet->GetFirstRow(), sheet->GetFirstColumn(), sheet->GetLastRow(), sheet->GetLastColumn());
	
	//Save the image as a PNG file
	wstring fileName = outputFile + L".png";
	image->Save(fileName.c_str());
	workbook->Dispose();
}

C++: Convert Excel to Images

Convert a Specific Cell Range to an Image in C++

Spire.XLS for C++ also allows you to use the Worksheet->ToImage(int firstRow, int firstColumn, int lastRow, int lastColumn) method to convert a specified cell range to an image. The following are the steps convert several cell ranges to different image formats.

  • Create a Workbook object.
  • Load a sample Excel document using Workbook->LoadFromFile() method.
  • Get a specified worksheet using Workbook->GetWorksheets()->Get() method.
  • Specify a cell range and save it to a specified image format using Worksheet->ToImage()->Save(LPCWSTR_S filename, ImageFormat* format) method.
  • C++
#include "Spire.Xls.o.h";

using namespace Spire::Xls;
using namespace std;

int main() {

	//Specify input file path and name
	wstring inputFile = L"Data\\Planner.xlsx";

	//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);

	//Specify cell ranges and save them to certain image formats
	sheet->ToImage(3, 1, 11, 4)->Save(L"ToImage\\SpecificCellsToImage.png", ImageFormat::GetPng());
	sheet->ToImage(3, 6, 11, 9)->Save(L"ToImage\\SpecificCellsToImage.jpg", ImageFormat::GetJpeg());
	sheet->ToImage(13, 6, 22, 9)->Save(L"ToImage\\SpecificCellsToImage.bmp", ImageFormat::GetBmp());
	workbook->Dispose();
}

C++: Convert Excel to Images

Convert a Worksheet to an Image Without White Spaces in C++

When converting a worksheet directly to an image, there are white spaces around the converted image. If you want to remove these white spaces, you can set the left, right, top and bottom margins of the worksheet to zero while conversion. The following are the detailed steps.

  • Create a Workbook object.
  • Load a sample Excel document using Workbook->LoadFromFile() method.
  • Get a specified worksheet using Workbook->GetWorksheets()->Get() method.
  • Return a page setup object using Worksheet->GetPageSetup() method, and then set the left, right, top and bottom margins of the worksheet using the methods of PageSetup class.
  • Save the worksheet as an image using Worksheet->ToImage()->Save() method.
  • C++
#include "Spire.Xls.o.h";

using namespace Spire::Xls;
using namespace std;

int main() {

	//Specify output file path and name
	wstring inputFile = L"Data\\Planner.xlsx";
	wstring outputFile = L"Output\\ToImageWithoutWhiteSpace.png";

	//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 the margin as 0 to remove the white space around the image
	sheet->GetPageSetup()->SetLeftMargin(0);
	sheet->GetPageSetup()->SetBottomMargin(0);
	sheet->GetPageSetup()->SetTopMargin(0);
	sheet->GetPageSetup()->SetRightMargin(0);

	//Save the worksheet as an image
	Image* image = sheet->ToImage(sheet->GetFirstRow(), sheet->GetFirstColumn(), sheet->GetLastRow(), sheet->GetLastColumn());
	image->Save(outputFile.c_str());
	workbook->Dispose();
}

C++: Convert Excel to Images

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.

Published in Conversion
Tuesday, 11 April 2023 01:01

C++: Convert Excel to XPS

XPS stands for XML Paper Specification, which is a fixed layout file format developed by Microsoft. Unlike some formats that allow for dynamic content and layout changes, XPS is built to preserve the layout of documents and maintain their visual appearance across different devices. XPS files can be easily viewed using the Microsoft XPS Viewer, which is pre-installed on many Windows computers.

There are several benefits to converting Excel files to XPS format. Firstly, it provides an efficient way to share Excel data with others who lack access to Microsoft Excel or other spreadsheet software. Secondly, it ensures that the formatting of the original Excel file remains intact, making it easy to read and understand. Another advantage of converting Excel to XPS is accessibility: XPS files are built to be accessible, meaning they can be read by screen readers and assistive technology. In this article, we will explain how to convert an Excel file to XPS format in C++ 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 Excel to XPS in C++

Converting an Excel file to XPS format is very straightforward using Spire.XLS for C++. You just need to load the Excel file using the Workbook->LoadFromFile(LPCWSTR_S fileName) method and then call the Workbook->SaveToFile(LPCWSTR_S fileName, FileFormat::XPS) method to save it as XPS. The detailed steps are as follows:

  • Specify the input and output file paths.
  • Initialize an instance of the Workbook class.
  • Load the Excel file specified by the input file path using Workbook->LoadFromFile() method.
  • Save the Excel file to an XPS file with the specified file path using Workbook->SaveToFile(LPCWSTR_S fileName, FileFormat::XPS) method.
  • C++
#include "Spire.Xls.o.h";

using namespace Spire::Xls;
using namespace std;

int main()
{
	//Specify the input and output file paths
	wstring inputFile = L"Sample.xlsx";
	wstring outputFile =  L"ExcelToXPS.xps";

	//Initialize an instance of the Workbook class
	Workbook* workbook = new Workbook();

	//Load the Excel file specified by the input file path
	workbook->LoadFromFile(inputFile.c_str());

	//Save the Excel file to an XPS file with the specified file path
	workbook->SaveToFile(outputFile.c_str(), Spire::Xls::FileFormat::XPS);
	workbook->Dispose();
	delete workbook;
}

C++: Convert Excel to XPS

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.

Published in Conversion

Excel and CSV (Comma-Separated Values) are two commonly used file formats for managing and storing tabular data in various industries. Excel organizes data in rows and columns and provides users with a wide range of advanced features for data manipulation, analysis and visualization, whereas CSV stores data in a plain text format that is lightweight and highly compatible with various applications. Converting Excel files to CSV format can be very helpful when users need to exchange or import Excel data in different programs. Conversely, users who require more advanced data analysis features, such as creating charts or applying formulas, may find it beneficial to convert CSV files to Excel format. In this article, we will demonstrate how to convert Excel to CSV or CSV to Excel in C++ 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 Excel to CSV in C++

Spire.XLS for C++ offers the XlsWorksheet->SaveToFile (LPCWSTR_S fileName, LPCWSTR_S separator, Spire::Common::Encoding* encoding) method to convert a worksheet in an Excel file to CSV. The detailed steps are as follows:

  • Initialize an instance of the Workbook class.
  • Load an Excel file using Workbook->LoadFromFile() method.
  • Get a specific worksheet in the workbook by its index using Workbook->GetWorksheets()->Get(int index) method.
  • Save the worksheet to a CSV file using XlsWorksheet->SaveToFile (LPCWSTR_S fileName, LPCWSTR_S separator, Spire::Common::Encoding* encoding) method.
  • C++
#include "Spire.Xls.o.h";

using namespace Spire::Xls;

int main()
{
	//Initialize an instance of the Workbook class
	Workbook* workbook = new Workbook();
	//Load an Excel file
	workbook->LoadFromFile(L"Input.xlsx");

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

	//Save the worksheet to a CSV file
	sheet->SaveToFile(L"ExcelToCsv.csv", L",", Encoding::GetUTF8());
	workbook->Dispose();
	delete workbook;
}

C++: Convert Excel to CSV or CSV to Excel

Convert Visible Data in Excel to CSV in C++

When converting an Excel worksheet to CSV using the above code, all data, including both visible and hidden data, will be saved to CSV. If you only want to save visible data in the worksheet to CSV, you can use the XlsWorksheet->SaveToFile (LPCWSTR_S fileName, LPCWSTR_S separator, bool retainHiddenData) method. The detailed steps are as follows:

  • Initialize an instance of the Workbook class.
  • Load an Excel file using Workbook->LoadFromFile() method.
  • Get a specific worksheet in the workbook by its index using Workbook->GetWorksheets()->Get(int index) method.
  • Save visible data in the worksheet to a CSV file using XlsWorksheet->SaveToFile (LPCWSTR_S fileName, LPCWSTR_S separator, bool retainHiddenData) method.
  • C++
#include "Spire.Xls.o.h";

using namespace Spire::Xls;

int main()
{
	//Initialize an instance of the Workbook class
	Workbook* workbook = new Workbook();
	//Load an Excel file
	workbook->LoadFromFile(L"Input.xlsx");

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

	//Save visible data in the worksheet to a CSV file
	sheet->SaveToFile(L"ExcelToCsv1.csv", L",", false);
	workbook->Dispose();
	delete workbook;
}

C++: Convert Excel to CSV or CSV to Excel

Convert CSV to Excel in C++

To convert a CSV file to Excel, you need to load the CSV file using Workbook->LoadFromFile(LPCWSTR_S fileName, LPCWSTR_S separator) method, then use the Workbook->SaveToFile (LPCWSTR_S fileName, ExcelVersion version) method to save it to an Excel file. The detailed steps are as follows:

  • Initialize an instance of the Workbook class.
  • Load a CSV file with separator using Workbook->LoadFromFile(LPCWSTR_S fileName,LPCWSTR_S separator) method.
  • Get a specific worksheet in the file by its index using Workbook->GetWorksheets()->Get(int index) method.
  • Set ignore error option to ignore errors when saving numbers in a specific cell range as text using Worksheet->GetRange(LPCWSTR_S name)->SetIgnoreErrorOptions(IgnoreErrorType::NumberAsText) method.
  • Auto-fit column widths using Worksheet->GetAllocatedRange()->AutoFitColumns() method.
  • Save the CSV file to an Excel file using Workbook->SaveToFile (LPCWSTR_S fileName, ExcelVersion version) method.
  • C++
#include "Spire.Xls.o.h";

using namespace Spire::Xls;

int main()
{
	//Initialize an instance of the Workbook class
	Workbook* workbook = new Workbook();
	//Load a CSV file with separator
	workbook->LoadFromFile(L"ExcelToCSV.csv", L",");

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

	//Set ignore error option to ignore errors when saving numbers in a specific cell range as text
	sheet->GetRange(L"C2:C11")->SetIgnoreErrorOptions(IgnoreErrorType::NumberAsText);

	//Auto-fit column widths
	sheet->GetAllocatedRange()->AutoFitColumns();

	//Save the file to an Excel XLSX file
	workbook->SaveToFile(L"CsvToExcel.xlsx", ExcelVersion::Version2013);
	workbook->Dispose();
	delete workbook;
}

C++: Convert Excel to CSV or CSV to Excel

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.

Published in Conversion
Tuesday, 07 March 2023 00:56

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.

Published in Conversion

Excel is a wonderful tool for the creation and management of spreadsheets. However, when it comes to sharing these files with others, Excel may not deliver the best results. As soon as you have finalized your report, you can convert it to PDF, which keeps the formatting of your spreadsheets and allows them to be displayed perfectly on a variety of devices. Furthermore, PDFs are secure and can be encrypted to prevent unauthorized changes to the content.

In this article, you will learn how to convert an Excel workbook to PDF and how to convert an Excel worksheet to PDF in C++ 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 Excel Workbook to a PDF Document in C++

Spire.XLS for C++ offers the Workbook->SaveToFile(LPCWSTR_S fileName, FileFormat fileFormat) method, enabling users to convert an entire workbook to another format file, like PDF, HTML, CSV and XPS. Besides, it offers the ConverterSetting class to specify the convert options, such as whether to automatically adjust the height and width of the cells during conversion. The following are the steps to convert an Excel workbook to PDF using it.

  • Create a Workbook object.
  • Load a sample Excel document using Workbook->LoadFromFile() method.
  • Make worksheets to fit to page when converting using Workbook->GetConverterSetting()->SetSheetFitToPage() method.
  • Convert the workbook to PDF using Workbook->SaveToFile() method.
  • C++
#include "Spire.Xls.o.h";

using namespace Spire::Xls;
using namespace std;

int main() {
	
	//Specify input file path
	wstring inputFilePath =  L"C:\\Users\\Administrator\\Desktop\\sample.xlsx";

	//Specify output file path and name
	wstring outputPath = L"Output\\";
	wstring outputFile = outputPath + L"ToPDF.pdf";

	//Create a Workbook object
	Workbook* workbook = new Workbook();

	//Load the source Excel file
	workbook->LoadFromFile(inputFilePath.c_str());

	//Set worksheets to fit to page when converting
	workbook->GetConverterSetting()->SetSheetFitToPage(true);

	//Save to PDF
	workbook->SaveToFile(outputFile.c_str(), FileFormat::PDF);
	workbook->Dispose();
}

C++: Convert Excel Workbooks or Worksheets to PDF

Convert a Specific Worksheet to a PDF Document in C++

To export a specific worksheet as a PDF, you must first use the Workbook->GetWorksheets()->Get(index) method to obtain the worksheet, and then use the Worksheet->SaveToPdf(LPCWSTR_S fileName) method to save it. The following are the detailed steps.

  • Create a Workbook object.
  • Load a sample Excel document using Workbook->LoadFromFile() method.
  • Make worksheets to fit to page when converting using Workbook->GetConverterSetting()->SetSheetFitToPage() method.
  • Get a specific worksheet using Workbook->GetWorksheets()->Get() method.
  • Convert the worksheet to PDF using Worksheet->SaveToPdf() method.
  • C++
#include "Spire.Xls.o.h";

using namespace Spire::Xls;
using namespace std;

int main() {

	//Specify input file path
	wstring inputFilePath = L"C:\\Users\\Administrator\\Desktop\\sample.xlsx";

	//Specify output file path and name
	wstring outputPath = L"Output\\";
	wstring outputFile = outputPath + L"ToPDF.pdf";

	//Create a Workbook object
	Workbook* workbook = new Workbook();

	//Load the source Excel file
	workbook->LoadFromFile(inputFilePath.c_str());

	//Set worksheets to fit to page when converting
	workbook->GetConverterSetting()->SetSheetFitToPage(true);

	//Get a specific worksheet
	Worksheet* sheet = workbook->GetWorksheets()->Get(0);

	//Save it to PDF
	sheet->SaveToPdf(outputFile.c_str());
	workbook->Dispose();
}

C++: Convert Excel Workbooks or Worksheets to PDF

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.

Published in Conversion

Coupon Code Copied!

Christmas Sale

Celebrate the season with exclusive savings

Save 10% Sitewide

Use Code:

View Campaign Details