下载地址:
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_t pm_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.SaveExcelFile("test_xls.xls");
 
    return 0;
}
以上程序在HP-UX 11.31上测试通过,其中xlslib的版本为1.6.0。
HP C++的aCC不支持语法 --m_DefaultFonts.end(),类似这样的都可以改为:
Font_Vect_Itor_t ftemp = m_DefaultFonts.end();
ftemp--;
if(font_dflt != ftemp)
// ...
完整的源码点这里下载(注意:将makefile中的路径根据实际情况进行修改)。

-
-----------------------------------------[2014-12-25更新] ------------------------------------------
关于中文乱码问题:
1.修改
globalrec.cpp,将wide2str16函数改为(注意:本文中使用的xlslib未定义HAVE_ICONV)
void  CGlobalRecords::wide2str16(const ustring& str1, u16string& str2)
{
	str2.append((unsigned16_t *)str1.c_str());
}

2.将Excel.cpp中使用的api改为wstring版(可用iconv转换GBK为UTF-16LE/UTF-16BE,见iconv的基本使用方法)。


本文链接地址: 一个Linux/Unix下的excel库“xlslib”的使用及其简单封装
https://blog.qingfengju.com/index.asp?id=237

分类:Linux 查看次数:15980 发布时间:2010/9/28 18:52:04