#include <vcl.h>
#pragma hdrstop
#include "OperateExcel2k.h "
void OperateExcel::OpenExcel()
{
ExcelApp =Variant::CreateObject( "Excel.Application ");
ExcelApp.OlePropertySet ( "visible ",true);
Workbook=ExcelApp.OlePropertyGet( "ActiveWorkBook ");
}
//---------------------------------------------------------------------------
void OperateExcel::OpenExcelFile(AnsiString str_ExcelFileName,bool Visibled,bool IfReadOnly)
{
//创建excel对象
ExcelApp =Variant::CreateObject( "Excel.Application ");
ExcelApp.OlePropertyGet( "WorkBooks ").OleProcedure( "Open ",str_ExcelFileName,NULL,IfReadOnly);
ExcelApp.OlePropertySet ( "visible ",Visibled);
Workbook=ExcelApp.OlePropertyGet( "ActiveWorkBook ");
WorkSheet=Workbook.OlePropertyGet( "ActiveSheet ");
}
//---------------------------------------------------------------------------
void OperateExcel::SetCellValue(int iRowNO, int iColumnNO, Variant Value)
{
WorkSheet.OlePropertyGet( "Cells ",iRowNO,iColumnNO).OlePropertySet( "Value ",Value);
}
//---------------------------------------------------------------------------
void OperateExcel::SetCellValue(int iRowNO, int iColumnNO, char* Value)
{
WorkSheet.OlePropertyGet( "Cells ",iRowNO,iColumnNO).OlePropertySet( "Value ",Value);
}
//---------------------------------------------------------------------------
void OperateExcel::WorkBookSave()
{
Workbook.OleProcedure( "Save ");
}
//---------------------------------------------------------------------------
void OperateExcel::WorkBookClose()
{
Workbook.OleProcedure( "Close ");
}
//---------------------------------------------------------------------------
void OperateExcel::WorkBookSaveAs(AnsiString str_FileName)
{
Workbook.OleProcedure( "Close ",str_FileName);
}
//---------------------------------------------------------------------------
void OperateExcel::ResetArray1D()
{
memset(str_DataArray1D, 0, 16 );
}
//---------------------------------------------------------------------------
void OperateExcel::ResetArray2D()
{
memset(str_DataArray2D, 0, 16 );
}
//---------------------------------------------------------------------------
void OperateExcel::SetRowValue(int iRowNO, int iColumnNO, int iElementsNum)
{
int i=0;
while(i <iElementsNum)
{
SetCellValue(iRowNO,i,str_DataArray1D[i]);
i++;
}
}
//---------------------------------------------------------------------------
void OperateExcel::SetColumnValue(int iRowNO, int iColumnNO, int iElementsNum)
{
int i=0;
while(i <iElementsNum)
{
SetCellValue(i,iColumnNO,str_DataArray1D[i]);
i++;
}
}
//---------------------------------------------------------------------------
AnsiString OperateExcel::GetValueFromCell(int iRowNO, int iColumnNO)
{
AnsiString str_Value;
str_Value=WorkSheet.OlePropertyGet( "cells ",iRowNO,iColumnNO).OlePropertyGet( "Value ");
return str_Value;
}
//---------------------------------------------------------------------------
bool OperateExcel::CellMerged(int iRowNO,int iColumnNO)
{
return WorkSheet.OlePropertyGet( "Cells ",iRowNO,iColumnNO).OlePropertyGet( "MergeCells ");
}
//---------------------------------------------------------------------------
void OperateExcel_v2::ReadValueByRow(int iTop, int iLeft, int iBottom, int iRight)
{
int i=0;
int iCount=0;
AnsiString temp;
for(int iRowNO=iTop;iRowNO <=iBottom;iRowNO++,i++)
{
int iColumnNO=iLeft;
int j=0;
while(iColumnNO <=iRight)
{
temp=GetValueFromCell(iRowNO,iColumnNO) ;
str_DataArray2D[i][j]=temp;
if(CellMerged(iRowNO,iColumnNO)==false)
{j++;iColumnNO++;}
else if(CellMerged(iRowNO,iColumnNO)==true)
{
iCount=WorkSheet.OlePropertyGet( "Cells ",iRowNO,iColumnNO).OlePropertyGet( "MergeArea ").OlePropertyGet( "Count ");
for(int num=0;num <iCount;num++,j++,iColumnNO++)
str_DataArray2D[i][j]=temp+ ": "+AnsiString(num);
}
}
}
}
//---------------------------------------------------------------------------
void OperateExcel_v2::SendDataToDataSet(TDataSet* pDataSet,int iColumnsNumber,int iRowsNumber)
{
for(int iRowNo=0;iRowNo <iRowsNumber;iRowNo++)
{
pDataSet-> Insert();
for(int iColumnNo=1;iColumnNo <=iColumnsNumber;iColumnNo++)
{
pDataSet-> Fields-> FieldByNumber(iColumnNo)-> Value=str_DataArray2D[iRowNo][iColumnNo-1];
}
}
pDataSet-> Post();
}
//---------------------------------------------------------------------------
int OperateExcel::GetUsedRowCount()
{
return WorkSheet.OlePropertyGet( "UsedRange ").OlePropertyGet( "Rows ").OlePropertyGet( "Count ");
}
//---------------------------------------------------------------------------
int OperateExcel::GetUsedColCount()
{
return WorkSheet.OlePropertyGet( "UsedRange ").OlePropertyGet( "Columns ").OlePropertyGet( "Count ");
}
//---------------------------------------------------------------------------
void OperateExcel::QuitExcel()
{
if(!ExcelApp.IsEmpty())
ExcelApp.OleProcedure( "Quit ") ;
}
//---------------------------------------------------------------------------
|