📄 excelcf.cpp
字号:
// ExcelCF.cpp: implementation of the ExcelCF class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ExcelCF.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
unsigned short XLSBOF[5] = {0x409, 6, 0, 0x10, 0};
unsigned short XLSGUTS[6] = {0x80, 8, 0, 0, 0, 0};
unsigned short XLSPalette[3] = {0x92, 54, 13};
unsigned char XLSFONT[10] = {0x31, 2, 0, 0, 0, 0, 0, 0, 0, 0};
unsigned char XLSFONTH[10] = {0x31, 2, 0, 0, 0, 0, 0, 0, 8, 0};
unsigned char XLSFONTG[10] = {0x31, 2, 0, 0, 0, 0, 0, 0, 10, 0};
unsigned char XLSSFont[16]={0x31, 2, 0x0C, 0, 0xC8, 0, 0, 0, 0xFF, 0x7F, 5, 0x41, 0x72, 0x69, 0x61, 0x6C};
unsigned char XLSXF1[16]={0x43, 4, 0x0C, 0, 0, 0, 0xF5, 0xFF, 0x20, 0, 0, 0xCE, 0, 0, 0, 0};
unsigned char XLSXF2[16]={0x43, 4, 0x0C, 0, 1, 0, 0xF5, 0xFF, 0x20, 0xF4, 0, 0xCE, 0, 0, 0, 0};
unsigned char XLSXF3[16]={0x43, 4, 0x0C, 0, 2, 0, 0xF5, 0xFF, 0x20, 0xF4, 0, 0xCE, 0, 0, 0, 0};
unsigned char XLSXF4[16]={0x43, 4, 0x0C, 0, 0, 0, 0xF5, 0xFF, 0x20, 0xF4, 0, 0xCE, 0, 0, 0, 0};
unsigned char XLSXF5[16]={0x43, 4, 0x0C, 0, 0, 0, 1, 0, 0x20, 0, 0, 0xCE, 0, 0, 0, 0};
unsigned char XLSXF6[16]={0x43, 4, 0x0C, 0, 1, 0x21, 0xF5, 0xFF, 0x20, 0xF8, 0, 0xCE, 0, 0, 0, 0};
unsigned char XLSXF7[16]={0x43, 4, 0x0C, 0, 1, 0x1F, 0xF5, 0xFF, 0x20, 0xF8, 0, 0xCE, 0, 0, 0, 0};
unsigned char XLSXF8[16]={0x43, 4, 0x0C, 0, 1, 0x20, 0xF5, 0xFF, 0x20, 0xF8, 0, 0xCE, 0, 0, 0, 0};
unsigned char XLSXF9[16]={0x43, 4, 0x0C, 0, 1, 0x1E, 0xF5, 0xFF, 0x20, 0xF8, 0, 0xCE, 0, 0, 0, 0};
unsigned char XLSXF10[16]={0x43, 4, 0x0C, 0, 1, 0x0D, 0xF5, 0xFF, 0x20, 0xF8, 0, 0xCE, 0, 0, 0, 0};
unsigned char XLSXF[16] = {0x43, 4, 0x0C, 0, 5, 0, 1, 0, 0x21, 0x78, 0x41, 3, 0, 0, 0, 0};
unsigned char XLSXFB[16] = {0x43, 4, 0x0C, 0, 8, 0, 1, 0, 0x22, 0x78, 0x41, 2, 0x71, 0x71, 0x71, 0};
unsigned char XLSXm_Filehandle[16] = {0x43, 4, 0x0C, 0, 6, 0, 1, 0, 0x22, 0x78, 0x41, 2, 0x71, 0x71, 0x71, 0x71};
unsigned char XLSXFG[16] = {0x43, 4, 0x0C, 0, 7, 0, 1, 0, 0x21, 0x78, 0xC1, 2, 0, 0, 0, 0};
unsigned char XLSXFF[16] = {0x43, 4, 0x0C, 0, 5, 0, 1, 0, 0x22, 0x78, 0xC1, 2, 0xB9, 0xB9, 0xB9, 0xB9};
unsigned char XLSXFF1[16] = {0x43, 4, 0x0C, 0, 5, 0, 1, 0, 0x22, 0x78, 0xC1, 2, 0xB9, 0, 0xB9, 0};
unsigned char XLSXFRF[16] = {0x43, 4, 0x0C, 0, 9, 0, 1, 0, 0x21, 0x78, 0xC1, 2, 0xB9, 0xB9, 0xB9, 0xB9};
unsigned char XLSXFRF1[16] = {0x43, 4, 0x0C, 0, 9, 0, 1, 0, 0x21, 0x78, 0xC1, 2, 0xB9, 0, 0xB9, 0};
unsigned short XLSDimension[7] = {0x200, 0x0A, 0, 0xFFFF, 0, 0xFF, 0};
unsigned short XLSCOL[8] = {0x7D, 0x0C, 0, 0, 0, 0xF, 0, 0};
unsigned short XLSLabel[6] = {0x204, 0, 0, 0, 0, 0};
unsigned short XLSBlank[5] = {0x201, 6, 0, 0, 0x17};
unsigned short XLSBlankF[5] = {0x201, 6, 0, 0, 0x19};
unsigned short XLSBlankRF[5] = {0x201, 6, 0, 0, 0x1C};
unsigned short XLSNumber[5] = {0x203, 14, 0, 0, 0};
unsigned short XLSRK[5] = {0x27E, 10, 0, 0, 0};
unsigned short XLSFormula[16] ={0x406, 0x1D, 0, 0, 0x18, 0, 0, 0, 0, 3, 0x0B, 0x25, 0, 0, 0, 0};
unsigned short XLSEOF[2] = {0x0A, 0};
ExcelCF::ExcelCF()
{
}
ExcelCF::~ExcelCF()
{
}
void ExcelCF::InsertTableColNum(int nCount)
{
// 设定每列字体
// XLSFONT[6]值 ( 0正常体 1粗体 2斜体 4底线 8叉线 )
int i;
m_nCount=nCount;
char s[100];
__int8 b;
div_t pt;
for( i=0;i<m_nCount;i++)
{
wsprintf(s,"Sylfaen");
b=__int8(strlen(s));
XLSFONT[2] = b+7;
pt=div(10*20,256); //10磅字体
XLSFONT[4] = pt.rem;
XLSFONT[5] = pt.quot;
XLSFONT[6] = 0; //正常体
XLSFONT[8] = 12;
fwrite(XLSFONT,1,10,m_Filehandle);
fwrite(&b,1,1,m_Filehandle);
fwrite(s,1,b,m_Filehandle);
}
fwrite(XLSXF1,1,16,m_Filehandle);
fwrite(XLSXF2,1,16,m_Filehandle);
fwrite(XLSXF2,1,16,m_Filehandle);
fwrite(XLSXF3,1,16,m_Filehandle);
fwrite(XLSXF3,1,16,m_Filehandle);
for(i=0;i<10;i++)
{
fwrite(XLSXF4,1,16,m_Filehandle);
}
fwrite(XLSXF5,1,16,m_Filehandle);
fwrite(XLSXF6,1,16,m_Filehandle);
fwrite(XLSXF7,1,16,m_Filehandle);
fwrite(XLSXF8,1,16,m_Filehandle);
fwrite(XLSXF9,1,16,m_Filehandle);
fwrite(XLSXF10,1,16,m_Filehandle);
XLSXF[12]=0xA7; //0xA7 细点线 0xA1线
XLSXF[13]=0xA7; //0xA7 细点线 0xA1线
XLSXF[14]=0xA7; //0xA7 细点线 0xA1线
XLSXF[15]=0xA7; //0xA7 细点线 0xA1线
fwrite(XLSXF,1,16,m_Filehandle);
fwrite(XLSXm_Filehandle,1,16,m_Filehandle);
fwrite(XLSXFG,1,16,m_Filehandle);
fwrite(XLSXFF,1,16,m_Filehandle);
fwrite(XLSXFF1,1,16,m_Filehandle);
fwrite(XLSXFB,1,16,m_Filehandle);
fwrite(XLSXFRF,1,16,m_Filehandle);
fwrite(XLSXFRF1,1,16,m_Filehandle);
//
// 设定每列对齐方式
// XLSXF[8]取值 ( 左=0x21 中=0x22 右=0x23 )
for (i=0;i<m_nCount;i++)
{
XLSXF[4] = i+10;
XLSXF[8] = 0x21; //置左
fwrite(XLSXF,1,16,m_Filehandle);
}
}
int ExcelCF::CreateExcelFile(CString strFileName)
{
int nRet=0;
if((m_Filehandle=fopen(strFileName,"w+b"))==NULL)
return -1;
fwrite(XLSBOF,2,5,m_Filehandle);//10
fwrite(XLSGUTS,2,6,m_Filehandle);//12
fwrite(XLSPalette,2,3,m_Filehandle);//6
//颜色 (RGB) 每色占2位
//0xFFFFFF 白色
//0x000000 黑色
long c;
c=0xFF0202; fwrite(&c,4,1,m_Filehandle);//4 HeaderFont.Color 首行 字体 蓝色
c=0x007D7D; fwrite(&c,4,1,m_Filehandle);//4 HeaderColor 底部 深黄绿色
c=0x000000; fwrite(&c,4,1,m_Filehandle);//4 GroupNodeTextColor 尾行 字体 嘿色
c=0xFF8080; fwrite(&c,4,1,m_Filehandle);//4 GroupNodeColor 底部 紫色
c=0x000000; fwrite(&c,4,1,m_Filehandle);//4 Font.Color 数据 字体 嘿色
c=0xDFFFFF; fwrite(&c,4,1,m_Filehandle);//4 Color 底部 土黄色
c=0x000000; fwrite(&c,4,1,m_Filehandle);//4 clWindowFrame
c=0xFFFFFF; fwrite(&c,4,1,m_Filehandle);//4 clWindow
c=0x000000; fwrite(&c,4,1,m_Filehandle);//4 BandFont.Color
c=0xFFFFFF; fwrite(&c,4,1,m_Filehandle);//4 BandColor
c=0x000000; fwrite(&c,4,1,m_Filehandle);//4 RowFooterTextColor
c=0xFFFFFF; fwrite(&c,4,1,m_Filehandle);//4 RowFooterColor
c=0x000000; fwrite(&c,4,1,m_Filehandle);//4 GridLineColor
// EXCEL Font 系统默认字体
fwrite(XLSSFont,1,16,m_Filehandle);//16
fwrite(XLSSFont,1,16,m_Filehandle);//16
fwrite(XLSSFont,1,16,m_Filehandle);//16
fwrite(XLSSFont,1,16,m_Filehandle);//16
// Grid Font
// 底部字体 18
char s[256];
div_t pt;
__int8 b;
{
wsprintf(s,"Microsoft Sans Serif");
b=__int8(strlen(s));
pt=div( 12*20,256 ); //12磅字体
XLSFONT[2] = b+7;
XLSFONT[4] = pt.rem;
XLSFONT[5] = pt.quot;
XLSFONT[6] = 1; //粗体
XLSFONT[8] = 12;
fwrite(XLSFONT,1,10,m_Filehandle);
fwrite(&b,1,1,m_Filehandle);
fwrite(s,1,b,m_Filehandle);
}
//
// 设定列头(行顶部)字体 单元格(0,0)
// XLSFONT[6]值 ( 0正常体 1粗体 2斜体 4底线 8叉线 )
{
wsprintf(s,"Arial");
b=__int8(strlen(s));
pt=div( 16*20,256 ); //12磅字体
XLSFONTH[2] = b+7;
XLSFONTH[4] = pt.rem;
XLSFONTH[5] = pt.quot;
XLSFONTH[6] = 1; //粗体
fwrite(XLSFONTH,1,10,m_Filehandle);
fwrite(&b,1,1,m_Filehandle);
fwrite(s,1,b,m_Filehandle);
}
// Group Font
{
wsprintf(s,"Microsoft Sans Serif");
b=__int8(strlen(s));
pt=div( 8*20,256);
XLSFONTG[2] = b+7;
XLSFONTG[4] = pt.rem;
XLSFONTG[5] = pt.quot;
XLSFONTG[6] = 1;
fwrite(XLSFONTG,1,10,m_Filehandle);
fwrite(&b,1,1,m_Filehandle);
fwrite(s,1,b,m_Filehandle);
}
// Band Font
{
wsprintf(s,"MS Gothic");
b=__int8(strlen(s));
pt=div( 8*20,256 );
XLSFONT[2] = b+7;
XLSFONT[4] = pt.rem;
XLSFONT[5] = pt.quot;
XLSFONT[6] = 1;
XLSFONT[8] = 16;
fwrite(XLSFONT,1,10,m_Filehandle);
fwrite(&b,1,1,m_Filehandle);
fwrite(s,1,b,m_Filehandle);
}
// Footer Font
{
wsprintf(s,"Modern");
b=__int8(strlen(s));
pt=div(160,256);
XLSFONT[2] = b+7;
XLSFONT[4] = pt.rem;
XLSFONT[5] = pt.quot;
XLSFONT[6] = 1;
XLSFONT[8] = 18;
fwrite(XLSFONT,1,10,m_Filehandle);
fwrite(&b,1,1,m_Filehandle);
fwrite(s,1,b,m_Filehandle);
}
return nRet;
}
void ExcelCF::InsertColName(CString strColname,int nCol,int Width)
{
int i;
char s[256];
__int16 w;
// 写入每列头部 可以省略
wsprintf(s,strColname);
i=strColname.GetLength();
// _itoa( rand() ,&s[i],2);
w=(__int16)strlen(s);
XLSLabel[1] = w+8;
XLSLabel[2] = 0; //行数 第一行
XLSLabel[3] = i; //列数
XLSLabel[4] = 0x16 ;
XLSLabel[5] = w;
fwrite(XLSLabel,2,6,m_Filehandle);
fwrite(s,1,w,m_Filehandle);
XLSCOL[2] = nCol;
XLSCOL[3] = nCol;
XLSCOL[4] = Width*1000; //列宽150
fwrite(XLSCOL,2,8,m_Filehandle);
}
void ExcelCF::InsertStringColValue(CString strValue,int nCol,int nRow)
{
char s[1024];
wsprintf(s,strValue);
int i=strValue.GetLength();
// _itoa( rand() ,&s[i],2);
__int16 w;
w=(__int16)strlen(s);
XLSLabel[1] = w+8;
XLSLabel[2] =(unsigned short) nRow; //行数
XLSLabel[3] =(unsigned short) nCol; //列数
XLSLabel[4] = 0x1D+0;
XLSLabel[5] = w;
fwrite(XLSLabel,2,6,m_Filehandle);
fwrite(s,1,w,m_Filehandle);
}
void ExcelCF::InsertDoubleColValue(double dValue,int nCol,int nRow)
{
XLSNumber[2]=(unsigned short) nRow; //行数
XLSNumber[3]=(unsigned short) nCol; //列数
XLSNumber[4]=0x1D+1;
fwrite(XLSNumber,2,5,m_Filehandle);
fwrite(&dValue,8,1,m_Filehandle);
}
void ExcelCF::InsertIntgerColValue(int nValue,int nCol,int nRow)
{
XLSRK[2]=(unsigned short) nRow; //行数
XLSRK[3]=(unsigned short) nCol; //列数
XLSRK[4]=0x1D+2;
fwrite(XLSRK,2,5,m_Filehandle);
fwrite(&nValue,4,1,m_Filehandle);
}
void ExcelCF::ExcelClose()
{
fwrite(XLSEOF,2,2,m_Filehandle);
fclose(m_Filehandle);
}
void ExcelCF::InsertColums(int* nColLength,int nColNum,LPTSTR* arrpsz )
{
int i;
m_nCount=nColNum;
char s[256];
__int8 b;
div_t pt;
for( i=0;i<m_nCount;i++)
{
wsprintf(s,"Sylfaen");
b=__int8(strlen(s));
XLSFONT[2] = b+7;
pt=div(10*20,256); //10磅字体
XLSFONT[4] = pt.rem;
XLSFONT[5] = pt.quot;
XLSFONT[6] = 0; //正常体
XLSFONT[8] = 12;
fwrite(XLSFONT,1,10,m_Filehandle);
fwrite(&b,1,1,m_Filehandle);
fwrite(s,1,b,m_Filehandle);
}
fwrite(XLSXF1,1,16,m_Filehandle);
fwrite(XLSXF2,1,16,m_Filehandle);
fwrite(XLSXF2,1,16,m_Filehandle);
fwrite(XLSXF3,1,16,m_Filehandle);
fwrite(XLSXF3,1,16,m_Filehandle);
for(i=0;i<10;i++)
{
fwrite(XLSXF4,1,16,m_Filehandle);
}
fwrite(XLSXF5,1,16,m_Filehandle);
fwrite(XLSXF6,1,16,m_Filehandle);
fwrite(XLSXF7,1,16,m_Filehandle);
fwrite(XLSXF8,1,16,m_Filehandle);
fwrite(XLSXF9,1,16,m_Filehandle);
fwrite(XLSXF10,1,16,m_Filehandle);
XLSXF[12]=0xA7; //0xA7 细点线 0xA1线
XLSXF[13]=0xA7; //0xA7 细点线 0xA1线
XLSXF[14]=0xA7; //0xA7 细点线 0xA1线
XLSXF[15]=0xA7; //0xA7 细点线 0xA1线
fwrite(XLSXF,1,16,m_Filehandle);
fwrite(XLSXm_Filehandle,1,16,m_Filehandle);
fwrite(XLSXFG,1,16,m_Filehandle);
fwrite(XLSXFF,1,16,m_Filehandle);
fwrite(XLSXFF1,1,16,m_Filehandle);
fwrite(XLSXFB,1,16,m_Filehandle);
fwrite(XLSXFRF,1,16,m_Filehandle);
fwrite(XLSXFRF1,1,16,m_Filehandle);
//
// 设定每列对齐方式
// XLSXF[8]取值 ( 左=0x21 中=0x22 右=0x23 )
for (i=0;i<m_nCount;i++)
{
XLSXF[4] = i+10;
XLSXF[8] = 0x21; //置左
fwrite(XLSXF,1,16,m_Filehandle);
}
//
// 设定每列宽度
// XLSCOL[4]取值 36 * 每列宽度
CString str;
int nLength;
for(i=0;i<m_nCount;i++)
{
XLSCOL[2] = i;
XLSCOL[3] = i;
str.Format("%s",arrpsz[ i ]);
nLength=str.GetLength();
XLSCOL[4] =nColLength[i]*800; //列宽150
fwrite(XLSCOL,2,8,m_Filehandle);
}
fwrite(XLSDimension,2,7,m_Filehandle);
__int16 w;
// 写入每列头部 可以省略
CString strColname;
for(i=0;i<m_nCount;i++)
{
strColname.Format("%s",arrpsz[ i ]);
wsprintf(s,strColname);
w=(__int16)strlen(s);
XLSLabel[1] = w+8;
XLSLabel[2] = 0; //行数 第一行
XLSLabel[3] = i; //列数
XLSLabel[4] = 0x16 ;
XLSLabel[5] = w;
fwrite(XLSLabel,2,6,m_Filehandle);
fwrite(s,1,w,m_Filehandle);
}
for(int n=0;n<m_nCount;n++)
{
delete arrpsz[ n ];
}
delete arrpsz;
}
void ExcelCF::ExceLDimension()
{
fwrite(XLSDimension,2,7,m_Filehandle);
}
void ExcelCF::InsertColums(CUIntArray& nIntArray,int nColNum,CStringArray& strColums)
{
int i;
m_nCount=nColNum;
char s[256];
__int8 b;
div_t pt;
for( i=0;i<m_nCount;i++)
{
wsprintf(s,"Sylfaen");
b=__int8(strlen(s));
XLSFONT[2] = b+7;
pt=div(10*20,256); //10磅字体
XLSFONT[4] = pt.rem;
XLSFONT[5] = pt.quot;
XLSFONT[6] = 0; //正常体
XLSFONT[8] = 12;
fwrite(XLSFONT,1,10,m_Filehandle);
fwrite(&b,1,1,m_Filehandle);
fwrite(s,1,b,m_Filehandle);
}
fwrite(XLSXF1,1,16,m_Filehandle);
fwrite(XLSXF2,1,16,m_Filehandle);
fwrite(XLSXF2,1,16,m_Filehandle);
fwrite(XLSXF3,1,16,m_Filehandle);
fwrite(XLSXF3,1,16,m_Filehandle);
for(i=0;i<10;i++)
{
fwrite(XLSXF4,1,16,m_Filehandle);
}
fwrite(XLSXF5,1,16,m_Filehandle);
fwrite(XLSXF6,1,16,m_Filehandle);
fwrite(XLSXF7,1,16,m_Filehandle);
fwrite(XLSXF8,1,16,m_Filehandle);
fwrite(XLSXF9,1,16,m_Filehandle);
fwrite(XLSXF10,1,16,m_Filehandle);
XLSXF[12]=0xA7; //0xA7 细点线 0xA1线
XLSXF[13]=0xA7; //0xA7 细点线 0xA1线
XLSXF[14]=0xA7; //0xA7 细点线 0xA1线
XLSXF[15]=0xA7; //0xA7 细点线 0xA1线
fwrite(XLSXF,1,16,m_Filehandle);
fwrite(XLSXm_Filehandle,1,16,m_Filehandle);
fwrite(XLSXFG,1,16,m_Filehandle);
fwrite(XLSXFF,1,16,m_Filehandle);
fwrite(XLSXFF1,1,16,m_Filehandle);
fwrite(XLSXFB,1,16,m_Filehandle);
fwrite(XLSXFRF,1,16,m_Filehandle);
fwrite(XLSXFRF1,1,16,m_Filehandle);
//
// 设定每列对齐方式
// XLSXF[8]取值 ( 左=0x21 中=0x22 右=0x23 )
for (i=0;i<m_nCount;i++)
{
XLSXF[4] = i+10;
XLSXF[8] = 0x21; //置左
fwrite(XLSXF,1,16,m_Filehandle);
}
//
// 设定每列宽度
// XLSCOL[4]取值 36 * 每列宽度
CString str;
int nLength;
for(i=0;i<m_nCount;i++)
{
XLSCOL[2] = i;
XLSCOL[3] = i;
str.Format("%s",strColums.GetAt(i));
nLength=str.GetLength();
XLSCOL[4] =(unsigned short)(nIntArray.GetAt(i))*800; //列宽150
fwrite(XLSCOL,2,8,m_Filehandle);
}
fwrite(XLSDimension,2,7,m_Filehandle);
__int16 w;
// 写入每列头部 可以省略
CString strColname;
for(i=0;i<m_nCount;i++)
{
strColname.Format("%s",strColums.GetAt(i));
wsprintf(s,strColname);
w=(__int16)strlen(s);
XLSLabel[1] = w+8;
XLSLabel[2] = 0; //行数 第一行
XLSLabel[3] = i; //列数
XLSLabel[4] = 0x16 ;
XLSLabel[5] = w;
fwrite(XLSLabel,2,6,m_Filehandle);
fwrite(s,1,w,m_Filehandle);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -