您的位置:首页 > 其它

libxl库 学习,读写excel中数据

2016-02-25 14:00 399 查看
程序如下:

#include "stdafx.h"
#include <iostream>
#include "include_cpp/libxl.h"
using namespace libxl;
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
Book* book = xlCreateBook(); // xlCreateXMLBook() for xlsx
if(book)
{
Sheet* sheet = book->addSheet(L"Sheet1");

if(sheet)
{
sheet->writeStr(2, 1, L"Hello, World !");
sheet->writeNum(3, 1, 1000);
}
book->save(L"example.xls");
book->release();
}

return 0;
}


配置:

1、把include_cpp 和libxl.lib,libxl.dll文件放到工程中。并在属性---VC++目录,包含这个目录

2、依赖项中加入Libxl.lib库

3、包含#include "include_cpp/libxl.h"

                                 using namespace libxl;

   

        

    

以下程序是把一个excel表格中的数据写入到  MNIST手写体识别   文件中,

写入4个字节的数据,读4个字节的数据,去掉了文件头

#include "stdafx.h"
#include <iostream>
#include <iostream>
#include <fstream>
#include <opencv2/opencv.hpp>
#include "include_cpp/libxl.h"

using namespace libxl;
using namespace std;
using namespace cv;
/*
#define NUMBER_OF_TRAINING_SAMPLES 60000
#define ATTRIBUTES_PER_SAMPLE 784
#define NUMBER_OF_TESTING_SAMPLES 10000
#define NUMBER_OF_CLASSES 10
*/
#define NUMBER_OF_TRAINING_SAMPLES 5120 //训练样本个数
#define ATTRIBUTES_PER_SAMPLE 21 //有多少列
#define NUMBER_OF_TESTING_SAMPLES 825 //测试样本个数

#define NUMBER_OF_CLASSES 4 //种类个数

int _tmain(int argc, _TCHAR* argv[])
{
ofstream outfile1,outfile2;
outfile1.open("train-images.idx3-ubyte",ios::binary);
outfile2.open("train-labels.idx1-ubyte",ios::binary);

//表示需要读出测试样例
ofstream testfile1,testfile2;
testfile1.open("t10k-images.idx3-ubyte",ios::binary);
testfile2.open("t10k-labels.idx1-ubyte",ios::binary);

Book *book = xlCreateBook(); // xlCreateXMLBook() for xlsx
CellType ct;
bool f=book->load("testnewdata.xls");

Sheet *sheet =book->getSheet(0);
int cellnum;
int label;

//写训练数据与label
for (int i=0;i<NUMBER_OF_TRAINING_SAMPLES;i++)
{
for (int j=1;j<ATTRIBUTES_PER_SAMPLE+1;j++)
{
//ct = sheet->cellType(i,j);//单元格的数据类型 1表示是数字 2表示是字符串
cellnum = (int)sheet->readNum(i,j);
outfile1.write((char *)&cellnum ,4);
//s = sheet->readStr(i,j);
//outfile1.write(s ,1);
}
label = sheet->readNum(i,0);
outfile2.write((char *)&label,4);
}

//写测试数据与label
for (int i=0;i<NUMBER_OF_TESTING_SAMPLES;i++)
{
for (int j=1;j<ATTRIBUTES_PER_SAMPLE+1;j++)
{
//ct = sheet->cellType(i,j);//单元格的数据类型 1表示是数字 2表示是字符串
cellnum = sheet->readNum(i,j);
testfile1.write((char *)&cellnum ,4);
}
label = sheet->readNum(i,0);
testfile2.write((char *)&label,4);
}
outfile1.close();
outfile2.close();
testfile1.close();
testfile2.close();

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: