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

  • 相关阅读:
    带你一分钟理解闭包js面向对象编程
    ThinkPad X1 Carbon 2022领衔,联想ThinkFamily 2022一众新品发布
    C#中const用法 C#中const用法详解
    ThinkPad哪个产品系列比较好?
    VB/VBA,可能超乎你的想象
    世界六大顶尖的软件强国
    2021年李宁跑鞋矩阵分析及挑选购买攻略
    .NET Core与.NET Framework、Mono之间的关系
    李宁动真格!多款「弹簧鞋」同时发售!扔地下能弹回来那种
    ts 泛型
  • 原文地址:https://www.cnblogs.com/gisvito/p/4841371.html
Copyright © 2020-2023  润新知