C++Builder 自编常用函数库(1)
int CurrRow,CurrCol;
CurrCol=0;
CurrRow=0;
Screen->Cursor=crHourGlass ;
if(Ex.IsEmpty())
try
{
HWND hPrevApp = ::FindWindow(NULL,"Microsoft Excel");
if(!hPrevApp)
{
Ex=Variant::CreateObject("Excel.Application");
}
else
{
Ex=Variant::GetActiveObject("Excel.Application");
}
}
catch(...)
{
Screen->Cursor=crDefault;
ShowMessage("打开Excel出错,请确认你已经正确安装了MS Office!");
return;
}
try
{
if(Ex.OlePropertyGet("ActiveWorkBook").IsEmpty())
Ex.OlePropertyGet("WorkBooks").OleProcedure("ADD");
if(Wb.IsEmpty())
Wb=Ex.OlePropertyGet("ActiveWorkBook");
Sh1=Wb.OlePropertyGet("Sheets").OleFunction("Add");
}
catch(...)
{
Ex=Ex.Empty();
Wb=Wb.Empty();
DataSetToExcel(DataSet,H);
}
if(H)
{
for (int j=0;j<DataSet->Fields->Count;j++)
{
if(DataSet->Fields->Fields[j]->Visible)
{
CurrCol++;
Sh1.OlePropertyGet("Cells",1,CurrCol).OlePropertySet("value",DataSet->Fields->Fields[j]->FieldName);
}
}
DataSet->First();
/* for (int i=0;i<DataSet->RecordCount;i++)
{
CurrCol=0;
for (int j=0;j<DataSet->Fields->Count;j++)
{
if(DataSet->Fields->Fields[j]->Visible)
{
CurrCol++;
Sh1.OlePropertyGet("Cells",i+2,CurrCol).OlePropertySet("value",DataSet->Fields->Fields[j]->AsString);
}
}
Application->ProcessMessages();
DataSet->Next();
}
*/
int i=0;
while(!DataSet->Eof)
{
CurrCol=0;
for (int j=0;j<DataSet->Fields->Count;j++)
{
if(DataSet->Fields->Fields[j]->Visible)
{
CurrCol++;
Sh1.OlePropertyGet("Cells",i+2,CurrCol).OlePropertySet("value",DataSet->Fields->Fields[j]->AsString);
}
}
Application->ProcessMessages();
DataSet->Next();
i++;
}
}
else
{
for (int j=0;j<DataSet->Fields->Count;j++)
{
if(DataSet->Fields->Fields[j]->Visible)
{
CurrRow++ ;
Sh1.OlePropertyGet("Cells",CurrRow,1).OlePropertySet("value",DataSet->Fields->Fields[j]->FieldName);
DataSet->First();
int i=0;
while(!DataSet->Eof)
{
Sh1.OlePropertyGet("Cells",CurrRow,i+2).OlePropertySet("value",DataSet->Fields->Fields[j]->AsString);
Application->ProcessMessages();
DataSet->Next();
i++;
}
}
}
}
Ex.OlePropertySet("Visible",true);
Screen->Cursor=crDefault;
}
//-------------------------------------------------------------------------
String makestr(String mstr,char b,int len,bool QH)//QH在前或者后加字符
{
int slen=mstr.Length();
String tmp="";
if(slen<len)
{
if(QH)
{
tmp=AnsiString::StringOfChar(b,len-slen)+mstr;
}
else
{
tmp=mstr+AnsiString::StringOfChar(b,len-slen);
}
}
else
{
tmp=mstr;
}
return tmp;
}
//------------------------------------------------------------------------
String getdtbh()
{
//按日期生成类似20001025--000001的编号
TDateTime dt;
unsigned short year,month,day;
::DecodeDate(dt.CurrentDate(),year,month,day);
String tmp="";
tmp=String(year)+makestr(month,'0',2,true)+makestr(day,'0',2,true);
return tmp;
}
//------------------------------------------------------------------------
//=============================================================================
// 函数使用说明:输入表名称,取得所有纪录的最大值加一,转换成n位的00001形式
// 输入:cTableName 数据库中的某一表的名称
// nNumkeep 格式化后字符串的总长度
//=============================================================================
AnsiString __fastcall TMainForm::GetPerId(AnsiString cTableName,int nNumkeep)
{
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Add("Select Count(*) AS curn from "+cTableName);
ADOQuery1->Prepared;
ADOQuery1->Active=true;
ADOQuery1->Open();
// ShowMessage(ADOQuery1->Fields->Fields[0]->AsString) ;
int ngetn=ADOQuery1->FieldByName("curn")->AsInteger;
ADOQuery1->Active=false;
ADOQuery1->Close();
return Get00Str(ngetn+1,nNumkeep);
}
/*-----------------------------------------------------------------------
函数:Get00Str
功能:生成 0000000x 形字符串
输入: oragn 被转换的整形数
nNumkeep 格式化后字符串的总长度
------------------------------------------------------------------------*/
AnsiString __fastcall TMainForm::Get00Str(int oragn,int nNumkeep)
{
AnsiString cc=IntToStr(oragn);
cc=cc.Trim();
AnsiString tmp0="";
int Flen=cc.Length();
for(int j=0;j<nNumkeep-Flen;j++)
{
tmp0="0"+tmp0;
}
cc=tmp0+cc;
return cc;
}
//==============================================================================

