BCB 控制 Excel类[转]

本文介绍了一个名为TExcel的类,该类允许在C++中进行Excel文件的操作,包括打开、创建、读写单元格数据及保存文件等。文章提供了详细的使用方法,帮助开发者快速上手。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TExcel.rar
TExcel.cpp

/**/ /***********************************************************************
 * Module:  TExcel.cpp
 * Author:  cjz
 * Modified: 2007-07-29日 11:07:30
 * Purpose: Implementation of the class TExcel
 * Comment: Excel巨摸
 **********************************************************************
*/

#include 
< vcl.h >
#include 
" TExcel.h "
#include 
" vcl\utilcls.h "

/**/ /**/ /**/ ////////////////////////////////////////////////////////////////////////
//  Name:       TExcel::TExcel()
//  Purpose:    Implementation of TExcel::TExcel()
//  Return:
/**/ /**/ /**/ ////////////////////////////////////////////////////////////////////////

TExcel::TExcel()
{
        Sheet
=new TSheet();
    NewExcel();
}


/**/ /**/ /**/ ////////////////////////////////////////////////////////////////////////
//  Name:       TExcel::TExcel(AnsiString m_strFile)
//  Purpose:    Implementation of TExcel::TExcel()
//  Comment:    带参数的构造函数
//  Parameters:
//  - m_strFile
//  Return:
/**/ /**/ /**/ ////////////////////////////////////////////////////////////////////////

TExcel::TExcel(AnsiString m_strFile)
{
   strFileName
=m_strFile;
   Sheet
=new TSheet();
   OpenExcel();
   
}


/**/ /**/ /**/ ////////////////////////////////////////////////////////////////////////
//  Name:       TExcel::~TExcel()
//  Purpose:    Implementation of TExcel::~TExcel()
//  Return:     
/**/ /**/ /**/ ////////////////////////////////////////////////////////////////////////

TExcel::
~ TExcel()
{
   
//CloseExcel();
}


/**/ /**/ /**/ ////////////////////////////////////////////////////////////////////////
//  Name:       TExcel::OpenExcel()
//  Purpose:    Implementation of TExcel::OpenExcel()
//  Return:     bool
/**/ /**/ /**/ ////////////////////////////////////////////////////////////////////////

bool  __fastcall TExcel::OpenExcel( void )
{
   
if(!FileExists(strFileName))
   
{
     MessageDlg(
"ゅンぃ!",
             mtWarning, TMsgDlgButtons() 
<< mbOK, 0);
     
return false;
   }

   
try
   
{
       CoInitialize(NULL);
       vEx
=CreateOleObject("Excel.Application");
       vEx.OPS(
"Visible",false);
       vEx.OPG(
"WorkBooks").OPR("Open",strFileName.c_str());
       vWb
=vEx.OPG("ActiveWorkBook");
       vSh
=vWb.OPG("ActiveSheet");

           nSheetCount
=vWb.OPG("sheets").OPG("count");

   }

   
catch()
   
{
        ShowMessage(
"礚猭币笆Excel,琌⊿Τ杆!");
         
return false;
   }

   
return true;
}


/**/ /**/ /**/ ////////////////////////////////////////////////////////////////////////
//  Name:       TExcel::NewExcel()
//  Purpose:    Implementation of TExcel::NewExcel()
//  Return:     bool
/**/ /**/ /**/ ////////////////////////////////////////////////////////////////////////

bool  __fastcall TExcel::NewExcel( void )
{
   
try
   
{
           CoInitialize(NULL);
       vEx
=Variant::CreateObject("Excel.Application");
       vEx.OPS(
"Visible",false);
       vEx.OPG(
"WorkBooks").OFN("Add",1);
       vWb
=vEx.OPG("ActiveWorkBook");
       vSh
=vWb.OPG("ActiveSheet");

           Sheet
->vSh=&vSh;
           nSheetCount
=1;

   }

   
catch()
   
{
        ShowMessage(
"礚猭币笆Excel,琌⊿Τ杆!");
         
return false;
   }

   
return true;
}


