2013年12月2日 星期一

[C++]LibXL:操控Excel

download libray

  • LibXL for Windows 3.5.3 : http://www.libxl.com/download/libxl.zip

Using library

  • 解壓縮後整包放到C槽

建立在Microsoft Visual C++環境下

  • add include directory in your project, for example: c:\libxl\include_cpp

          Project -> Properties -> C/C++ -> General -> Additional Include Directories






















  • add library directory in your project, for example: c:\libxl\lib
          Project -> Properties -> Linker -> General -> Additional Library Directories


















  













  • add libxl.lib in project dependencies:
         Project -> Properties -> Linker -> Input -> Additional Dependencies




















  • copy bin\libxl.dll to directory of your project

使用前先引入標頭檔與命名空間
  • #include "libxl.h"
  • using namespace libxl;

建立一個excel物件叫book
  • Book* book = xlCreateBook();
如果建立book成功的話,在book內新建一個分頁物件叫sheet
  • if(book)
  • Sheet* sheet = book->addSheet(L"Sheet1");
如果建立sheet成功在sheet內寫入字串或數字
  • if(sheet)
  • sheet->writeStr(2, 1, L"Hello, World !"); //寫在excel的3,b
  • sheet->writeNum(3, 1, 1000); //寫在excel的4,b
  • col and row參數都比實際excel位置 都會少1
寫完了要存檔成叫做example.xls的excel檔案

存檔完關閉
  • book->release();
讀取一個excel檔(如果讀取成功)

讀取excel內的第一的分頁
  • Sheet* sheet = book->getSheet(0);
讀取某欄位的字串或數字
  • const wchar_t* s = sheet->readStr(2, 1);
if(s) wcout << s << endl;
  • double d = sheet->readNum(3, 1);
cout << d << endl;




網站:http://www.libxl.com/home.html



沒有留言:

張貼留言