C++读写数据,一般通过txt文件,但是随着数据量的增大,采集数据时运用excel表格的优势得以逐步体现。本文主要介绍一下运用第三方库libxl,对excel表格数据进行读写。分为三个部分,第一部分是Visual Studio2010环境配置,第二部分是读操作,第三部分是写操作。

一、环境配置

1、下载第三方库libxl,网址是http://www.libxl.com/download.html

2、文件包含readme.txt,根据自己的系统对环境进行配置,我的是64位,采取的是Microsoft Visual C++配置。

二、读操作

#include <iostream>
#include <conio.h>
#include "libxl.h"

using namespace libxl;

int main()
{
    Book* book = xlCreateBook();
    if(book)
    {
        if(book->load(L"..\\generate\\example.xls"))
        {
            Sheet* sheet = book->getSheet(0);
            if(sheet)
            {
                const wchar_t* s = sheet->readStr(2, 1);
                if(s) std::wcout << s << std::endl << std::endl;                

                std::cout << sheet->readNum(4, 1) << std::endl;
                std::cout << sheet->readNum(5, 1) << std::endl;
                const wchar_t* f = sheet->readFormula(6, 1);
                if(f) std::wcout << f << std::endl << std::endl;

                int year, month, day;
                book->dateUnpack(sheet->readNum(8, 1), &year, &month, &day);
                std::cout << year << "-" << month << "-" << day << std::endl;
            }
        }
        else
        {
            std::cout << "At first run generate !" << std::endl;
        }

        book->release();
    }

    std::cout << "\nPress any key to exit...";
    _getch();

    return 0;
}

三、写操作

#include <iostream>
#include <windows.h>
#include "libxl.h"

using namespace libxl;

int main()
{
    Book* book = xlCreateBook();
    if(book)
    {
        Sheet* sheet = book->addSheet(L"Sheet1");
        if(sheet)
        {
            sheet->writeStr(2, 1, L"Hello, World !");
            sheet->writeNum(4, 1, 1000);
            sheet->writeNum(5, 1, 2000);

            Font* font = book->addFont();
            font->setColor(COLOR_RED);
            font->setBold(true);
            Format* boldFormat = book->addFormat();
            boldFormat->setFont(font);
            sheet->writeFormula(6, 1, L"SUM(B5:B6)", boldFormat);

            Format* dateFormat = book->addFormat();
            dateFormat->setNumFormat(NUMFORMAT_DATE);
            sheet->writeNum(8, 1, book->datePack(2008, 4, 29), dateFormat);

            sheet->setCol(1, 1, 12);
        }

        if(book->save(L"example.xls"))
        {
            ::ShellExecute(NULL, L"open", L"example.xls", NULL, NULL, SW_SHOW);
        }
        else
        {
            std::cout << book->errorMessage() << std::endl;
        }

        book->release();
    } 

    return 0;
}


