1. 程式人生 > >linux 上使用libxls讀和使用xlslib寫excel的方法簡介

linux 上使用libxls讀和使用xlslib寫excel的方法簡介

lease stc sbin sin 1.4 讀取 iostream r++ bsp

讀取excel文件:libxls-1.4.0.zip
下載地址:http://sourceforge.net/projects/libxls/
安裝方法:
  ./configure
  make
  make install

//示例:

建立文件readXls.cpp, 代碼如下:

#include <stdexcept>
#include <xls.h>
#include <iostream>
using namespace xls;
using namespace std;
/////////////////////////////////////////////////
int main(int argc, char **argv)
{
  if (argc < 2)
  {
    cerr << "please input the excel file." << endl;
    return 1;
  }

  xlsWorkBook* pWorkBook = xls_open(argv[1], "UTF-8");
  if (NULL == pWorkBook)
  {
    cerr << "file is not excel" << endl;
    return 1;
  }

  xlsWorkSheet* pWorkSheet = xls_getWorkSheet(pWorkBook, 0);
  xls_parseWorkSheet(pWorkSheet);

  for (int r=0; r<=pWorkSheet->rows.lastrow; r++)
  {
    xlsRow* row = &pWorkSheet->rows.row[r];
    for (int c=0; c<pWorkSheet->rows.lastcol; c++)
    {
      BYTE* pCurCellInfo = row->cells.cell[c].str;
      if (NULL != pCurCellInfo)
      {
        cout << pCurCellInfo;
        getchar();
      }
    }
    cout << endl;
  }

  xls_close_WS(pWorkSheet);
  xls_close_WB(pWorkBook);

  return 0;
}

編譯:g++ readXls.cpp -o readXls -I/usr/local/libxls/include -L/usr/local/libxls/lib -lxlsreader

如果運行時出現:error while loading shared libraries: libxxx.so.1: cannot open shared

這表示:系統不知道xxx.so放在哪個目錄下,這時候就要在/etc/ld.so.conf中加入xxx.so所在的目錄

解決方法:

  1.在/etc/ld.so.conf中加入【動態庫所在路勁】,保存之後,再運行:/sbin/ldconfig –v更新一下配置即可。如libxls的動態庫路徑是 /usr/local/libxsl/lib
  2.在/etc/ld.so.conf.d/下新建一個.conf文件,並在其中加入【動態庫所在路勁】就可以了,在運行/sbin/ldconfig。

參考文檔:

   http://blog.csdn.net/yao_guet/article/details/7326065

  http://blog.csdn.net/zhangqiu1989/article/details/8822853

api文檔可參考:

  http://www.codeweblog.com/libxls%E4%BD%BF%E7%94%A8/

/////////////////////////////////////////////////////////
生成excel文件:xlslib-package-2.5.0.zip
下載地址:http://sourceforge.net/projects/xlslib/
安裝方法:
  ./configure
  make
  make install

示例:

建立文件writeXls.cpp,寫入如下代碼:
#include <string.h>
#include <xlslib/xlslib.h>

using namespace xlslib_core;
using namespace std;

int main (int argc, char *argv[])
{
  workbook wb;
  xf_t* xf = wb.xformat();
  worksheet* ws;
  ws = wb.sheet("sheet1");
  string label = "Hello, World!";
  ws->label(1,2,label,xf); // 從0開始數,第1行,第2列,即C3
  wb.Dump("workbook.xls");
  return 0;
}

編譯:
  g++ writeXls.cpp -lxls -I /usr/local/include/xlslib/ -I /usr/local/include/ -L /usr/local/lib/ -o writeXls

參考文檔:http://ju.outofmemory.cn/entry/106483

聲明:此博客都是參考別人的,為了自己方便使用,總結了一下!謝謝各位博主

linux 上使用libxls讀和使用xlslib寫excel的方法簡介