class ConExcel
{
//#include "vcl\\utilcls.h" in head file//
/*LiYongQuan*/
public:
AnsiString *rowVal,cellVal;
/*row当前行;col当前列;colCount总列数;rowCount总行数*/
unsigned int row,col,colCount,rowCount;
private:
Variant MSSheet,MSExcel;
AnsiString FileName;
char RW;
public:
ConExcel(AnsiString FileName,char LYQ='R')
{
if(Trim(FileName)=="")
{ShowMessage("未指定EXCEL文件");
return;
}
else
{
try
{
MSExcel=CreateOleObject("Excel.Application");
MSExcel.OlePropertySet("Visible",false);
}
catch(...)
{
Application->MessageBox("无法启动Excel","错误",MB_ICONSTOP|MB_OK);
return;
}
if (FileExists(FileName))
{MSExcel.OlePropertyGet("WorkBooks").OleProcedure("Open",FileName.c_str());
}
else
{
MSExcel.OlePropertyGet("WorkBooks").OleFunction("Add");
// MSExcel.OlePropertyGet("Sheets").OleFunction("Add");
}
MSSheet=MSExcel.OlePropertyGet("ActiveWorkBook").OlePropertyGet("ActiveSheet");
row=1;
col=1;
this->RW=LYQ;
this->FileName=FileName;
colCount=MSSheet.OlePropertyGet("UsedRange").OlePropertyGet("Columns").OlePropertyGet("Count"); //列数
rowCount=MSSheet.OlePropertyGet("UsedRange").OlePropertyGet("Rows").OlePropertyGet("Count"); //行数
rowVal=NULL;
}
}
AnsiString GetCellVal(unsigned int row=0,unsigned int col=0)
{
if(row==0)
row=this->row;
if(col==0)
col=this->col;
AnsiString temp=Trim((AnsiString)MSSheet.OlePropertyGet("Cells",row,col).OlePropertyGet("Value"));
this->cellVal=temp;
return temp;
}
AnsiString * GetRowVal(unsigned int row=0,unsigned int tcol=0)
{
if(row==0)
row=this->row;
if(tcol==0)
tcol=this->colCount;
if(this->rowVal!=NULL)
delete [] this->rowVal;
rowVal= new AnsiString[tcol];
for(unsigned int c=1;c<=tcol;c++)
rowVal[c-1]=Trim((AnsiString)MSSheet.OlePropertyGet("Cells",row,c).OlePropertyGet("Value"));
return rowVal;
}
void setCellVal(unsigned int row,unsigned int col,AnsiString CellVal)
{
if(row==0)
row=this->row;
if(col==0)
col=this->col;
MSSheet.OlePropertyGet("Cells",row,col).OlePropertySet("Value",CellVal.c_str());
}
void setRowVal(unsigned int row,unsigned int tcol,AnsiString * RowVal)
{
if(row==0)
row=this->row;
if(tcol==0)
tcol=this->colCount;
for(unsigned int c=1;c<=tcol;c++)
MSSheet.OlePropertyGet("Cells",row,c).OlePropertySet("Value",(*(RowVal+c-1)).c_str());
}
void SaveFile(AnsiString FName="")
{
if(UpperCase(this->RW)=='W')
{
if(FName=="")
FName=this->FileName;
MSSheet.OleProcedure("SaveAs",FName.c_str());
}
}
~ConExcel()
{
MSExcel.OlePropertyGet("ActiveWorkBook").OleProcedure("Close");
MSExcel.OleFunction("Quit");
MSExcel=Unassigned;
if(this->rowVal!=NULL)
delete [] this->rowVal;
Application->MessageBox("Excel读取完成!","Invoice Process System",MB_OK+MB_ICONINFORMATION);
}
};
阅读(1652) | 评论(0) | 转发(0) |