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

时间:2021-08-05 11:41:18

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