windows平台下xlnt结合visualstudio2017⽤c++操作excel 安装cmake
Windows下载安装xlnt
Cmake编译xlnt
1.打开Cmake(Gui)
2.点击左下⾓ config
3.设置完成之后点击 Generate
4. ⽣成之后点击openProject
5.在VS中点击本地调试器()等项⽬⽣成完成
visual studio2017 项⽬中引⼊xlnt
配置xlnt 到你的项⽬
以下是visual 2017 使⽤xlnt ⽣成excel的⽰例项⽬
1.⽣成空的项⽬
2.给项⽬配置xlnt
3.在demo.cpp添加代码
4. ⽣成 --> 编译或者直接点击本地调试器完成调试最后会⽣成
解决xlnt读写中⽂写的问题
参考
安装cmake
cmake官⽹
下64位
Windows下载安装xlnt
git clone github/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
ntdwholeWorksheet.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
//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
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论