0


【C++】开源:跨平台Excel处理库-libxlsxwriter配置使用

😏★,°:.☆( ̄▽ ̄)/$:.°★ 😏
这篇文章主要介绍Excel处理库-libxlsxwriter配置使用。
无专精则不能成,无涉猎则不能通。——梁启超
欢迎来到我的博客,一起学习,共同进步。
喜欢的朋友可以关注一下,下次更新不迷路🥞

文章目录

😏1. 项目介绍

项目Github地址:

https://github.com/jmcnamara/libxlsxwriter

Wiki地址:

https://libxlsxwriter.github.io/

libxlsxwriter 是一个用于创建 Microsoft Excel XLSX 文件的C库。它提供了一系列功能,可以让您通过编程方式生成包含单元格、图表、格式化等内容的 Excel 文件。下面是 libxlsxwriter 的一些特点和功能:

1.跨平台性:libxlsxwriter 可以在多个操作系统上工作,包括 Linux、macOS 和 Windows。

2.创建 XLSX 文档:它允许您创建 XLSX 格式的 Excel 文档,支持 Excel 2007 及更高版本。

3.丰富的功能:libxlsxwriter 支持创建工作表、单元格、公式、图表、条件格式化、数据筛选等功能。

4.高性能:该库被设计为具有高性能,在大型数据集的情况下生成速度快。

5.支持多种格式和样式:您可以设置单元格的格式、字体、颜色、边框、背景等属性,以及应用数值格式、日期格式、公式和函数等。

6.支持图表:libxlsxwriter 允许您创建各种 Excel 图表,如条形图、饼图、折线图等,并支持自定义图表的样式和属性。

但是要注意,

libxlsxwriter

只能用于创建 XLSX 文件,不支持读取或修改现有的 Excel 文件。(可以通过c++自带的文件处理来读取,处理后的数据再手动导入到最后的表中)

😊2. 环境配置

下面进行环境配置:

# 安装依赖sudoapt-getinstall libxslt-dev
# 下载源码git clone https://github.com/jmcnamara/libxlsxwriter
# 编译cd libxlsxwriter
makesudocp lib/libxlsxwriter.so.5 /usr/lib/libxlsxwriter.so.5
cd cmake
cmake ..makesudomakeinstall

编译运行:

g++ -o main main.cpp -lxlsxwriter&& ./main

😆3. 使用说明

下面进行使用分析:

创建excel并写入示例:

#include"xlsxwriter.h"intmain(){/* Create a new workbook and add a worksheet. */
    lxw_workbook  *workbook  =workbook_new("demo.xlsx");
    lxw_worksheet *worksheet =workbook_add_worksheet(workbook,NULL);/* Add a format. */
    lxw_format *format =workbook_add_format(workbook);/* Set the bold property for the format */format_set_bold(format);/* Change the column width for clarity. */worksheet_set_column(worksheet,0,0,20,NULL);/* Write some simple text. */worksheet_write_string(worksheet,0,0,"Hello",NULL);/* Text with formatting. */worksheet_write_string(worksheet,1,0,"World", format);/* Write some numbers. */worksheet_write_number(worksheet,2,0,123,NULL);worksheet_write_number(worksheet,3,0,123.456,NULL);/* Insert an image. */worksheet_insert_image(worksheet,1,2,"logo.png");workbook_close(workbook);return0;}

参考某博主,生成指定个数的uuid示例:

#include<chrono>#include<condition_variable>#include<ctime>#include<curl/curl.h>#include<curl/easy.h>#include<fstream>#include<functional>#include<future>#include<iostream>#include<iomanip>#include<map>#include<memory>#include<mutex>#include<random>#include<stdio.h>#include<string>#include<uuid/uuid.h>#include<vector>#include"xlsxwriter.h"// ref: https://www.cnblogs.com/Fred1987/p/17442487.html// 编译: g++ -o main main.cpp -lxlsxwriter -luuid -lpthread && ./main

