How to compile the xlnt by cmake and deploy it to Visual Studio 2017 in windows
CMAKE
Download cmake from the link https://cmake.org/download/.
Note: You can choose download .zip and use it directly.
xlnt
Download xlnt from the link https://github.com/tfussell/xlnt.
Compile xlnt
Open the cmake-gui.exe, please refer to figure 1.
Step 1: Click the Browse Source… button, select the path where you xlnt code is.
Step 2: Click the Browse Build… button, Select the path where you plan to build.
Step 3: Click the Configure button, please refer to figure 2.
Note 1: Optional platform for generator: You can input ‘x64’ to build x64 lib and dll. If empty, the input is “Win32″[1]. You can refer to this link( https://cmake.org/cmake/help/latest/generator/Visual%20Studio%2015%202017.html ) for more detail.
Optional toolset to use: Empty is OK.
Select the “Use default native compilers“.(The default one)
Step 4: Click the Generate button.
Step 5: Click the Open Project button. Then it will open the Visual Studio, you need to select Debug, Win32[2] and click the Local Windows debugger . And refer to the Figure 3.
Now, If anything goes well, we get include file, the path is “{You system path}\xlnt-master\include”, lib file & dll file, the path is “{You system path }\build\source\Debug”.
Configure Visual Studio 2017
Step1: Create a new empty project.
Step2: Right click the project name, open the attribute page.
Step3: VC++ catalogue->including catalogue: Input the path for include file. “{You system path}\xlnt-master\include”.
Step4: VC++ catalogue->library catalogue: Input the path for lib file. “{You system path }\build\source\Debug
Please refer to the Figure 4
Step 5: Linker->Input->Additional dependency. Please input the path for lib file. ” “{You system path }\build\source\Debug\xlntd.lib”. Please refer to the Figure 5.
Step 6: Copy the .dll file to the directory which the .exe file exists.
Now. We just add a cpp file and the sample code to test it.
#include <iostream> #include <vector> #include <string> #include <xlnt/xlnt.hpp> int main() { //Creating a 2 dimensional vector which we will write values to std::vector< std::vector<std::string> > wholeWorksheet; //Looping through each row (100 rows as per the second argument in the for loop) for (int outer = 0; outer < 100; outer++) { //Creating a fresh vector for a fresh row std::vector<std::string> singleRow; //Looping through each of the columns (100 as per the second argument in the for loop) in this particular row for (int inner = 0; inner < 100; inner++) { //Adding a single value in each cell of the row std::string val = std::to_string(inner + 1); singleRow.push_back(val); } //Adding the single row to the 2 dimensional vector wholeWorksheet.push_back(singleRow); std::clog << "Writing to row " << outer << " in the vector " << std::endl; } //Writing to the spread sheet //Creating the output workbook std::clog << "Creating workbook" << std::endl; xlnt::workbook wbOut; //Setting the destination output file name std::string dest_filename = "output.xlsx"; //Creating the output worksheet xlnt::worksheet wsOut = wbOut.active_sheet(); //Giving the output worksheet a title/name wsOut.title("data"); //We will now be looping through the 2 dimensional vector which we created above //In this case we have two iterators one for the outer loop (row) and one for the inner loop (column) std::clog << "Looping through vector and writing to spread sheet" << std::endl; for (int fOut = 0; fOut < wholeWorksheet.size(); fOut++) { std::clog << "Row" << fOut << std::endl; for (int fIn = 0; fIn < wholeWorksheet.at(fOut).size(); fIn++) { //Take notice of the difference between accessing the vector and accessing the work sheet //As you may already know Excel spread sheets start at row 1 and column 1 (not row 0 and column 0 like you would expect from a C++ vector) //In short the xlnt cell reference starts at column 1 row 1 (hence the + 1s below) and the vector reference starts at row 0 and column 0 wsOut.cell(xlnt::cell_reference(fIn + 1, fOut + 1)).value(wholeWorksheet.at(fOut).at(fIn)); //Further clarification to avoid confusion //Cell reference arguments are (column number, row number); e.g. cell_reference(fIn + 1, fOut + 1) //Vector arguments are (row number, column number); e.g. wholeWorksheet.at(fOut).at(fIn) } } std::clog << "Finished writing spread sheet" << std::endl; wbOut.save(dest_filename); return 0; }
The code will create a output.xlsx file if the configuration is correct.
Note: [1][2], platform in attribute page, solution platform could be Win32, x86 or x64, x64. But they must be consistent.
Enjoy it.
If you have any questions, please feel free to make a comment.