我使用C++语言编写的导出Excel文件的代码。
功能:
1:可生成Xls的Excel二进制文件,但不包括最新的2007格式(此格式正在编写学习中)。
2:生成字符串、数字、日期等格式的单元格,可以自由设置格式。
3:自由嵌入多个图片(目前不需要也不想支持线型、文本框等图型元素)。
4:自由合并单元格,设置列宽和行高(以像素为单位)。
5:可同时生成多个命名的Sheet。
特点:
1:快速。
2:体积小且格式正确,未发现生成的文件有任何异常。
3:C++语言编写的。(2011-02-26)
主要用途:
目的是主要应用于在项目中生成Excel,比如在报表中导出Excel等,所以不包括像公式这样的在导出时用不到的功能。
代码分为头文件和Cpp文件,但不包括里面用到的一个自定义类CTArray,这个类是我自己实现的。看情况如果需要再贴出。
ExcelWorkBook1.h:
ExcelWorkBook1.cpp:
template<typename T, int I=256, bool Auto=false>
class CTArray
{
public:
CTArray(void)
: m_Array(NULL)
, m_Count(0)
, m_MaxSize(I)
, m_AutoSize(Auto)
{
this->m_Array=new T[this->m_MaxSize];
/*尊重元素的构造函数,不能强制的都初始化为0值
memset(this->m_Array, 0, sizeof(T)*this->m_MaxSize);
*/
}
CTArray(CTArray& src)
{
this->m_MaxSize=src.m_Count;//Copy合适的大小
this->m_Count=src.m_Count;
this->m_AutoSize=src.m_AutoSize;
this->m_Array=new T[src.m_Count];
memcpy_s(this->m_Array, sizeof(T)*src.m_Count, src.m_Array, sizeof(T)*src.m_Count);
}
~CTArray(void)
{
delete[] this->m_Array;
}
int Count(void)
{
return this->m_Count;
}
void RemoveAll(void)
{
this->m_Count=0;
}
void DeleteAll(void)
{
for(int i=0;i<this->m_Count;i++)
{
delete *(this->m_Array+i);
}
this->m_Count=0;
}
void Zero()
{
memset(this->m_Array, 0, sizeof(T)*this->m_MaxSize);
}
CTArray& operator=(CTArray& src)
{
this->m_MaxSize=src.m_Count;//Copy合适的大小
this->m_Count=src.m_Count;
this->m_AutoSize=src.m_AutoSize;
this->m_Array=new T[src.m_Count];
memcpy_s(this->m_Array, sizeof(T)*src.m_Count, src.m_Array, sizeof(T)*src.m_Count);
return *this;
}
T& operator[](int index)
{
if(m_AutoSize)
{
this->EnsureSize(index+1);
}
return this->m_Array[index];
}
T* GetAt(int index)
{
if (m_AutoSize)
{
this->EnsureSize(index+1);
}
return this->m_Array+index;
}
int Add(T value)
{
this->EnsureSize();
this->m_Array[this->m_Count]= value;
return this->m_Count++;
}
int Add(T value, int length)
{
this->EnsureSize();
memcpy(this->m_Array[this->m_Count++], value, length*(sizeof value[0]));
return this->m_Count;
}
int Insert(int index, T value)
{
this->EnsureSize();
for(int i=this->m_Count-1;i>index-1;i--)
{
this->m_Array[i+1]=this->m_Array[i];
}
this->m_Array[index]=value;
return ++this->m_Count;
}
int Insert(int index, T value, int length)
{
this->EnsureSize();
for(int i=this->m_Count-1;i>index-1;i--)
{
memcpy(this->m_Array[i+1], this->m_Array[i], sizeof this->m_Array[i]);
}
memcpy(this->m_Array[index], value, length*(sizeof value[0]));
return ++this->m_Count;
}
void EnsureSize(int size=0)
{
//int count=this->m_Count;
if (size>0 && this->m_Count<size)
{
this->m_Count=size;
}
if (this->m_Count<this->m_MaxSize)
{
/*尊重元素的构造函数,不能强制的都初始化为0值
for(int i=count;i<this->m_Count;i++)
{
memset(this->m_Array+count, 0, sizeof(T)*(this->m_Count-count));
}
*/
return;
}
this->m_MaxSize=this->m_Count+I;
T* tmpArray=new T[this->m_MaxSize];
memcpy(tmpArray, this->m_Array, this->m_Count*sizeof(T));
/*尊重元素的构造函数,不能强制的都初始化为0值
memset(tmpArray+count, 0, sizeof(T)*(this->m_MaxSize-count));
*/
delete[] this->m_Array;
this->m_Array=tmpArray;
}
void Sort()
{
CTArray::quickSort(this->m_Array, 0, this->m_Count-1);
}
T* Array()
{
return this->m_Array;
}
void static quickSort(T* tArray, int left, int right)
{
do
{
int lindex = left;
int rindex = right;
int m = (lindex + rindex) >> 1;
T mv = tArray[m];
do
{
while (tArray[lindex]<mv)
{
lindex++;
}
while (mv<tArray[rindex])
{
rindex--;
}
if (lindex > rindex)
{
break;
}
if (lindex < rindex)
{
T lv = tArray[lindex];
tArray[lindex] = tArray[rindex];
tArray[rindex] = lv;
}
lindex++;
rindex--;
}
while (lindex <= rindex);
if ((rindex - left) <= (right - lindex))
{
if (left < rindex)
{
quickSort(tArray, left, rindex);
}
left = lindex;
}
else
{
if (lindex < right)
{
quickSort(tArray, lindex, right);
}
right = rindex;
}
}
while (left < right);
}
public:
T* m_Array;
int m_Count;
bool m_AutoSize;
private:
int m_MaxSize;
};
欢迎给出意见!2011-02-26 09:58:49