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博客


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

相关推荐
AI产品实战15 分钟前
Gradle打包生成dependencies.xml详解
xml·java·gradle
方华世界29 分钟前
企业级Java AI Agent应用平台
java·开发语言·人工智能
劉宇雁1 小时前
C++ ASCII 3D无尽跑酷游戏
c++·游戏·3d
折哥的程序人生 · 物流技术专研1 小时前
第8篇:模板方法模式的优缺点与面试高频考点
java·设计模式·面试·模板方法模式·行为型模式·优缺点·扩充系列
杜子不疼.1 小时前
【C++ 在线五子棋对战】- 会话管理模块实现
开发语言·c++
有点。1 小时前
C++深度优先搜索(DFS)的概念(一)
开发语言·c++·深度优先
旖-旎2 小时前
《LeetCode 978 最长湍流子数组 || LeetCode 139 单词拆分》
c++·算法·leetcode·动态规划
人道领域2 小时前
【0-1的agent进阶篇】Prompt 与上下文工程
java·开发语言·prompt·mcp
aaPIXa6222 小时前
C++大型项目模块化拆分实战记录
开发语言·c++
AOwhisky2 小时前
Python 基础语法(上篇 + 下篇)——综合自测题
linux·windows·python