一个Linux/Unix下的excel库“xlslib”的使用及其简单封装
2011-02-26 21:05
483 查看
下载地址:
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.SaveExcelFile("test_xls.xls");
return 0;
}
以上程序在HP-UX上测试通过。
完整的源码点
这里下载(注意:将makefile中的路径根据实际情况进行修改)。
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.SaveExcelFile("test_xls.xls");
return 0;
}
以上程序在HP-UX上测试通过。
完整的源码点
这里下载(注意:将makefile中的路径根据实际情况进行修改)。
相关文章推荐
- 俺使用的C语言面向对象范式(前言与第一节:一个简单类的封装)
- 一个及其简单的卸载linux的方法
- Linux中ifreq 结构体分析和使用 及其在项目中的简单应用
- RHCE基础指南:一个及其简单的卸载linux的方法
- Java日志 - log4简单使用实例及其再次封装
- 由一个简单需求到Linux环境下的syslog、unix domain socket
- Unix中常用的编译系统及其简单的使用
- Linux操作系统的简单指令及如何使用vim编写一个程序,然后使用gcc查看【预处理】、【编译】、【汇编】、【链接】各阶段文件的内容。
- 简单的封装了一个OKHttp网络请求工具类,直接粘贴进去调用使用,很方便
- 先简单写一个,Linux下man命令使用的一些东东
- Linux下,使用C/C++一个简单的消息处理程序
- BerkeleyDB使用的一个简单封装
- 一个简单的linux下原生socket的tcp程序及其修改
- Linux下,使用C/C++编写"静态链接库"的一个简单例子
- Linux下Socket的简单使用及最简化封装
- 刚做了一个开源工具:使用UI来管理Linux和Unix机器--SSHConnector
- 在linux环境下安装Memcached及其简单使用+相关文档
- Linux中ifreq 结构体分析和使用 及其在项目中的简单应用
- Linux下,使用C/C++编写的一个简单的信号处理例程
- python使用线程封装的一个简单定时器类实例