惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
N
Netflix TechBlog - Medium
腾讯CDC
C
Check Point Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
S
SegmentFault 最新的问题
F
Fortinet All Blogs
美团技术团队
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
F
Full Disclosure
Recorded Future
Recorded Future
D
DataBreaches.Net
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
J
Java Code Geeks
I
InfoQ
Y
Y Combinator Blog
A
About on SuperTechFans
AI
AI
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
W
WeLiveSecurity
M
MIT News - Artificial intelligence
雷峰网
雷峰网
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Schneier on Security
Schneier on Security
The GitHub Blog
The GitHub Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Docker
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
V2EX - 技术
V2EX - 技术
Project Zero
Project Zero

博客园 - tecsun

Base64 Encode and Decode - BCB[转] char * 与 wchar_t *(BSTR) 转换 Borland IDE 常用热键简介 WINXP总是默认以Administrator登陆系统,怎样让自建用户启动 [轉]MD5算法(C++实现) [轉]MYSQL 字符集问题 [轉]mysql多种存储引擎及特点 MySQL date/time 不能转换为 System.DateTime的问题[VS2005(C#)+mysql] mysql 相关 [转]写得蛮好的linux学习笔记 VS2005中使用mysql 在ubuntu下使用qq[转] [轉自ccrun by dewbow ] BCB和TTS5.1做中文语音 分割字符串(BCB)(從文本文檔中取出需要的字符串) [C++]BCD碼轉換問題 [轉]我们必须了解的8个dos命令(转自http://www.hxblog.net/article.asp?id=491) [C#][轉]字符转ASCII码,ASCII码转字符 控制外部應用程式的一些記錄(C++) [轉]窗口句柄的获得
BCB 控制 Excel类[转]
tecsun · 2008-06-12 · via 博客园 - tecsun

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