// ---------------------------------------------------------------------
// 睰痢
void  __fastcall TExcel::AddSheet(AnsiString SheetName)
{
    vEx.OPG(
"WorkBooks").OFN("Add",1);
    nSheetCount
=vWb.OPG("sheets").OPG("count");
    Sheets[nSheetCount]
->Name=SheetName;
}

/**/ /**/ /**/ ////////////////////////////////////////////////////////////////////////
//  Name:       TExcel::SaveExcel()
//  Purpose:    Implementation of TExcel::SaveExcel()
//  Return:     bool
/**/ /**/ /**/ ////////////////////////////////////////////////////////////////////////

bool   __fastcall TExcel::SaveExcel( void )
{
   
try
   
{
    vWb.OFN(
"save");
   }

   
catch()
   
{
      MessageDlg(
"玂Excelゅ郎ア毖!",
            mtWarning, TMsgDlgButtons() 
<< mbOK, 0);
         
return false;
   }

   
return true;

}


/**/ /**/ /**/ ////////////////////////////////////////////////////////////////////////
//  Name:       TExcel::SaveAsExcel()
//  Purpose:    Implementation of TExcel::SaveAsExcel()
//  Return:     bool
/**/ /**/ /**/ ////////////////////////////////////////////////////////////////////////

bool   __fastcall TExcel::SaveAsExcel(AnsiString fileName)
{
   
if(FileExists(fileName))
   
{
          
if(MessageDlg("ゅン,琌滦籠?",mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)==mrYes)
          
{
              DeleteFile(fileName);
          }

          
else
          
{
                vWb.OPG(
"Application").OPS("DisplayAlerts",false);
                
return false;
          }

   }

   
try
   
{
     vWb.OFN(
"SaveAs",fileName.c_str());
   }

   
catch()
   
{
         MessageDlg(
"玂Excelゅ郎ア毖!",
            mtWarning, TMsgDlgButtons() 
<< mbOK, 0);
         
return false;
   }

   
return true;
}


/**/ /**/ /**/ ////////////////////////////////////////////////////////////////////////
//  Name:       TExcel::CloseExcel()
//  Purpose:    Implementation of TExcel::CloseExcel()
//  Return:     void
/**/ /**/ /**/ ////////////////////////////////////////////////////////////////////////

void   __fastcall TExcel::CloseExcel( void )
{
   vWb.OFN(
"close");
   vEx.OFN(
"quit");

   vWb
=Unassigned;
   vEx
=Unassigned;
   CoUninitialize();
}


// ---------------------------------------------------------------------
// 妮┦:莉材i茂
TSheet  *   __fastcall TExcel::GetSheet( int  i)
{
    
//狦讽玡痢i,玥玥痢i
    if(i==nCurrSheet)
       
return Sheet;
    
else
       nCurrSheet
=i;
    
if(i<1||i>nSheetCount)
        
throw "ぃ茂"+i;
    vSh
=vWb.OPG("Sheets",i);

    nRows
=vSh.OPG("UsedRange").OPG("Rows").OPG("count");
    nColumns
=vSh.OPG("UsedRange").OPG("Columns").OPG("count");

    Sheet
->nRows=nRows;
    Sheet
->nColumns=nColumns;
    Sheet
->vSh=&vSh;
    
return Sheet;

}


TExcel.h

/**/ /**/ /**/ /***********************************************************************
 * Module:  TExcel.h
 * Author:  cjz
 * Modified: 2007-07-29日 11:07:30
 * Purpose: Declaration of the class TExcel
 * Comment: Excel操作类
 **********************************************************************
*/


#if  !defined(__TExcel_TExcel_h)
#define  __TExcel_TExcel_h

// --------------------------------------
#include  " TSheet.h "


#define  OPG OlePropertyGet
#define  OPS OlePropertySet
#define  OFN OleFunction
#define  OPR OleProcedure
#define  PR Procedure

