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

#include <iostream>
#include <Windows.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include "libxl.h"

#define MaxRandomCount 745;
#define MaxCol 40

using namespace libxl;
using namespace std;

bool copyCellToDestination(Sheet* srcSheet,int srcRow, int srcCol, Sheet* dstSheet, int dstRow, int dstCol);

int main()
{
  const wchar_t* filename = L"D:\\WORKSTATION\\DATA\\图像分类实验\\ydexcel\\bmtYD.xls";
  cout<<"program started successfully!"<<endl;
  cout<<"started to generate random index..."<<endl;
  //generate random index
  vector<unsigned> vec;
  vector<unsigned>::iterator it;
  while(vec.size() < 400)
  {
    unsigned index = rand()%MaxRandomCount;
    bool isExists = false;
    for( it = vec.begin(); it != vec.end(); it++)
    {
      if(index == *it)
      {
        isExists = true;
        break;
      }
    }
    if(isExists) continue;
    cout<<index<<"\t";
    vec.push_back(index);
  }

  //operate excel file
  Book* book = xlCreateBook();
  if(!book) return 1;
  if(!book->load(filename)) return 1;

  int count = book->sheetCount();
  Sheet* dstSheet = book->addSheet(L"selected");

  if( !dstSheet)
  {
    cout<<"create new sheet failed, program is shutting down"<<endl;
    return 1;
  }
  cout<<"create new sheet successful!"<<endl;
  //get original sheet and destination sheet
  Sheet* oriSheet = book->getSheet(0);

  if(!oriSheet || !dstSheet)
  {
    cout<<"open sheet faild!"<<endl;
    return 1;
  }
  //travel the whole vector
  int dstRow = 1;
  for(it=vec.begin(); it!=vec.end();it++)
  {
    for(int col = 0; col < MaxCol; col++)
    {
      //copy selected data to new sheet
      if(!copyCellToDestination(oriSheet,*it, col, dstSheet, dstRow, col))
      {
        cout<<"fatal error occured when copy selected data in original sheet to destination sheet"
          "program will ended! "<<endl;
        return 1;
      }
    }
    dstRow++;
  }
  book->save(L"C:\\Users\\Administrator\\Desktop\\result.xls");

  system("pause");
  return 0;
}

bool copyCellToDestination(Sheet* srcSheet,int srcRow, int srcCol, Sheet* dstSheet, int dstRow, int dstCol)
{
  Format* format = srcSheet->cellFormat(srcRow, srcCol);
  CellType type = srcSheet->cellType(srcRow, srcCol);
  switch (type)
  {
    case CELLTYPE_BLANK:
    {
      if(!dstSheet->writeBlank(dstRow, dstCol,format)) return false;
      break;
    }
    case CELLTYPE_EMPTY:
      break;
    case CELLTYPE_ERROR:
    {
      if(!dstSheet->writeStr(dstRow, dstCol, L"error", format)) return false;
      break;
    }
    case CELLTYPE_BOOLEAN:
    {
      if(!dstSheet->writeBool(dstRow, dstCol, srcSheet->readBool(srcRow,srcCol,&format))) return false;
      break;
    }
    case CELLTYPE_NUMBER:
    {
      double value = srcSheet->readNum(srcRow, srcCol, &format);
      bool isSuccessed = dstSheet->writeNum(dstRow, dstCol, value);
      if(!isSuccessed) return false;
      break;
    }
    case CELLTYPE_STRING:
    {
      if (!dstSheet->writeStr(dstRow, dstCol, srcSheet->readStr(srcRow, srcCol,&format))) return false;
      break;
    }
    default:
    {
      return false;
      break;
    }
  }
  return true;
}

  以上是实现功能的代码,经过实际运行,可以实现相应的效果,但是由于libxl试用版的限制,只能处理部分数据。网上有破解版的libxl库,要想达到完全的效果可以去网上搜搜。

