BasicExcel的使用

参考: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
获取单元格值类型,包括以下值:UNDEFINEDINTDOUBLESTRINGWSTRING
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
 1 #include <QtCore/QCoreApplication>
 2 #include "mythread.h"
 3 
 4 int main(int argc, char *argv[])
 5 {
 6     QCoreApplication a(argc, argv);
 7 
 8     MyThread mythread;
 9     mythread.setCurrentDirectory(QString("C:/Program Files/360"));
10     mythread.setStart();
11 
12     return a.exec();
13 }

 

mythread.h

 1 #ifndef MY_THREAD_H
 2 #define MY_THREAD_H
 3 
 4 
 5 #include <QThread>
 6 #include <QMutex>
 7 #include <QWaitCondition>
 8 
 9 #include "BasicExcel.h"
10 
11 using namespace YExcel;
12 
13 class MyThread : public QThread
14 {
15     Q_OBJECT
16 
17 public:
18     MyThread();
19     ~MyThread();
20 
21     void setStart();
22     void setCurrentDirectory(QString strCurrentDirectory);
23     void recursiveTraverseDir(QString dirString);
24 
25 protected:
26     void run();
27 
28 
29 private:
30     void ExportToExcel();
31 
32 private:
33     bool bRunning;
34     QWaitCondition waitRunning;
35     QMutex mutex;
36 
37     QString strCurrentDirectory;
38     QString strFileName;
39     int  iFileSize;
40 
41     BasicExcel                beObject;
42     BasicExcelWorksheet*    bewCurrentSheet;
43     BasicExcelCell*            becCurrentCell;
44     char                    strCurrentSheet[8];
45     int                        iCurrentRow;
46     int                        iSheetIndex;
47 };
48 
49 #endif    // MY_THREAD_H

 

mythread.cpp
  1 #include <QDir>
  2 #include <QFileInfo>
  3 #include <Qt>
  4 #include <QtGlobal>
  5 #include <QtCore/qmath.h>
  6 #include "mythread.h"
  7 
  8 MyThread::MyThread()
  9 {
 10     bRunning = false;
 11 
 12     beObject.New();
 13     bewCurrentSheet = beObject.GetWorksheet("Sheet1");
 14     iCurrentRow = 0;
 15     iSheetIndex = 1;
 16 
 17     start();
 18 }
 19 
 20 MyThread::~MyThread()
 21 {
 22     wait();
 23 }
 24 
 25 void MyThread::setStart()
 26 {
 27     QMutexLocker locker(&mutex);
 28     this->bRunning = true;
 29     waitRunning.wakeOne();
 30 }
 31 
 32 void MyThread::setCurrentDirectory(QString strCurrentDirectory)
 33 {
 34     QMutexLocker locker(&mutex);
 35     this->strCurrentDirectory = strCurrentDirectory;
 36 }
 37 
 38 void MyThread::run()
 39 {
 40     forever
 41     {
 42         {
 43             QMutexLocker locker(&mutex);
 44             if(!bRunning)
 45             {
 46                 waitRunning.wait(&mutex);
 47             }
 48         }
 49 
 50         recursiveTraverseDir(strCurrentDirectory);
 51         beObject.SaveAs("example.xls");
 52 
 53         {
 54             QMutexLocker locker(&mutex);
 55             if(bRunning)
 56             {
 57                 bRunning = false;
 58             }
 59         }
 60     }
 61 
 62 }
 63 
 64 void MyThread::recursiveTraverseDir(QString dirString)
 65 {
 66     QDir dir(dirString);
 67     if (!dir.exists())
 68     {
 69         return;
 70     }
 71 
 72     dir.setFilter(QDir::Dirs | QDir::Files);
 73     dir.setSorting(QDir::DirsFirst);
 74 
 75     QFileInfoList fileInfolist = dir.entryInfoList();
 76 
 77     int i = 0;
 78     bool bIsDir;
 79     QFileInfo fileInfo;
 80 
 81     do{
 82         fileInfo = fileInfolist.at(i);
 83         if(fileInfo.fileName() == "." | fileInfo.fileName() == "..")
 84         {
 85             i++;
 86             continue;
 87         }
 88 
 89         bIsDir = fileInfo.isDir();
 90         if (bIsDir)
 91         {
 92             recursiveTraverseDir(fileInfo.filePath());
 93         }
 94         else
 95         {    
 96             strFileName = fileInfo.fileName();
 97             iFileSize = qCeil((fileInfo.size()) / 1024);
 98 
 99             cout << strFileName.toLatin1().data() << "\t\t" << iFileSize << endl;
100 
101             ExportToExcel();
102         }
103 
104         msleep(50);
105         i++;
106 
107     }while(i < fileInfolist.size());
108 }
109 
110 void MyThread::ExportToExcel()
111 {
112     if(bewCurrentSheet)
113     {
114         becCurrentCell = bewCurrentSheet->Cell(iCurrentRow, 0);
115         becCurrentCell->SetString(strFileName.toLatin1().data());
116 
117         becCurrentCell = bewCurrentSheet->Cell(iCurrentRow, 1);
118         becCurrentCell->SetInteger(iFileSize);
119 
120         iCurrentRow++;
121     }
122 }

执行结果:

 
 
 
posted @ 2014-05-04 00:47  paullam  阅读(7300)  评论(0编辑  收藏  举报