先到 http://www.libxl.com/ 下载linux版本

我下载的是libxl-lin-3.8.4.tar.gz

将压缩包移到/usr/local下,解压缩后,文件夹改名为libxl,进入可以看到几个文件夹

-rw-r--r-- 1 hao hao 38852 Dec 18 04:54 changelog.txt
drwxr-xr-x 3 hao hao   229 Dec 18 04:54 doc
drwxr-xr-x 7 hao hao    69 Dec 18 04:54 examples
drwxr-xr-x 2 hao hao   280 Dec 18 04:54 include_c
drwxr-xr-x 2 hao hao   163 Dec 18 04:54 include_cpp
drwxr-xr-x 2 hao hao    22 Dec 18 04:54 lib
drwxr-xr-x 2 hao hao    22 Dec 18 04:54 lib64
-rw-r--r-- 1 hao hao  8376 Dec 18 04:54 license.txt
-rw-r--r-- 1 hao hao   748 Dec 18 04:54 readme.txt

我在/usr/local/include/ 下建立一个文件夹libxl,并将上面 include_cpp的全部档案复制到/usr/local/include/libxl

所以最终我的头文件的位置/usr/local/include/libxl/libxl.h

Lib的位置/usr/local/libxl/lib64

然后进入上述examples的文件夹,里面的c++文件夹,有6个范例,我们用invoice的为例

路径设置在上面readme的文件里写:

  g++ -I <path_to_headers> -L <path_to_library> -lxl -Wl,-rpath,<path_to_library> <your_cpp_file>

参考readme的指引,我们打开vs,加入-I /usr/local/include/libxl -L /usr/local/libxl/lib64 -lxl -Wl,-rpath,/usr/local/libxl/lib64

 

把invoince的贴到项目里,导入头文件

#include <iostream>
#include <libxl/libxl.h>

using namespace libxl;

int main()
{
    Book* book = xlCreateXMLBook();
    if(book) 
    {   
        Font* boldFont = book->addFont();
        boldFont->setBold();

        Font* titleFont = book->addFont();
        titleFont->setName("Arial Black");
        titleFont->setSize(16);

        Format* titleFormat = book->addFormat();
        titleFormat->setFont(titleFont);

        Format* headerFormat = book->addFormat();
        headerFormat->setAlignH(ALIGNH_CENTER);
        headerFormat->setBorder(BORDERSTYLE_THIN);
        headerFormat->setFont(boldFont);        
        headerFormat->setFillPattern(FILLPATTERN_SOLID);
        headerFormat->setPatternForegroundColor(COLOR_TAN);

        Format* descriptionFormat = book->addFormat();
        descriptionFormat->setBorderLeft(BORDERSTYLE_THIN);

        Format* amountFormat = book->addFormat();
        amountFormat->setNumFormat(NUMFORMAT_CURRENCY_NEGBRA);
        amountFormat->setBorderLeft(BORDERSTYLE_THIN);
        amountFormat->setBorderRight(BORDERSTYLE_THIN);
                
        Format* totalLabelFormat = book->addFormat();
        totalLabelFormat->setBorderTop(BORDERSTYLE_THIN);
        totalLabelFormat->setAlignH(ALIGNH_RIGHT);
        totalLabelFormat->setFont(boldFont);

        Format* totalFormat = book->addFormat();
        totalFormat->setNumFormat(NUMFORMAT_CURRENCY_NEGBRA);
        totalFormat->setBorder(BORDERSTYLE_THIN);
        totalFormat->setFont(boldFont);
        totalFormat->setFillPattern(FILLPATTERN_SOLID);
        totalFormat->setPatternForegroundColor(COLOR_YELLOW);

        Format* signatureFormat = book->addFormat();
        signatureFormat->setAlignH(ALIGNH_CENTER);
        signatureFormat->setBorderTop(BORDERSTYLE_THIN);
             
        Sheet* sheet = book->addSheet("Invoice");
        if(sheet)
        {
            sheet->writeStr(2, 1, "Invoice No. 3568", titleFormat);

            sheet->writeStr(4, 1, "Name: John Smith");
            sheet->writeStr(5, 1, "Address: San Ramon, CA 94583 USA");

            sheet->writeStr(7, 1, "Description", headerFormat);
            sheet->writeStr(7, 2, "Amount", headerFormat);

            sheet->writeStr(8, 1, "Ball-Point Pens", descriptionFormat);
            sheet->writeNum(8, 2, 85, amountFormat);
            sheet->writeStr(9, 1, "T-Shirts", descriptionFormat);
            sheet->writeNum(9, 2, 150, amountFormat);
            sheet->writeStr(10, 1, "Tea cups", descriptionFormat);
            sheet->writeNum(10, 2, 45, amountFormat);

            sheet->writeStr(11, 1, "Total:", totalLabelFormat);
            sheet->writeFormula(11, 2, "=SUM(C9:C11)", totalFormat);

            sheet->writeStr(14, 2, "Signature", signatureFormat);

            sheet->setCol(1, 1, 40);
            sheet->setCol(2, 2, 15);
          }

          if(book->save("invoice.xlsx")) {
              std::cout << "File invoice.xlsx has been created." << std::endl;
          }
          book->release();   
    }

    return 0;
}

运行后得到

 

Excel文件的位置可以用命令查看

find / -name invoice.xlsx

 

 

 

 

Logo

更多推荐