Linux libxls和xlslib讀寫Excel文檔【轉】

轉自:https://www.joxrays.com/linux-xls/

這裏要討論的是兩款開源庫 libxlsxlslib,前者用與 Excel,後者用於 .所以可以在Linux或Windows上使用

libxls(讀Excel

獲取libxls

可以在 官網 獲取源代碼, 目前最新版爲 1.4.0

unzip libxls-1.4.0.zip
cd libxls
./configure
make
make install

注意默認安裝到 /usr/local 目錄下,所以需要自己手動複製相關文件到系統目錄下

cp -r /usr/local/libxls/include/* /usr/include
cp -r /usr/local/libxls/lib/* /usr

至此基本上可以了

例子

test.cpp

#include <iostream>
#include <xls.h>
using namespace std;
using namespace xls;
int main(int argc,char *argv[]) {
    //版本信息
    cout<<"version:"<<xls::xls_getVersion()<<endl;
    xlsWorkBook *pwb;
    xlsWorkSheet *pws;
    //打開一個excel表格
    pwb=xls_open("test.xls","UTF-8");
    if(!pwb){
        cout<<"open failed!"<<endl;
        exit(EXIT_SUCCESS);
    }
    xlsSummaryInfo *summaryInfo=xls_summaryInfo(pwb);
    if(summaryInfo){
        if(summaryInfo->title)cout<<"title:"<<summaryInfo->title<<endl;
        if(summaryInfo->manager)cout<<"manager:"<<summaryInfo->manager<<endl;
        if(summaryInfo->lastAuthor)cout<<"lastAuthor:"<<summaryInfo->lastAuthor<<endl;
        if(summaryInfo->keywords)cout<<"keywords:"<<summaryInfo->keywords<<endl;
        if(summaryInfo->company)cout<<"company:"<<summaryInfo->company<<endl;
        if(summaryInfo->comment)cout<<"comment:"<<summaryInfo->comment<<endl;
        if(summaryInfo->appName)cout<<"appName:"<<summaryInfo->appName<<endl;
        if(summaryInfo->subject)cout<<"subject:"<<summaryInfo->subject<<endl;
    }
    cout<<"charset:"<<pwb->charset<<endl;
    cout<<"font name:"<<pwb->fonts.font->name<<endl;
    cout<<"font bold:"<<pwb->fonts.font->bold<<endl;
    cout<<"當前表名:"<<pwb->sheets.sheet->name<<endl;
    cout<<"總表數:"<<pwb->sheets.count<<endl<<endl;
    //獲取第一張表單 索引從0開始
    pws= xls_getWorkSheet(pwb,0);
    //開始解析表單
    xls_parseWorkSheet(pws);
    cout<<"行數:"<<pws->rows.lastrow+1<<endl;
    cout<<"列數:"<<pws->rows.lastcol<<endl;
    //遍歷
    for (int i = 0; i < pws->rows.lastrow+1; ++i) {
        //xlsRow = st_row::st_row_data
        //獲取每一行的數據
        st_row::st_row_data row= pws->rows.row[i];
        for (int j = 0; j < pws->rows.lastcol; ++j) {
            if(row.cells.cell[j].str) {
                cout << (char*)row.cells.cell[j].str << "\t";
            }
        }
        cout<<endl;
    }
    //關閉
    xls_close_WS(pws);
    xls_close_WB(pwb);
    
    return 0;
}

 

 

   

注意,由於我使用 clion 編寫代碼,所以在鏈接庫時在 CMakeLists.txt 最後一行添加 target_link_libraries(libxls_read libxlsreader.so) 其中 libxls_read 爲項目名.
或者直接 g++ test.cpp -o libxls_read -lxlsreader

測試:
假設有一個Excel文件
img

img

 

執行程序輸出

version:1.4.0
標題:我是標題
keywords:我是關鍵字
comment:這是一個測試
subject:我是主題
charset:UTF-8
font name:文泉驛正黑
font bold:400
當前表名:第一張表單
總表數:1

行數:4
列數:5
劉備 關羽 張飛 曹操 劉禪
曹丕 趙雲 孫權 黃蓋 曹植
貂蟬 張角 孔明 周瑜 小喬
馬超

xlslib(寫Excel

獲取xlslib

可以從 http://sourceforge.net/projects/xlslib/ 獲取 xlslib並編譯安裝,同樣的默認也是安裝到 /usr/local/include ,需手動複製到系統目錄下

cp -r /usr/local/include/xlslib /usr/include/

例子

#include <iostream>
#include <xlslib/xlslib.h>
using namespace xlslib_core;
using namespace std;
int main() {
    workbook wb;
    string label;
    printf("Version: %s\n",wb.version());
    //設置字體
    font_t *t=wb.font("Ubuntu");
    t->SetColor(CLR_RED);
    t->SetItalic(true);
    t->SetHeight(20*15);
   // t->SetBoldStyle(BOLDNESS_BOLD);
    xf_t *xf= wb.xformat();
    xf->SetFillBGColor(CLR_GREEN);
    xf->SetFillFGColor(CLR_RED);
    xf->SetFont(t);
    //第一張表單
    worksheet *ws=wb.sheet("one");
    //第二張表單
    worksheet* ws2=wb.sheet("two");
    ws->MakeActive();
    //設置列寬度,行高度
    ws->defaultColwidth(15);
    ws->defaultRowHeight(25);
    //6行
    for (int i = 0; i < 6; ++i) {
        //6列
        for (int j = 0; j <6 ; ++j) {
            char buf[20]={0};
            sprintf(buf,"%d",i*j);
            label=buf;
            // 寫入
            cell_t * cell= ws->label(i,j,label,xf);
            //或者 ws->number(i,j,i*j,xf);
            //設置字體陰影
            cell->fontshadow(true);
            //設置單元格文本對齊
            cell->halign(HALIGN_CENTER);
            cell->valign(VALIGN_CENTER);
            //設置行高度
            ws->rowheight(i,20*15);
        }
    }
    range *ran=ws->rangegroup(1,1,1,1);
    ran->cellcolor(CLR_GREEN);
    //保存到excel文件
    wb.Dump("mynew.xls");
    return 0;
}

 

   
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章