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. 转载--http协议学习和总结

    http的了解一直停留在一知半解的程度,今天看到阿蜜果大大的博客,果断学习了,这里做个转载,希望阿蜜果大大不要怪罪~~ 3.1 Cookie和Session Cookie和Session都为了用来保存 ...

  2. 163k地方门户网站系统团购定时结束限量控制

    #coding=utf8 #!/usr/bin/env python # 网站自动审核系统 import pymssql import re import sys import datetime im ...

  3. Smarty 变量使用

    Smarty的标签都是使用定界符括起来. 默认定界符是{ 和 }, 但定界符可以被改变. 比如说在本手册,我们会假定你在使用默认的定界符. 在Smarty看来,任何在定界符之外的内容,都是静态的,或者 ...

  4. mysql use mysql hang

    uat-db03:/root# mysql -A -uroot -p1234567 Warning: Using a password on the command line interface ca ...

  5. 简单实用后台任务执行框架(Struts2+Spring+AJAX前端web界面可以获取进度)

    使用场景: 在平常web开发过程中,有时操作员要做一个后台会运行很长时间的任务(如上传一个大文件到后台处理),而此时前台页面仍需要给用户及时的进度信息反馈,同时还要避免前台页面超时. 框架介绍: 本架 ...

  6. Uva227.Puzzle

    题目连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  7. JavaScript面向对象之类的创建

    JavaScript对象的定义: 在js中函数极为对象,对象分为二种:对象字变量产生的对象连接到Object.prototype:函数对象连接到Function.prototype 方法:当一个函数被 ...

  8. PC--CSS技巧

    1.图片不存在的时候,显示一个默认图片 <img src=”01.jpg” onerror=”this.src=’02.jpg'” /> 2.CSS强制图片自适应大小 img {width ...

  9. Atitit.软件guibuttonand面板---os区-----linux windows搜索文件 目录

    Atitit.软件guibuttonand面板---os区-----搜索文件 1. Find 1 2. 寻找文件夹 1 3. 2. Locate// everything 1 4. 3. Wherei ...

  10. 使用maven编译的时候提示 maven-source 1.3 中不支持注释请使用 -source 5 或更高版本以启用注释的错误。

    在编译的模块的pom文件中加上 <build> <plugins> <plugin> <groupId>org.apache.maven.plugins ...