std::string get_time_now(bool is_exact =false){
    std::chrono::time_point<std::chrono::high_resolution_clock> now = std::chrono::high_resolution_clock::now();
    time_t raw_time = std::chrono::high_resolution_clock::to_time_t(now);structtm tm_info =*localtime(&raw_time);
    std::stringstream ss;
    ss << std::put_time(&tm_info,"%Y%m%d%H%M%S");if(is_exact){
        std::chrono::seconds seconds = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch());
        std::chrono::milliseconds mills = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
        std::chrono::microseconds micros = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch());
        std::chrono::nanoseconds nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch());
        std::uint64_t mills_count = mills.count()- seconds.count()*1000;
        std::uint64_t micros_count = micros.count()- mills.count()*1000;
        std::uint64_t nanos_count = nanos.count()- micros.count()*1000;
        ss <<"_"<< std::setw(3)<< std::setfill('0')<< std::to_string(mills_count)<< std::setw(3)<< std::setfill('0')<< std::to_string(micros_count)<< std::setw(3)<< std::setfill('0')<< std::to_string(nanos_count);}return ss.str();}// generate uuidchar*uuid_value =(char*)malloc(40);char*get_uuid_value(){
    uuid_t new_uuid;uuid_generate(new_uuid);uuid_unparse(new_uuid, uuid_value);return uuid_value;}voidxlsxwriter_excel(constint&len){
    lxw_workbook *workbook =workbook_new("uuid.xlsx");

    lxw_worksheet *worksheet =workbook_add_worksheet(workbook,NULL);worksheet_set_column(worksheet,0,0,50,NULL);// 设置列宽// write headerworksheet_write_string(worksheet,0,0,"Header",NULL);worksheet_write_string(worksheet,0,1,"Number",NULL);for(int row =1; row <= len; row++){worksheet_write_string(worksheet, row,0,get_uuid_value(),NULL);worksheet_write_number(worksheet, row,1, row,NULL);}workbook_close(workbook);
    std::cout <<get_time_now(true)<<",finish in "<< __FUNCTION__ << std::endl;}intmain(int agrs,char**argv){xlsxwriter_excel(atoi(argv[1]));// 读取命令行,生成几个uuidreturn0;}

结合CGAL计算几何库对txt点集进行处理,并处理后的数据写入xlsx,示例:

#include<iostream>#include<fstream>#include<vector>#include"xlsxwriter.h"#include<CGAL/Exact_predicates_inexact_constructions_kernel.h>#include<CGAL/Alpha_shape_2.h>typedef CGAL::Exact_predicates_inexact_constructions_kernel K;typedef K::Point_2 Point_2;// 编译: g++ -o main main.cpp -lxlsxwriter -lCGAL -lgmp && ./mainintmain(){// 1. 从文本文件中读取 XY 点集
    std::ifstream inputFile("input.txt");if(!inputFile){
        std::cout <<"Failed to open input file."<< std::endl;return1;}

    std::vector<Point_2> points;double x, y;while(inputFile >> x >> y){
        points.push_back(Point_2(x, y));}
    inputFile.close();// ...// 2. 将处理后的点集写入 xlsx 文件中
    lxw_workbook* workbook =workbook_new("output.xlsx");
    lxw_worksheet* worksheet =workbook_add_worksheet(workbook,NULL);int row =0;for(const Point_2& point : points){int col =0;worksheet_write_number(worksheet, row, col++,CGAL::to_double(point.x()),NULL);worksheet_write_number(worksheet, row, col,CGAL::to_double(point.y()),NULL);
        row++;}workbook_close(workbook);

    std::cout <<"Output file has been generated."<< std::endl;return0;}

在这里插入图片描述

以上。

标签: c++ 开源 excel

本文转载自: https://blog.csdn.net/qq_40344790/article/details/132297300
版权归原作者 Frank学习路上 所有, 如有侵权,请联系我们删除。

“【C++】开源:跨平台Excel处理库-libxlsxwriter配置使用”的评论:

还没有评论