一个Linux/Unix下的excel库“xlslib”的使用及其简单封装

 

下载地址:
http://xlslib.sourceforge.net/

简单封装的类:Excel.h
#ifndef  Excel_h__
#define  Excel_h__
 
#include  <vector>
using  namespace  std ;
 
#include  <xlslib.h>
#include  <extformat.h>
using  namespace  xlslib_core ;
 
///Excel文件操作类
class   CExcel
{
public :
     CExcel ();
     ~CExcel ();
private :
     CExcel ( CExcel& pm_objExcel);
     CExcel & operator=( CExcel& pm_objExcel);
 
private :
    ///Excel 工作薄
    workbook m_WorkBook;
 
    ///Excel 工作表,使用之前必须先创建,参见<sheetrec.h>
     worksheet * m_pWorkSheet;
 
private :
 
    /*
    注意:
    测试发现Excel最多支持同时使用505种字体;
    workbook::font()的每次调用都会创建并添加一个“字体”到excel文件中,所以
    在这里保存了字体信息,使得每个不同的“字体”只创建并添加一次。
 
    最多支持4028种不同的单元格格式;
    workbook::xformat()的每次调用都会创建并添加一个“格式”到excel文件中,所以
    在这里保存了格式信息,使得每个不同的“格式”只创建并添加一次。
    */
 
    /// 字体列表
     vector < font_t*> m_FontPtrs;
    /// 单元格格式列表
     vector < xf_t*> m_FormatPtrs;
     xf_t *  get_xf_t(string& pm_fontName,unsigned16_t pm_fontSize,unsigned16_t pm_fontBoldStyle);
     font_t *  get_font_t(string& pm_fontName,unsigned16_t pm_fontSize,unsigned16_tpm_fontBoldStyle);
 
public :
 
    ///创建一张工作表,注意:这个函数必须首先调用
    void  CreateWorkSheet (const  string& pm_WorkSheetText);
 
    ///保存Excel文件
    void  SaveExcelFile (const  string& pm_FilePath);
 
    ///设置单元格的文本
    void  SetCellText (
         unsigned16_t  pm_row,
         unsigned16_t  pm_col,
        const  string & pm_Text,
         string  pm_FontName = "Arial",
         unsigned16_t  pm_fontSize = 12,
        bool pm_FontBold = false);
 
    ///设置单元格的数字
    void  SetCellNumber (
         unsigned16_t  pm_row,
         unsigned16_t  pm_col,
        double pm_Number,
         string  pm_FontName = "Arial",
         unsigned16_t  pm_fontSize = 12 ,
        bool pm_FontBold = false);
 
    ///合并单元格
    void  MergeCells (
         unsigned16_t  pm_first_row,
         unsigned16_t  pm_first_col,
         unsigned16_t  pm_last_row,
         unsigned16_t  pm_last_col);
 
    /**
    * @brief 设置单元格的宽度,高度
    * 这里有个问题:宽度和高度的单位不一致,需要通过多次设置不同的数据来找出一个合适的值。
    */
    void  SetSize (
         unsigned16_t  pm_row,
         unsigned16_t  pm_col,
         unsigned16_t  pm_row_height,
         unsigned16_t  pm_col_width);
 
    ///设置单元格边框为黑色实线
    void  SetCellBorder (
         unsigned16_t  pm_row,
         unsigned16_t  pm_col);
 
    ///设置单元格的对齐方式
    void  SetCellAlign (
         unsigned16_t  pm_row,
         unsigned16_t  pm_col,
         halign_option_t  pm_ha_option,
         valign_option_t  pm_va_option);
 
    ///设置单元格的背景颜色
    void  SetCellBgColor (
         unsigned16_t  pm_row,
         unsigned16_t  pm_col,
         color_name_t  pm_color);
};
 
#endif  // Excel_h__
测试程序:test_xls.cpp
/// c
#include  <string.h>
#include  <stdlib.h>
#include  <assert.h>
 
/// unix
#include  <sys/stat.h>
#include  <signal.h> //sigset
#include  <unistd.h> //sleep,access
#include  <dirent.h> //DIR
 
/// stl
#include  <string>
#include  <iostream>
#include  <vector>
using  namespace  std ;
 
#include  "Excel.h"
 
int  main(int, char **, char **)
{
    CExcel objExcel;
    objExcel.CreateWorkSheet("New Sheet1");
 
    objExcel.MergeCells(0,0,0,3);
    objExcel. SetCellText (0,0,"Test xlslib .","Microsoft Sans Serif",16,true);
    objExcel. SetCellAlign (0,0,HALIGN_CENTER,VALIGN_CENTER);
    objExcel. SetSize (0,0,20,20000);
    objExcel. SetCellBgColor (0,0,CLR_GRAY50);
    objExcel. SetCellBorder (0,0);
    objExcel. SetCellBorder (0,1);
    objExcel. SetCellBorder (0,2);
    objExcel. SetCellBorder (0,3);
 
    for (int i=1;i<20;i++)
    {
        objExcel. SetCellText (i,0,"Arial Black","Arial Black",12,true);
        objExcel. SetSize (i,0,100,5500);
        objExcel. SetCellAlign (i,0,HALIGN_GENERAL,VALIGN_CENTER);
        objExcel. SetCellBgColor (i,0,CLR_TEAL);
 
        objExcel. SetCellText (i,1,"Courier New","Courier New",14,true);
        objExcel. SetSize (i,1,100,5500);
 
        objExcel. SetCellText (i,2,"Default");
 
        objExcel. SetCellNumber (i,3,i*10,"Dotum");
        objExcel. SetCellBgColor (i,3,CLR_PINK);
        objExcel. SetCellBorder (i,3);
    }
 
    objExcel. Save ExcelFile( "test_xls.xls");
 
    return 0;
}
以上程序在HP-UX上测试通过。
完整的源码点这里下载(注意:将makefile中的路径根据实际情况进行修改)。

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值