這個庫是libxl,可以去官網下載,也可以用破解(僅供學習,不可用於商業,如用官方產生法律糾紛,概不負責)
我這邊是動態庫libxl.dll 和libxl.lib 還有include
因爲是動態庫所以不管vs20xx還是debug/release/win32/x64都可以用,
具體查看我的下載管理
《libxl綜合文件》
或者百度網盤:
https://pan.baidu.com/s/1Aff6yNwy8t4oM4f4sIi4rQ
提取碼:1eg9
二維碼
下面是我寫的一個簡單案例,基本包括了表格建立,寫表格,保存等操作,可以作爲參考:
// ConsoleApplication1.cpp : 定義控制檯應用程序的入口點。
//
#include "stdafx.h"
#include "libxl.h"
#include <string>
#include <stdio.h>
#include <time.h>
#include <IBookT.h>
#include <iostream>
#include <vector>
#include <list>
using namespace std;
using namespace libxl;
int main()
{
list<vector<string>> excel_content;
vector<string> vec_header;
vec_header.push_back("序號");
vec_header.push_back("信息");
vec_header.push_back("時間");
vec_header.push_back("設備");
vec_header.push_back("位置");
vec_header.push_back("備註");
vec_header.push_back("備註時間");
vec_header.push_back("處理人");
vector<int> col_width_vec;
col_width_vec.push_back(25);
col_width_vec.push_back(25);
col_width_vec.push_back(25);
col_width_vec.push_back(25);
col_width_vec.push_back(25);
col_width_vec.push_back(30);
col_width_vec.push_back(25);
col_width_vec.push_back(25);
excel_content.push_back(vec_header);
for (int i = 0; i < 10; ++i)//10隨便寫的一個
{
vector<string> vec;
vec.push_back("something");
vec.push_back("other some things.");
excel_content.push_back(vec);
}
//----------------------
int t = time(NULL);
string basename = "exel";
string xls_name = ".\\"+basename+".xlsx";
Book* xls_book = xlCreateXMLBook();
if (xls_book == nullptr) {
cout<<"create xls file error"<<endl;
return 1;
}
xls_book->setKey("Halil Kural", "windows-2723210a07c4e90162b26966a8jcdboe");
Sheet* sheet = xls_book->addSheet("Sheet1");//添加一個工作表
if (sheet == nullptr)
{
cout<<"create sheet in excel fail error"<<endl;
return 1;
}
Format* cell_format = xls_book->addFormat();
cell_format->setAlignH(AlignH::ALIGNH_CENTER);
cell_format->setAlignV(AlignV::ALIGNV_CENTER);
cell_format->setBorder(BORDERSTYLE_DASHDOT);
std::vector<int> column_width_vec= col_width_vec;
for (int i = 0; i < column_width_vec.size(); ++i)
{
sheet->setCol(i, i, column_width_vec[i]);
}
int i = 0;
int j = 0;
std::list<std::vector<std::string>> content_list= excel_content;
auto it = content_list.begin();
for (; it != content_list.end(); ++it, ++i)
{
vector<string> vec = *it;
for (j = 0; j < vec.size(); ++j)
{
sheet->writeStr(i, j, vec[j].c_str(), cell_format);
}
}
if (!xls_book->save(xls_name.c_str()))
{
cout<<"fail to save excel"<<endl;
return -1;
}
return 0;
}