tecsun

study .net

博客园 首页 新随笔 联系 订阅 管理
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
posted on 2008-06-12 14:12  tecsun  阅读(917)  评论(2编辑  收藏  举报