一个Linux/Unix下的excel库“xlslib”的使用及其简单封装
来源:互联网 发布:淘宝上哪家书店比较好 编辑:程序博客网 时间:2024/05/17 22:49
下载地址:
http://xlslib.sourceforge.net/
简单封装的类:Excel.h
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中的路径根据实际情况进行修改)。
完整的源码点这里下载(注意:将makefile中的路径根据实际情况进行修改)。
- 一个Linux/Unix下的excel库“xlslib”的使用及其简单封装
- Linux/Unix下使用rsync同步文件(简单的例子)
- Linux下Socket的简单使用及最简化封装
- Linux下Socket的简单使用及最简化封装
- Unix中常用的编译系统及其简单的使用
- linux下c++使用ncurses库的一个简单实例
- ExcelUtil,一个简单封装的excel解析和生成类
- java 导出excel封装了一个简单的导出方法。
- BerkeleyDB使用的一个简单封装
- 由一个简单需求到Linux环境下的syslog、unix domain socket
- libxls 与 xlslib的结合
- 一个简单的linux下原生socket的tcp程序及其修改
- linux下使用管道实现一个简单的服务器/客户端
- unix/linux socket read函数的封装
- unix/linux socket write函数的封装
- 一个及其简单的卸载linux的方法
- 一个及其简单的卸载linux的方法
- 一个及其简单的卸载linux的方法
- JAVA从头学-笔记一
- OpenGL编程指南
- Windows Azure Training Kit中"Introduction to Windows Azure"动手实验出现 System.Data.Services.Client.DataServiceClientException异常和Resour
- 查询Oracle数据库表空间和数据文件方法
- 互联网软件开发和企业软件开发
- 一个Linux/Unix下的excel库“xlslib”的使用及其简单封装
- 宏(preprocessor)系列 chips
- 可编辑的TableView
- 报表制作工具定时器配置简要说明
- pku 3786 DP
- jsp页面中文乱码的解决方法
- 泛洪攻击的几种方法解析
- HTTP协议基础
- 关于jsp网站的几点总结