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


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

相关推荐
神仙别闹33 分钟前
基于java的改良版超级玛丽小游戏
java
唐诺1 小时前
几种广泛使用的 C++ 编译器
c++·编译器
黄油饼卷咖喱鸡就味增汤拌孜然羊肉炒饭1 小时前
SpringBoot如何实现缓存预热?
java·spring boot·spring·缓存·程序员
暮湫1 小时前
泛型(2)
java
超爱吃士力架1 小时前
邀请逻辑
java·linux·后端
南宫生1 小时前
力扣-图论-17【算法学习day.67】
java·学习·算法·leetcode·图论
转码的小石2 小时前
12/21java基础
java
李小白662 小时前
Spring MVC(上)
java·spring·mvc
冷眼看人间恩怨2 小时前
【Qt笔记】QDockWidget控件详解
c++·笔记·qt·qdockwidget
GoodStudyAndDayDayUp2 小时前
IDEA能够从mapper跳转到xml的插件
xml·java·intellij-idea