C++中利用libxl操作Excel表格的更多相关文章

  1. Python 利用Python操作excel表格之openyxl介绍Part2

    利用Python操作excel表格之openyxl介绍 by:授客 QQ:1033553122 欢迎加入全国软件测试交流qq群(群号:7156436) ## 绘图 c = LineChart()    ...

  2. Python 利用Python操作excel表格之openyxl介绍Part1

    利用Python操作excel表格之openyxl介绍 by:授客 QQ:1033553122 欢迎加入全国软件测试交流qq群(群号:7156436),免费获取以下性能监控工具(类似Nmon精简版) ...

  3. Python 利用Python操作excel表格之xlwt介绍

    利用Python操作excel表格之xlwt介绍   by:授客 QQ:1033553122 直接上代码   案例1 #!/usr/bin/env python # -*- coding:utf-8 ...

  4. Visual Studio 2010利用libxl读写excel表格数据

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

  5. 用NPOI、C#操作Excel表格生成班级成绩单

    在C#中利用NPOI操作Excel表格非常方便,几乎上支持所有的Excel表格本身所有的功能,如字体设置.颜色设置.单元格合并.数值计算.页眉页脚等等. 这里准备使用NPOI生成一个班级成绩单Exce ...

  6. php中使用PHPExcel操作excel(xls)文件

    读取中文的xls.csv文件会有问题,网上找了下资料,发现PHPExcel类库好用,官网地址:http://phpexcel.codeplex.com/ 1.读取xls文件内容  代码如下 复制代码 ...

  7. 【转】python操作excel表格(xlrd/xlwt)

    [转]python操作excel表格(xlrd/xlwt) 最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异, ...

  8. 转载:python操作excel表格(xlrd/xlwt)

    python操作excel表格(xlrd/xlwt)   最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异,而 ...

  9. C#开发中使用Npoi操作excel实例代码

    C#开发中使用Npoi操作excel实例代码 出处:西西整理 作者:西西 日期:2012/11/16 9:35:50 [大 中 小] 评论: 0 | 我要发表看法 Npoi 是什么? 1.整个Exce ...

随机推荐

  1. File类详解

    一.File类: File类时io包中唯一代表磁盘文件本身的对象.File类定义了一些与平台无关的方法来操作文件,可以通过调用File类中的方法,实现创建.删除.重命名文件等. File类的对象主要用 ...

  2. jquery checkbox 全选、取消全选

    $("#checkall").click(function(){ $("input[name='uid']").prop("checked" ...

  3. BZOJ 2324 营救皮卡丘

    http://www.lydsy.com/JudgeOnline/problem.php?id=2324 思路:最小费用最大流 考虑设数组d[k][i][j],代表只用前k个城市,i到j的最短路 然后 ...

  4. Unity重要的函数

    Awake 当一个脚本实例被载入时Awake被调用. Start Start仅在Update函数第一次被调用前调用. Update 当MonoBehaviour启用时,其Update在每一帧被调用. ...

  5. [HDU 1535]Invitation Cards[SPFA反向思维]

    题意: (欧洲人自己写的题面就是不一样啊...各种吐槽...果断还是看晕了) 有向图, 有个源叫CCS, 求从CCS到其他所有点的最短路之和, 以及从其他所有点到CCS的最短路之和. 思路: 返回的时 ...

  6. LR选择哪种方式录制

    LR选择哪种方式录制,有以下考虑原则: 1.基于浏览器的应用程序推荐使用HTML-basic script方式录制 2.不是基于浏览器的应用程序推荐使用URL-basic script方式录制 3.如 ...

  7. js转换ascii编码如中文友转换为编码友;可防止乱码

  8. Query获取值常用

    Query获取Select选择的Text和Value:语法解释:1. $("#select_id").change(function(){//code...});   //为Sel ...

  9. webservice 远程调试配置

    在.NET 中已经默认将WEBSERVICE的远程调试功能关闭,有的时候我们需要远程调试程序的时候,就需要打开此功能我们只需在WEBSERVICE的项目的中添web.config的<system ...

  10. Android 使用开源xUtils来实现多线程下载(非原创)

    1.程序员自己也是可以实现多线程下载的,只是代码量比较大,而且,其中有许多细节需要考虑到,在GitHub上有人写好的代码,我们可以拿过来使用下,节省了我们开发程序的时间 2.导包:xUtils-2.6 ...