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. C#编程实现朴素贝叶斯算法下的情感分析

    C#编程实现 这篇文章做了什么 朴素贝叶斯算法是机器学习中非常重要的分类算法,用途十分广泛,如垃圾邮件处理等.而情感分析(Sentiment Analysis)是自然语言处理(Natural Lang ...

  2. myeclipse中java文件头注释格式设置

    转载:http://www.blogjava.net/yxhxj2006/archive/2014/01/14/408940.html myeclipse中java文件头注释格式设置  windows ...

  3. [POJ] 3461 Oulipo [KMP算法]

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23667   Accepted: 9492 Descripti ...

  4. abiword rtf 解析

    目前为止,代码跟进,知道是这个地方进行文件解析的 T_Error IE_Imp_RTF::importFile(const char * szFilename)

  5. Is there a complete List of JVM exit codes

    Argument passed to System.exit(x) -> becomes the JVM exit code. Exit code 0 is used to indicate n ...

  6. poj3480--John

    题意:n堆石子,两人轮流操作,每次操作只能选定其中一堆,并取走若干个(>=1个).谁取走最后一个谁输.给定一个状态,问先取的赢还是后取的赢. 整个游戏反过来,如果sg为0先手必胜,不为0必败.( ...

  7. Find the largest multiple of 3 解答

    Question Given an array of non-negative integers. Find the largest multiple of 3 that can be formed ...

  8. KDTree详解及java实现

    本文内容基于An introductory tutoril on kd-trees 1.KDTree介绍 KDTree根据m维空间中的数据集D构建的二叉树,能加快常用于最近邻查找(在加快k-means ...

  9. C++小知识之wsprintf使用

    在C语言中格式化字符串可以使用printf,但是在WINDOWS编程设计中却行不通了,但是却有变通的方法,那就是用 wsprintf这个函数.它的格式如下: int wsprintf (    LPT ...

  10. Hive 12、Hive优化

    要点:优化时,把hive sql当做map reduce程序来读,会有意想不到的惊喜. 理解hadoop的核心能力,是hive优化的根本. 长期观察hadoop处理数据的过程,有几个显著的特征: 1. ...