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++.
- Convert an Entire Excel Worksheet to an Image in C++
- Convert a Specific Cell Range to an Image in C++
- Convert a Worksheet to an Image Without White Spaces in 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(); }
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(); }
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(); }
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.