找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 3338|回復(fù): 1
收起左側(cè)

libxl-3.1.0下載 可操作excel,不依賴excel安裝環(huán)境

[復(fù)制鏈接]
ID:230393 發(fā)表于 2017-9-1 12:22 | 顯示全部樓層 |閱讀模式
我們常用ole方式操作excel,需要安裝excel,并且如果訪問(wèn)越界會(huì)runtime error。
采用libxl操作excel,不依賴excel安裝環(huán)境。直接讀取excel,并且不會(huì)出現(xiàn)runtime error。非常方便。
0.png

所有資料51hei提供下載:
libxl-3.1(破解版).rar (3.27 MB, 下載次數(shù): 15)



源程序如下:
  1. #include <iostream>
  2. #include <libxl.h>
  3. #include <Windows.h>

  4. using namespace std;
  5. using namespace libxl;

  6. //中文的內(nèi)容讀出來(lái)后要進(jìn)行編碼的轉(zhuǎn)換,這個(gè)為轉(zhuǎn)換函數(shù):wchar_t to char
  7. char *w2c(char *pcstr,const wchar_t *pwstr, size_t len)

  8. {
  9.         int nlength=wcslen(pwstr);
  10.         //獲取轉(zhuǎn)換后的長(zhǎng)度
  11.         int nbytes = WideCharToMultiByte( 0, 0, pwstr, nlength, NULL,0,NULL, NULL );
  12.         if(nbytes>len)   nbytes=len;
  13.         // 通過(guò)以上得到的結(jié)果,轉(zhuǎn)換unicode 字符為ascii 字符
  14.         WideCharToMultiByte( 0,0, pwstr, nlength,   pcstr, nbytes, NULL,   NULL );
  15.         return pcstr ;
  16. }

  17. int main(int argc, char* argv[])
  18. {
  19.         Book* book = xlCreateXMLBook();
  20.         if(book->load(L"1.xlsx")){
  21.                 Sheet * sheet = book->getSheet(0);
  22.                 if(sheet){
  23.                         CellType celltype = sheet->cellType(1,1);
  24.                         cout<<"the type is:"<<celltype<<endl;

  25.             const wchar_t * t = sheet->readStr(1,1);
  26.                         char *pcstr = (char *)malloc(sizeof(char)*(2 * wcslen(t)+1));
  27.                         memset(pcstr , 0 , 2 * wcslen(t)+1 );
  28.                         w2c(pcstr,t,2 * wcslen(t)+1) ;
  29.                         cout<<"result:"<<pcstr<<endl;
  30.                         free(pcstr);
  31.                 }
  32.         }
  33.         system("pause");
  34.         return 0;
  35. }
復(fù)制代碼





回復(fù)

使用道具 舉報(bào)

ID:232829 發(fā)表于 2017-9-13 10:53 | 顯示全部樓層
沒有用過(guò)不知道好不好用。
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表