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库,要想达到完全的效果可以去网上搜搜。
C++中利用libxl操作Excel表格的更多相关文章
- Python 利用Python操作excel表格之openyxl介绍Part2
利用Python操作excel表格之openyxl介绍 by:授客 QQ:1033553122 欢迎加入全国软件测试交流qq群(群号:7156436) ## 绘图 c = LineChart() ...
- Python 利用Python操作excel表格之openyxl介绍Part1
利用Python操作excel表格之openyxl介绍 by:授客 QQ:1033553122 欢迎加入全国软件测试交流qq群(群号:7156436),免费获取以下性能监控工具(类似Nmon精简版) ...
- Python 利用Python操作excel表格之xlwt介绍
利用Python操作excel表格之xlwt介绍 by:授客 QQ:1033553122 直接上代码 案例1 #!/usr/bin/env python # -*- coding:utf-8 ...
- Visual Studio 2010利用libxl读写excel表格数据
C++读写数据,一般通过txt文件,但是随着数据量的增大,采集数据时运用excel表格的优势得以逐步体现.本文主要介绍一下运用第三方库libxl,对excel表格数据进行读写.分为三个部分,第一部分是 ...
- 用NPOI、C#操作Excel表格生成班级成绩单
在C#中利用NPOI操作Excel表格非常方便,几乎上支持所有的Excel表格本身所有的功能,如字体设置.颜色设置.单元格合并.数值计算.页眉页脚等等. 这里准备使用NPOI生成一个班级成绩单Exce ...
- php中使用PHPExcel操作excel(xls)文件
读取中文的xls.csv文件会有问题,网上找了下资料,发现PHPExcel类库好用,官网地址:http://phpexcel.codeplex.com/ 1.读取xls文件内容 代码如下 复制代码 ...
- 【转】python操作excel表格(xlrd/xlwt)
[转]python操作excel表格(xlrd/xlwt) 最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异, ...
- 转载:python操作excel表格(xlrd/xlwt)
python操作excel表格(xlrd/xlwt) 最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异,而 ...
- C#开发中使用Npoi操作excel实例代码
C#开发中使用Npoi操作excel实例代码 出处:西西整理 作者:西西 日期:2012/11/16 9:35:50 [大 中 小] 评论: 0 | 我要发表看法 Npoi 是什么? 1.整个Exce ...
随机推荐
- Facebook公开其Hadoop与Avatarnode代码——有效解决Namenode顽疾
Google在2004年创造了MapReduce,MapReduce系统获得成功的原因之一是它为编写需要大规模并行处理的代码提供了简单的编程模式.MapReduce集群可包括数以千计的并行操作的计算机 ...
- LDD命令--可执行文件依赖的库出现错误时
http://littlepig3056.blog.163.com/blog/static/180758353201212751814134/ ldd 查看可执行文件依赖的库,结果会列出依赖的库名及 ...
- Linux下Bash运行脚本
命令行应该这样写: sh -c "脚本字符串" example: sh -c "if ! type dpkg > /dev/null ; then echo 'so ...
- paip.Adblock屏蔽onlinedown华军软件园的4秒下载广告总结..
paip.Adblock屏蔽onlinedown华军软件园的4秒下载广告总结.. 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址 ...
- Android Studio代码着色插件
文章将给大家分享Studio中代码高亮插件,个人觉得换个代码着色方式还是挺有必要的,起码让视觉上有个变换,感官上爽一些.就像吃惯了大鱼大肉,偶尔也来点青菜萝卜吧.以下是个人喜欢的几款,给个效果图大家看 ...
- HTTP缓存 1.0 vs 1.1
在“使用ETag跟踪用户”中有一点被忽略了,因为要用这张小图统计统计uv, 所以要求浏览器必须每次都要发送这个图片的请求.这需要服务器对图片的缓存策略做设置. http/1.0 和 http/1.1 ...
- hdu 5427 A problem of sorting(字符排序)
Problem Description There are many people's name and birth in a list.Your task is to print the name ...
- linux文件权限整理
网上对linux文件权限的已经很多,不过还是要自己整理一下,不然每次都要查资料. linux下所有东西都是文件,包括设备,所以这里的文件也包括文件夹. 先是查看文件权限:ls -lh xzc@xzc- ...
- Thinkphp多表联查mysql写法
$model=M("user","","mysql://root:222222@localhost:3306/jiaoyou"); //换数 ...
- Tcp通讯协议
了解了Udp通讯协议之后,我们再认识一个常用的通讯协议:Tcp Tcp传输特点: --依赖于Socket和ServerSocket对象 --建立客户端和服务端 --建立连接后,通过Socket中的 I ...