一個用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
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 }
執行結果:
