C++ xlsx文件格式读写

测试
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifndef _WIN32
#include <unistd.h>
#endif
#include "xlsxio_write.h"
#include "xlsxio_read.h"

int testWrite()
{
    const char* filename = "example.xlsx";
    xlsxiowriter handle;
    //open .xlsx file for writing (will overwrite if it already exists)
    if ((handle = xlsxiowrite_open(filename, "MySheet")) == NULL) {
        fprintf(stderr, "Error creating .xlsx file\n");
        return 1;
    }
    //set row height
    xlsxiowrite_set_row_height(handle, 1);
    //how many rows to buffer to detect column widths
    xlsxiowrite_set_detection_rows(handle, 10);
    //write column names
    xlsxiowrite_add_column(handle, "Col1", 0);
    xlsxiowrite_add_column(handle, "Col2", 21);
    xlsxiowrite_add_column(handle, "Col3", 0);
    xlsxiowrite_add_column(handle, "Col4", 2);
    xlsxiowrite_add_column(handle, "Col5", 0);
    xlsxiowrite_add_column(handle, "Col6", 0);
    xlsxiowrite_add_column(handle, "Col7", 0);
    xlsxiowrite_next_row(handle);
    //write data
    int i;
    for (i = 0; i < 1000; i++) {
        xlsxiowrite_add_cell_string(handle, "Test");
        xlsxiowrite_add_cell_string(handle, "A b  c   d    e     f\nnew line");
        xlsxiowrite_add_cell_string(handle, "&% <test> \"'");
        xlsxiowrite_add_cell_string(handle, NULL);
        xlsxiowrite_add_cell_int(handle, i);
        xlsxiowrite_add_cell_datetime(handle, time(NULL));
        xlsxiowrite_add_cell_float(handle, 3.1415926);
        xlsxiowrite_next_row(handle);
    }
    //close .xlsx file
    xlsxiowrite_close(handle);
    return 0;
}


//callback data structure for listing sheets
struct xlsx_list_sheets_data {
    char* firstsheet;
};

//callback function for listing sheets
int xlsx_list_sheets_callback(const char* name, void* callbackdata)
{
    struct xlsx_list_sheets_data* data = (struct xlsx_list_sheets_data*)callbackdata;
    if (!data->firstsheet)
        data->firstsheet = strdup(name);
    printf(" - %s\n", name);
    return 0;
}

//callback function for end of row
int sheet_row_callback(size_t row, size_t maxcol, void* callbackdata)
{
    printf("\n");
    return 0;
}

//callback function for cell data
int sheet_cell_callback(size_t row, size_t col, const XLSXIOCHAR* value, void* callbackdata)
{
    if (col > 1)
        printf("\t");
    if (value)
        printf("%s", value);
    return 0;
}

int testRead() {
    const char* filename = "example.xlsx";
    xlsxioreader xlsxioread;
    //open .xlsx file for reading
    if ((xlsxioread = xlsxioread_open(filename)) == NULL) {
        fprintf(stderr, "Error opening .xlsx file\n");
        return 1;
    }
    //list available sheets
    struct xlsx_list_sheets_data sheetdata;
    sheetdata.firstsheet = NULL;
    printf("Available sheets:\n");
    xlsxioread_list_sheets(xlsxioread, xlsx_list_sheets_callback, &sheetdata);

    //read data
    xlsxioread_process(xlsxioread, sheetdata.firstsheet, XLSXIOREAD_SKIP_EMPTY_ROWS, sheet_cell_callback, sheet_row_callback, NULL);

    //clean up
    free(sheetdata.firstsheet);
    xlsxioread_close(xlsxioread);
    return 0;
}

void test() {
    testWrite();    // 测试写入
    testRead();     // 测试读取
}
输出
文件结构
参考

GitHub - brechtsanders/xlsxio: XLSX I/O - C library for reading and writing .xlsx files

C++ PDF PoDoFo库使用教程-CSDN博客

C++ 导出CSV文件_c++导出cvs文件-CSDN博客


创作不易,小小的支持一下吧!

相关推荐
wainyz1 分钟前
Java NIO操作
java·开发语言·nio
工业3D_大熊7 分钟前
【虚拟仿真】CEETRON SDK在船舶流体与结构仿真中的应用解读
java·python·科技·信息可视化·c#·制造·虚拟现实
lzb_kkk16 分钟前
【JavaEE】JUC的常见类
java·开发语言·java-ee
Zfox_16 分钟前
【Linux】进程信号全攻略(二)
linux·运维·c语言·c++
我不是李.杨31 分钟前
Windows10/11开启卓越性能模式 windows开启卓越性能电源模式 工作电脑开启卓越性能模式 电脑开启性能模式
windows·电脑
起名字真南34 分钟前
【OJ题解】C++实现字符串大数相乘:无BigInteger库的字符串乘积解决方案
开发语言·c++·leetcode
少年负剑去35 分钟前
第十五届蓝桥杯C/C++B组题解——数字接龙
c语言·c++·蓝桥杯
cleveryuoyuo35 分钟前
AVL树的旋转
c++
爬山算法40 分钟前
Maven(28)如何使用Maven进行依赖解析?
java·maven
神仙别闹1 小时前
基于MFC实现的赛车游戏
c++·游戏·mfc