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

  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库,要想达到完全的效果可以去网上搜搜。

 

转载于:https://www.cnblogs.com/gisvito/p/4841371.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值