BCB版 Excel操作类 待完善
[cpp]
#ifndef EXCEL_OP_H
#define EXCEL_OP_H
#include <vcl.h>
class ExcelOp
{
Variant s_vExcelApp,s_vSheet;
bool s_bOpen;
int s_nRowCount,s_nColCount;
protected:
Variant GetCell(int row,int col)
{
if(s_bOpen)
{
return s_vSheet.OlePropertyGet("Cells",row,col).OlePropertyGet("Value");
}
return Null;
}
void SetCell(int row,int col,Variant &var)
{
if(!s_bOpen)
{
return ;
}
String temp=var;
s_vSheet.OlePropertyGet("Cells",row,col).OlePropertySet("Value",temp.c_str());
}
int GetRowCount()
{
if(!s_bOpen)
return 0;
int i=1;
String strValue=Cells[i][1];
if(!strValue.IsEmpty())
{
do
{
strValue=Cells[++i][1];
}while(!strValue.IsEmpty());
--i;
}
s_nRowCount=i;
return s_nRowCount;
}
int GetColCount()
{
if(!s_bOpen)
return 0;
int i=1;
String strValue=Cells[1][i];
if(!strValue.IsEmpty())
{
do
{
strValue=Cells[1][++i];
}while(!strValue.IsEmpty());
--i;
}
s_nColCount=i;
return s_nColCount;
}
public:
const enum Op
{
Create_Op,
Load_Op
};
ExcelOp():s_vExcelApp(Null),
s_vSheet(Null),
s_bOpen(false),
s_nRowCount(0),
s_nColCount(0)
{}
~ExcelOp()
{
try
{
s_vExcelApp.OleFunction("Quit");
s_vSheet=Unassigned;
s_vExcelApp=Unassigned;
}
catch(...)
{}
}
void Close()
{
if(s_bOpen)
{
s_vExcelApp.OlePropertyGet("ActiveWorkbook").OleFunction("Close");
s_bOpen=false;
}
}
void Save()
{
if(s_bOpen)
{
s_vExcelApp.OlePropertyGet("ActiveWorkbook").OleFunction("Save");
}
}
void Save(const char* lpszFileName)
{
if(s_bOpen)
{
s_vExcelApp.OlePropertyGet("ActiveWorkbook").OleFunction("SaveAs",lpszFileName);
}
}
bool Open(const char* szFileName,const Op op)
{
if(op!=Create_Op)
{
if(!FileExists(szFileName))
return false;
}
Close();
do
{
try
{
s_vExcelApp=Variant::CreateObject("Excel.Application");
}
catch(...)
{
Application->MessageBoxA("CreateObject出错,请确认本机是否安装了MS-EXCEL。","错误",MB_ICONINFORMATION);
break;
}
try
{
s_vExcelApp.OlePropertySet("Visible", false);
if(op==Load_Op)
{
s_vExcelApp.OlePropertyGet("Workbooks").OleProcedure("Open",szFileName); // 工作表
s_vSheet=s_vExcelApp.OlePropertyGet("ActiveWorkbook").OlePropertyGet("ActiveSheet");
}
else if(op==Create_Op)
{
s_vExcelApp.OlePropertyGet("Workbooks").OleFunction("Add", 1); // 工作表
s_vSheet=s_vExcelApp.OlePropertyGet("ActiveWorkbook").OlePropertyGet("Sheets", 1);
}
}
catch(...) www.zzzyk.com
{
break;
}
&n
补充:软件开发 , C++ ,