Visual Studio 2010利用libxl读写excel表格数据的更多相关文章

  1. 【转】Visual Studio 2010在数据库生成随机测数据

    测试在项目中是很重要的一个环节,在Visual Studio 2010中,在测试方面已经有很好的支持了,比如有单元测试,负载测试等等.在数据测试的方面,Visual Studio 2010,还支持对数 ...

  2. C++中利用libxl操作Excel表格

    libxl是一款操作excel的商业库,支持C.C++.C#和Delphi.下文利用libxl在C++中实现了从excel文档的表1中随机挑选记录,保存到新建的表中.以下为代码: #include & ...

  3. Python利用xlutils统计excel表格数据

    假设有像上这样一个表格,里面装满了各式各样的数据,现在要利用模板对它进行统计每个销售商的一些数据的总和.模板如下: 代码开始: 1 #!usr/bin/python3 2 # -*-coding=ut ...

  4. Visual Studio 2010如何利用宏

    最近在做后台代码的拆分,由于机器升级,原来装的添加注释的插件不能用了. 看来只有自己想办法了,看了下利用宏添加注释与把项目展开.折叠的方式: 参考了以下几个内容: 1.Visual Studio 20 ...

  5. [入门级] visual studio 2010 mvc4开发,用ibatis作为数据库访问媒介(一)

    [入门级] visual studio 2010 mvc4开发,用ibatis作为数据库访问媒介(一) Date  周二 06 一月 2015 By 钟谢伟 Tags mvc4 / asp.net 示 ...

  6. [转]基于Visual Studio 2010 进行敏捷/Scrum模式开发

    http://www.infoq.com/cn/articles/visual-studio-2010-agile-scrum-development 根据Forrester Research今年第二 ...

  7. 【转】【UML】使用Visual Studio 2010 Team System中的架构师工具(设计与建模)

    Lab 1: 应用程序建模 实验目标 这个实验的目的是展示如何在Visual Studio 2010旗舰版中进行应用程序建模.团队中的架构师会通过建模确定应用程序是否满足客户的需求. 你可以创建不同级 ...

  8. Visual Studio 2010 中的 Web 开发

    概述 Microsoft Visual Studio 2010 为 ASP.NET Web 应用程序的开发提供非常多新的功能.这些新功能旨在帮助开发者高速方便地创建和部署质量高且功能全的 Web 应用 ...

  9. 在 Visual Studio 2010 中开发和部署 Windows Azure 应用程序

    原文 在 Visual Studio 2010 中开发和部署 Windows Azure 应用程序 在 Visual Studio 2010 中开发和部署 Windows Azure 应用程序 Jim ...

随机推荐

  1. Android Studio: You need to use a Theme.AppCompat theme (or descendant) with this activity.

    错误描述为: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with ...

  2. linux2.6.37内核接两个硬盘导致读写效率变低的问题

    一.问题分析: 通过跟踪定位write系统调用的实现发现,在每次调用a_ops->write_end之后,都会去调用balance_dirty_pages_ratelimited,该函数负责检查 ...

  3. linux下数据同步、回写机制分析

    一.前言在linux2.6.32之前,linux下数据同步是基于pdflush线程机制来实现的,在linux2.6.32以上的版本,内核彻底删掉了pdflush机制,改为了基于per-bdi线程来实现 ...

  4. Hadoop2动态调整Log级别-以datanode的heartbeat log为例

    在Hadoop中,有些log信息在正常情况下是不打印出来的.比如datanode发送heartbeat的日志. 代码位于BPServiceActor#sendHeartBeat方法中,如下图: 由于默 ...

  5. React Native自定义导航条

    Navigator和NavigatorIOS 在开发中,需要实现多个界面的切换,这时候就需要一个导航控制器来进行各种效果的切换.在React Native中RN为我们提供了两个组件:Navigator ...

  6. Linux 高性能服务器编程——socket选项

    socket选项函数 功能:用来读取和设置socket文件描述符属性的方法 函数: #include <sys/scoket.h> int getsockopt ( int sockfd, ...

  7. Servlet - Listener、Filter、Decorator

    Servlet 标签 : Java与Web Listener-监听器 Listener为在Java Web中进行事件驱动编程提供了一整套事件类和监听器接口.Listener监听的事件源分为Servle ...

  8. 详解EBS接口开发之库存事务处理-物料批次导入

    库存事务处理-物料批次导入 --系统批次表 SELECT * FROM MTL_LOT_NUMBERS T; --API创建批次 inv_lot_api_pub.create_inv_lot(x_re ...

  9. activiti实战系列 activiti连线

    11:连线 11.1:流程图 注意:如果将流程图放置在和java类相同的路径,需要配置: 11.2:部署流程定义+启动流程实例 11.3:查询我的个人任务 11.4:完成任务 说明: 1)使用流程变量 ...

  10. UE4 读取本地图片

    参考链接:https://answers.unrealengine.com/questions/235086/texture-2d-shows-wrong-colors-from-jpeg-on-ht ...