class  TExcel
{
public:
   TExcel();
   
/**//**//**//* 带参数的构造函数 */
   TExcel(AnsiString m_strFile);
   
~TExcel();
   
/**//**//**//* 获取单元格row,column的值 */
   
bool __fastcall SaveExcel(void);
   
bool __fastcall SaveAsExcel(AnsiString);
   
void __fastcall AddSheet(AnsiString);
   
void __fastcall CloseExcel(void);
   
int nRows;
   
int nColumns;
   
int nSheetCount;
   TSheet
* __fastcall GetSheet(int i);
   __property TSheet
* Sheets[int i]={read=GetSheet};

protected:
private:
   
bool __fastcall OpenExcel(void);
   
bool __fastcall NewExcel(void);

   
int nCurrSheet;
   
/**//**//**//* Excel文件名(全名) */
   AnsiString strFileName;
   Variant vEx;
   Variant vWb;
   Variant vSh;
   Variant vRange;
   TSheet 
*Sheet;

}
;


#endif

TSheet.cpp




#pragma  hdrstop


#include 
< vcl.h >

#include 
" vcl\utilcls.h "
#include 
" TSheet.h "

// ---------------------------------------------------------------------------
#pragma  package(smart_init)

TSheet::TSheet()
{

}

TSheet::
~ TSheet()
{}
Variant __fastcall  TSheet::GetCells(
int  i,  int  j)
{
   
return vSh->OPG("Cells",i,j).OPG("value");
}


/**/ /**/ /**/ ////////////////////////////////////////////////////////////////////////
//  Name:       TExcel::SetCells(int i, int j, AnsiString strValue)
//  Purpose:    Implementation of TExcel::SetCells()
//  Parameters:
//  - i
//  - j
//  - strValue
//  Return:     void
/**/ /**/ /**/ ////////////////////////////////////////////////////////////////////////
void   __fastcall TSheet::SetCells( int  i,  int  j, AnsiString Value)
{
   vSh
->OPG("Cells",i,j).OPS("value","'"+Value);
   
return;
}

// ---------------------------------------------------------------------
void   __fastcall TSheet::SetCells( int  i,  int  j, AnsiString Value, int  Width)
{
   vSh
->OPG("Cells",i,j).OPS("value",Value);
   vSh
->OPG("Cells",i,j).OPS("ColumnWidth",Width);
   
return;
}

void   __fastcall TSheet::SetCells( int  i,  int  j,  int  Value)
{
   vSh
->OPG("Cells",i,j).OPS("value",Value);
   
return;
}

// -----------------------------------------------------------------------
AnsiString TSheet::GetName()
{
        
return VarToStr(vSh->OPG("name"));
}

void  TSheet::SetName( AnsiString strName)
{
         vSh
->OPS("name",strName);
}


TSheet.h


#ifndef TSheetH
#define  TSheetH
 
#define  OPG OlePropertyGet
#define  OPS OlePropertySet
#define  OFN OleFunction
#define  OPR OleProcedure
#define  PR Procedure

// 计沮摸
class  TSheet
{
  
public:
      TSheet();
      
~TSheet();
      Variant __fastcall GetCells(
int i, int j);
      
void __fastcall SetCells(int i, int j, AnsiString Value);
      
void __fastcall SetCells(int i, int j, AnsiString Value,int Width);
      
void __fastcall SetCells(int i, int j, int Value);
      
int nRows;
      
int nColumns;
      
//妮┦:sheet嘿
      AnsiString GetName();
      
void SetName(AnsiString );
      __property AnsiString Name
={read=GetName,write=SetName};
      Variant 
*vSh;

  
private:

}
;
// ---------------------------------------------------------------------------
#endif


  使用方法:
     1.打開文件
         TExcel *excel=new TExcel("要打開的文件名");
     2.新建文件
         TExcel *excel=new TExcel();
     3.獲取工作薄n的單元格(i,j)
         excel->Sheets[n]->GetCells(i,j);返回值是一個Varaint類型。
     4.設置工作薄n的單元格(i,j)值
        excel->Sheets[n]->SetCell(i,j,value);
      5.獲取或設置工作薄名稱
        excel->Sheets[n]->Name="...";或sheetname=excel->Sheets[n]->Name;
      6.保存和另存為。
       excel->SaveExcel();
       excel->SaveAsExcel("新的文件名");
      7.關閉
       excel->CloseExcel();

下载源码   TExcel.rar

转载于:https://www.cnblogs.com/tecsun/archive/2008/06/12/1218357.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值