from:http://www.cnblogs.com/paullam/p/3705924.html
使用的平台:vs2013 控制台
创建时需要注意, 安全开发生命周期(SDL)检查 不能勾选(不嫌麻烦的话就默认选吧)

本文百度云下载: http://pan.baidu.com/s/1i5NRg6x //其中ExcelRead.cpp为优快云作者的演示
优快云 download: http://download.youkuaiyun.com/download/xcwmy/6992365 //其中ExcelRead.cpp为优快云作者的演示
codeproject链接:
http://www.codeproject.com/Articles/13852/BasicExcel-A-Class-to-Read-and-Write-to-Microsoft
参考:OpenOffice's Excel file format (
http://sc.openoffice.org/excelfileformat.pdf)
一个用STL C++写的读写Excel文件的类,是CSpreadSheet作者封装的,与CSpreadSheet的区别:不依赖ODBC,而CSpreadSheet依赖ODBC,需要MFC库的支持,不能跨平台。
BasicExcel的限制:
1)不支持格式化;
2)不支持公式;
3)不支持图表;
4)不支持Unicode UTF-32;
5)中文支持不好;
class BasicExcel
void New(int sheets=3)
|
创建一个新工作薄,默认3张工作表
|
bool Load(const char* filename)
| 载入一个已存在的工作薄文件 |
bool Save()
|
保存当前工作薄到已载入文件
|
bool SaveAs(const char* filename)
|
保存当前工作薄到一个新文件
|
size_t GetTotalWorkSheets()
|
获取当前工作薄的工作表数目
|
BasicExcelWorksheet* GetWorksheet(size_t sheetIndex)
BasicExcelWorksheet* GetWorksheet(const char* name)
BasicExcelWorksheet* GetWorksheet(const wchar_t* name)
|
获取指定索引的工作表对象,索引从0开始,索引无效则返回值为NULL
获取指定名称的工作表对象,名称无效则返回值为NULL
|
BasicExcelWorksheet* AddWorksheet(int sheetIndex=-1)
BasicExcelWorksheet* AddWorksheet(const char* name, int sheetIndex=-1)
BasicExcelWorksheet* AddWorksheet(const wchar_t* name, int sheetIndex=-1)
|
添加指定索引的工作表,名称默认为SheetX,X从1开始,如果sheetIndex==-1,则默认添加到最后一个位置
添加指定名称和索引的工作表,如果sheetIndex==-1,则默认添加到最后一个位置
|
bool DeleteWorksheet(size_t sheetIndex)
bool DeleteWorksheet(const char* name)
bool DeleteWorksheet(const wchar_t* name)
| 删除指定索引或名称的工作表 |
char* GetAnsiSheetName(size_t sheetIndex)
wchar_t* GetUnicodeSheetName(size_t sheetIndex)
bool GetSheetName(size_t sheetIndex, char* name)
bool GetSheetName(size_t sheetIndex, wchar_t* name)
|
获取指定索引的工作表名称
|
bool RenameWorksheet(size_t sheetIndex, const char* to)
bool RenameWorksheet(size_t sheetIndex, const wchar_t* to)
bool RenameWorksheet(const char* from, const char* to)
bool RenameWorksheet(const wchar_t* from, const wchar_t* to)
|
重命名指定索引或名称的工作表
|
class BasicExcelWorksheet
char* GetAnsiSheetName()
wchar_t* GetUnicodeSheetName()
bool GetSheetName(char* name)
bool GetSheetName(wchar_t* name)
|
获取当前工作表的名称
|
bool Rename(const char* to)
bool Rename(const wchar_t* to)
|
重命名当前工作表
|
void Print(ostream& os, char delimiter=',', char textQualifier='\0')
|
输出整张工作表到指定输出流,指定列分隔字符和文本限定符
指定列分隔符为','和文本限定符为'\"',该函数可以用来保存当前工作表为CSV格式
|
size_t GetTotalRows()
|
获取当前工作表的总行数
|
size_t GetTotalCols()
|
获取当前工作表的总列数
|
BasicExcelCell* Cell(size_t row, size_t col)
|
获取指定行、列的单元格对象,行、列值从0开始,如果行值超过65535或者列值超过255则返回NULL
|
bool EraseCell(size_t row, size_t col)
|
清空指定行、列的单元格对象的内容
|
class BasicExcelCell
int Type() const
|
获取单元格值类型,包括以下值:
UNDEFINED ,
INT ,
DOUBLE ,
STRING ,
WSTRING
|
bool Get(int& val) const
bool Get(double& val) const
bool Get(char* str) const
bool Get(wchar_t* str) const
|
从当前单元格获取指定类型的内容
|
size_t GetStringLength()
|
获取当前单元格字符串长度
|
int GetInteger() const
double GetDouble() const
const char* GetString() const
const wchar_t* GetWString() const
|
从当前单元格获取指定类型的内容
|
ostream& operator<<(ostream& os, const BasicExcelCell& cell)
|
输出当前单元格内容到输出流中
|
void Set(int val)
void Set(double val)
void Set(const char* str)
void Set(const wchar_t* str)
| 输出指定格式的内容到当前单元格 |
void SetInteger(int val)
void SetDouble(double val)
void SetString(const char* str)
void SetWString(const wchar_t* str)
|
输出指定格式的内容到当前单元格
|
void EraseContents()
| 清空当前单元格的内容 |
示例代码:在Qt线程中递归遍历文件夹中文件后将文件名和文件大小写入Excel表格
main.cpp
#include <QtCore/QCoreApplication>
#include "mythread.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyThread mythread;
mythread.setCurrentDirectory(QString("C:/Program Files/360"));
mythread.setStart();
return a.exec();
}
mythread.h
#ifndef MY_THREAD_H
#define MY_THREAD_H
#include <QThread>
#include <QMutex>
#include <QWaitCondition>
#include "BasicExcel.h"
using namespace YExcel;
class MyThread : public QThread
{
Q_OBJECT
public:
MyThread();
~MyThread();
void setStart();
void setCurrentDirectory(QString strCurrentDirectory);
void recursiveTraverseDir(QString dirString);
protected:
void run();
private:
void ExportToExcel();
private:
bool bRunning;
QWaitCondition waitRunning;
QMutex mutex;
QString strCurrentDirectory;
QString strFileName;
int iFileSize;
BasicExcel beObject;
BasicExcelWorksheet* bewCurrentSheet;
BasicExcelCell* becCurrentCell;
char strCurrentSheet[8];
int iCurrentRow;
int iSheetIndex;
};
#endif // MY_THREAD_H
mythread.cpp
#include <QDir>
#include <QFileInfo>
#include <Qt>
#include <QtGlobal>
#include <QtCore/qmath.h>
#include "mythread.h"
MyThread::MyThread()
{
bRunning = false;
beObject.New();
bewCurrentSheet = beObject.GetWorksheet("Sheet1");
iCurrentRow = 0;
iSheetIndex = 1;
start();
}
MyThread::~MyThread()
{
wait();
}
void MyThread::setStart()
{
QMutexLocker locker(&mutex);
this->bRunning = true;
waitRunning.wakeOne();
}
void MyThread::setCurrentDirectory(QString strCurrentDirectory)
{
QMutexLocker locker(&mutex);
this->strCurrentDirectory = strCurrentDirectory;
}
void MyThread::run()
{
forever
{
{
QMutexLocker locker(&mutex);
if(!bRunning)
{
waitRunning.wait(&mutex);
}
}
recursiveTraverseDir(strCurrentDirectory);
beObject.SaveAs("example.xls");
{
QMutexLocker locker(&mutex);
if(bRunning)
{
bRunning = false;
}
}
}
}
void MyThread::recursiveTraverseDir(QString dirString)
{
QDir dir(dirString);
if (!dir.exists())
{
return;
}
dir.setFilter(QDir::Dirs | QDir::Files);
dir.setSorting(QDir::DirsFirst);
QFileInfoList fileInfolist = dir.entryInfoList();
int i = 0;
bool bIsDir;
QFileInfo fileInfo;
do{
fileInfo = fileInfolist.at(i);
if(fileInfo.fileName() == "." | fileInfo.fileName() == "..")
{
i++;
continue;
}
bIsDir = fileInfo.isDir();
if (bIsDir)
{
recursiveTraverseDir(fileInfo.filePath());
}
else
{
strFileName = fileInfo.fileName();
iFileSize = qCeil((fileInfo.size()) / 1024);
cout << strFileName.toLatin1().data() << "\t\t" << iFileSize << endl;
ExportToExcel();
}
msleep(50);
i++;
}while(i < fileInfolist.size());
}
void MyThread::ExportToExcel()
{
if(bewCurrentSheet)
{
becCurrentCell = bewCurrentSheet->Cell(iCurrentRow, 0);
becCurrentCell->SetString(strFileName.toLatin1().data());
becCurrentCell = bewCurrentSheet->Cell(iCurrentRow, 1);
becCurrentCell->SetInteger(iFileSize);
iCurrentRow++;
}
}
执行结果:
