windows 平臺下 xlnt 結合visual studio 2017 用c++操作excel

安裝cmake

cmake官網
下64位

Windows下載安裝xlnt

git clone https://github.com/tfussell/xlnt.git
cd xlnt
mkdir build
# git clone剛纔下載的xlnt源代碼位置 F:\CODE\CPPCODE\xlnt
# 如果下載的zip文件  源代碼位置 F:\CODE\CPPCODE\xlnt-master 
# cd xlnt-master 
# mkdir build
# 我直接下載的zip文件 所以我的xlnt源代碼地址是  F:\CODE\CPPCODE\xlnt-master 

Cmake編譯xlnt

1.打開Cmake(Gui)

在這裏插入圖片描述

 在where is souce codedir 放xlnt源代碼地址  ( F:\CODE\CPPCODE\xlnt-maste )

 在在where build the ... 放你 新建的 build 文件夾地址(F:/CODE/CPPCODE/xlnt-master/build)

2.點擊左下角 config

在這裏插入圖片描述

選中你的visual Studio 版本 下拉選中電腦版本的vs (默認是32位的 64位visual Stdio需要自己選擇)  
--> finish

在這裏插入圖片描述

3.設置完成之後點擊 Generate

4. 生成之後點擊openProject

5.在VS中點擊 本地調試器 ()等項目生成完成

PS: 項目在visual stdudio 本地調試器生成解決方案
會彈窗跳出警告 無法啓動程序 請無視
 在 xlnt-master\include中得到頭文件文件夾

 在 xlnt-master\build\source\Debug中得到動態鏈接庫 
動態鏈接庫中有  xlntd.dll  xlntd.lib 這是需要用到的東西 

如果沒有xlntd.dll就是沒編譯好 請重試

visual studio2017 項目中引入xlnt

配置xlnt 到你的項目

項目右鍵點擊它,然後選擇最下方的屬性按鈕,打開配置的窗口

  1. 配置頭文件路徑的配置
    配置屬性-VC++目錄-引用目錄
    填入 F:\CODE\CPPCODE\xlnt-master\include (根據你自己的源碼位置做出相應改變)
  2. 第三方庫庫文件路徑
    配置屬性-VC++目錄-庫目錄
    填入 F:\CODE\CPPCODE\xlnt-master\build\source\Debug
  3. 引用庫名稱的配置

配置屬性-連接器-依賴-附加依賴項
填入 xlntd.lib
4. 配置完之後 複製xlntd.dll放到你的項目文件夾中去 (將xlnt-master\include下的 xlnt文件夾也複製到項目中去)

以下是visual 2017 使用xlnt 生成excel的示例項目

環境 win10 64位 VS2017 64位 cmake 3.12.2 64位

1.生成空的項目

vs2017 文件->新建–>其他–>空項目–>ddxls(C:\Users\Administrator\source\repos\ddxls)

  • 添加demo.cpp文件
    • 項目–>右鍵–>添加新項 -->demo.cpp

2.給項目配置xlnt

- 項目右鍵點擊,然後選擇最下方的屬性按鈕,打開配置的窗口
- 1. 配置頭文件路徑的配置
    配置屬性-VC++目錄-引用目錄
    填入 xlnt-master\include
- 2. 第三方庫庫文件路徑
    配置屬性-VC++目錄-庫目錄
    填入 xlnt-master\build\source\Debug
- 3. 引用庫名稱的配置 
配置屬性-連接器-依賴-附加依賴項
填入 xlntd.lib

- 4.給項目添加動態鏈接庫
 複製xlntd.dll放到你的項目文件夾中去 (將xlnt-master\include下的 xlnt文件夾也複製到項目文件夾"C:\Users\Administrator\source\repos\ddxls\ddxls中去"
3.在demo.cpp添加代碼

#include <iostream>
#include "xlnt/xlnt.hpp"
#include <string.h>
#include <stdio.h>
#define _CRT_SECURE_NO_WARNINGS
#include <vector>
#include <string>

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;

}



4. 生成 --> 編譯 或者直接點擊 本地調試器完成調試 最後會生成

在這裏插入圖片描述

在這裏插入圖片描述

一個簡單的例子 xlnt讀寫excel 但是無法解決中文字符輸入的問題
解決xlnt讀寫中文寫的問題 需要將Unicode 轉 utf-8

#demoReadAndWriteExcel.cpp

#include <xlnt/xlnt.hpp>
#include <iostream>

void create() {
	xlnt::workbook wb;
	xlnt::worksheet ws = wb.active_sheet();
	ws.cell("A1").value(5);
	ws.cell("B2").value("string data");
	ws.cell("C3").formula("=RAND()");
	ws.merge_cells("C3:C4");
	ws.freeze_panes("B2");
	wb.save("sample.xlsx");
}

void read() {
	xlnt::workbook wb;
	wb.load("sample.xlsx");
	xlnt::worksheet ws = wb.active_sheet();

	std::cout << ws.cell("A1") << std::endl;
	std::cout << ws.cell("B2") << std::endl;
	std::cout << ws.cell("C3") << std::endl;
}

int main()
{
	create();
	read();
	return 0;
}

解決xlnt讀寫中文寫的問題

需要將Unicode 轉 utf-8
xlnt讀寫中文寫的問題

C++操作Excel,xlnt庫的使用

參考

CMake下載
win10下安裝cmake

cmake和win10結合

Visual Studio下C++第三方庫的配置方法總結

C++操作excel(利用github上的項目:xlnt)

C++操作Excel,xlnt庫的使用

cmake簡明教程-半小時從入門到精通
c++ example xlnt

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章