功能
- 表格不存在则创建后再进行操作
- 创建sheet添加新的工作表
- 在sheet中增加数据
- 设置单元格样式
相关配置
- 下载地址:libxl
- 选择 LibXL for Linux 4.2.0 i386 x64 armhf aarch64
安装配置
- 1,使用 tar zxvf 文件名.tar.gz 进行文件解压
- 2,创建 /usr/local/libxl/lib64目录,将libxl\libxl-4.2.0\lib64文件夹下的libxl.so库文件拷贝到刚才创建的目录下
- 3,创建 /usr/local/include/libxl目录,将libxl\libxl-4.2.0\include_cpp文件夹下的头文件拷贝到刚才创建的目录下
Clion配置
cmake_minimum_required(VERSION 3.23)
set(CMAKE_CXX_STANDARD 11)
link_directories(/usr/local/libxl/lib64)
include_directories(/usr/local/include/libxl)
add_executable(libxl_test main.cpp)
target_link_libraries(${PROJECT_NAME} xl z)
# g++ -o ExcelOutputProgram ExcelOutputProgram.cpp -lxl -lz
代码
#define filename "/home/chy-cpabe/CLionProjects/libxl_test/output.xlsx"
bool fileExists(const std::string& excel_file) {
std::ifstream file(excel_file);
int write_excel_content(libxl::Book *USBKey_info,libxl::Sheet *sheet,
std::string DEVINFO_SerialNumber,
std::string Signing_Certificate_SerialNumber)
int nextRowIndex = sheet->lastRow();
sheet->writeStr(nextRowIndex, 0, DEVINFO_SerialNumber.c_str());
sheet->writeStr(nextRowIndex, 1,Signing_Certificate_SerialNumber.c_str());
auto now = std::chrono::system_clock::now();
std::time_t currentTime = std::chrono::system_clock::to_time_t(now);
std::tm *localTime = std::localtime(¤tTime);
int year = localTime->tm_year + 1900;
int month = localTime->tm_mon + 1;
int day = localTime->tm_mday;
int hour = localTime->tm_hour;
int minute = localTime->tm_min;
libxl::Format* format2 = USBKey_info->addFormat();
format2->setNumFormat(libxl::NUMFORMAT_CUSTOM_MDYYYY_HMM);
format2->setAlignH(libxl::ALIGNH_CENTER);
sheet->writeNum(nextRowIndex, 2, USBKey_info->datePack(year,month,day,hour,minute),format2);
int write_excel(bool increase_sheet,std::string sheetName_new,
std::string DEVINFO_SerialNumber,
std::string Signing_Certificate_SerialNumber){
libxl::Book *USBKey_info = xlCreateXMLBook();
if (!fileExists(filename)) {
if (USBKey_info->save(filename)) {
std::cout << "excel表格创建成功!" << std::endl;
std::cout << "excel表格创建失败!" << std::endl;
USBKey_info->load(filename);
if (increase_sheet == true){
libxl::Sheet *sheet = USBKey_info->addSheet(sheetName_new.c_str());
libxl::Format* centerAlign = USBKey_info->addFormat();
centerAlign->setAlignH(libxl::ALIGNH_CENTER);
sheet->setCol(0, 2, 30, centerAlign);
sheet->writeStr(1, 0, "DEVINFO SerialNumber");
sheet->writeStr(1, 1, "Signing Certificate SerialNumber");
sheet->writeStr(1, 2, "Date");
write_excel_content(USBKey_info, sheet,DEVINFO_SerialNumber,
Signing_Certificate_SerialNumber);
std::cerr << "新增 使用用户指定名称创建sheet的索引失败!" << std::endl;
int sheetCount = USBKey_info->sheetCount()-1;
libxl::Sheet *sheet = USBKey_info->getSheet(sheetCount);
write_excel_content(USBKey_info, sheet,DEVINFO_SerialNumber,
Signing_Certificate_SerialNumber);
std::cerr << "利旧 获取最后一个sheet的索引失败!" << std::endl;
if (USBKey_info->save(filename)) {
std::cout << "Excel 文件成功保存" << std::endl;
std::cout << "Excel 文件保存失败" << std::endl;
printf("请输入要创建的工作表名称:\n");
std::string DEVINFO_SerialNumber{"00FF4821104E2607"};
std::string Signing_Certificate_SerialNumber{"4495"};
write_excel(true,sheetName,DEVINFO_SerialNumber,
Signing_Certificate_SerialNumber);
write_excel(true,"new1",DEVINFO_SerialNumber,
Signing_Certificate_SerialNumber);
write_excel(false,"new",DEVINFO_SerialNumber,
Signing_Certificate_SerialNumber);
结果展示
注意
- sheet新建需要保证每个sheet的name是不一样的,重名会出错
write_excel(true,"new1",DEVINFO_SerialNumber,
Signing_Certificate_SerialNumber);