如何用c++builder操纵excel?

时间:2021-12-05 20:22:37
想把 cbuilder查询出来的数据通过excel进行操作
大侠们帮忙??????

9 个解决方案

#1


内容比较多我有一个文档你要不要?

#2


我做了一个最简的例子:
//---------------------------------------------------------------------------

#ifndef MainH
#define MainH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <utilcls.h>
#include <math.h>

#include "CCEdit.h"
#include <Db.hpp>
#include <DBGrids.hpp>
#include <DBTables.hpp>
#include <Grids.hpp>
#include <ADODB.hpp>
#define  PG OlePropertyGet
#define  PS OlePropertySet
#define  FN OleFunction
#define  PR OleProcedure
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
        TButton *Button1;
        TButton *Button2;
        TCCEdit *CCEdit1;
        TButton *Button3;
        TDBGrid *DBGrid1;
        TDataSource *DataSource1;
        TDatabase *MyDB;
        TButton *Button4;
        TQuery *MyQuery;
        TLabel *Label1;
        TEdit *Edit1;
        TEdit *Edit2;
        TEdit *Edit3;
        TEdit *Edit4;
        TEdit *Edit5;
        TLabel *Label2;
        TLabel *Label3;
        TLabel *Label4;
        TLabel *Label5;
        TLabel *Label6;
        TButton *Button5;
        void __fastcall Button1Click(TObject *Sender);
        void __fastcall Button4Click(TObject *Sender);
        void __fastcall Button2Click(TObject *Sender);
        void __fastcall Button3Click(TObject *Sender);
        void __fastcall Button5Click(TObject *Sender);
private: // User declarations
public: // User declarations
        __fastcall TForm1(TComponent* Owner);
protected:
        virtual void __fastcall WndProc(TMessage &Message);

};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

.cpp File
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Main.h"
#include "utilcls.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "CCEdit"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::WndProc(TMessage &Message)
{
        if(Message.Msg==WM_SYSCOMMAND)
        {
                if(Message.WParam==SC_CLOSE || Message.WParam==SC_SCREENSAVE)
                {
                        Message.WParam=0;
                }
        }
        TForm::WndProc(Message);
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
        Variant V,WB1,SH1;
        V=Variant::CreateObject("Excel.Application");
        V.PS("Visible",true);
        V.PG("WorkBooks").PR("Open","c:\\Chenbin\\BCB\\Excel\\Book1.xls");
        WB1=V.PG("ActiveWorkBook");
        SH1=WB1.PG("ActiveSheet");
        CCEdit1->Text=SH1.PG("Cells",1,1).PG("Value");
        for(int i=1;i<=10;i++)
        {
                for(int j=1;j<=10;j++)
                {
                        //AnsiString &S=IntToStr(i*100+j);
                        wchar_t* oleStr = SysAllocString(L"Hello");



                        SH1.PG("Cells",i,j).PS("Value",Variant(oleStr));
                }
        }
        V.PR("Save");
        //V.FN("Close");
        V.PR("Quit");
        V=Unassigned;
        WB1=Unassigned;
        SH1=Unassigned;
        Sys
        ShowMessage("Excel has been opened by BCB");
        SysFreeString(oleStr);



}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{

       // Button5->Enabled=false;
        MyDB->Connected=true;
        MyQuery->Close();
        MyQuery->SQL->Clear();
        MyQuery->SQL->Add("insert into Excel2000(Name,Gender,Age,Nationality,Address) Values(:Name,:Gender,:Age,:Nationality,:Address)");
        MyQuery->ParamByName("Name")->AsString=Edit1->Text;
        MyQuery->ParamByName("Gender")->AsString=Edit2->Text;
        MyQuery->ParamByName("Age")->AsInteger=StrToInt(Edit3->Text);
        MyQuery->ParamByName("Nationality")->AsString=Edit4->Text;
        MyQuery->ParamByName("Address")->AsString=Edit5->Text;
        MyQuery->ExecSQL();
        MyQuery->Close();
        MyQuery->SQL->Clear();
        MyQuery->SQL->Add("select  * from Excel2000");
        MyQuery->Open();


}
//---------------------------------------------------------------------------



void __fastcall TForm1::Button2Click(TObject *Sender)
{
        int button;
        button=Application->MessageBox("Do you really want to exit thisform?","Confirmation",MB_YESNO+MB_ICONQUESTION);
        if(button==IDYES)
        {
                Application->Terminate();
        }




}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button3Click(TObject *Sender)
{
        MyDB->Connected=true;
        MyQuery->Close();
        MyQuery->SQL->Clear();
        MyQuery->SQL->Add("select * from Excel2000");
        MyQuery->Open();
       //Button5->Enabled=false;
        
        




}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button5Click(TObject *Sender)
{
       /* Button3->Enabled=false;
        Button4->Enabled=false;
        ADOConnection->Connected=true;
        ADOQuery->Close();
        ADOQuery->SQL->Clear();
        ADOQuery->SQL->Add("select * from Excel2000");
        ADOQuery->Open();
                               */
                               
        



        



}
//---------------------------------------------------------------------------
这里面的内容比较多,可以搜索以前的贴子。

#3


我也想要我的邮箱:jb9802@sohu.com

#4


为什么就不用SERVER控件呢?

#5


我感觉Server的函数名称太奇怪了。不如用Variant

#6


给你一个我以前写的用server的例子
/ExportExcel为将query的查询结果转存为excel文件的函数
void __fastcall TForm2::ExportExcel(TExcelApplication *EA,TExcelWorkbook *EWB,TExcelWorksheet *EWS,AnsiString FileName,TDBGridEh *dbgrid)
{
RangePtr range;
AnsiString colstring;
try
 {
  Screen->Cursor=crHourGlass;
  dbgrid->DataSource->DataSet->DisableControls();
   try
    {
    EA->Connect();
    EWB->ConnectTo(EA->Workbooks->Add(Null,0));
    EWS->ConnectTo(EWB->Worksheets->get_Item("Sheet1"));
    if(dbgrid->DataSource->DataSet->Active)
      {
      dbgrid->DataSource->DataSet->First();
      //for中的代码完成列宽度及标题的设置(excel最多支持256个列)
      for(int i=0;i<dbgrid->Columns->Count;i++)
       {
       if(i/26>0)

          {
          if(i%26!=0)
            {
            colstring=char(i/26+64);
            colstring=colstring+char(i%26+65);
            }
          else
            {
            colstring=char(i/26+64);
            colstring=colstring+'A';
            }
          }
       else
          colstring=char(i%26+65);
       range=EWS->Get_Range(colstring+"1",colstring+"1");
       range->set_ColumnWidth(dbgrid->Columns->Items[i]->Field->DisplayWidth);
       EWS->Cells->set_Item(1,i+1,dbgrid->Columns->Items[i]->Title->Caption);
       }
      int row=2;
      //把grid中的数据写入excel
      while(!dbgrid->DataSource->DataSet->Eof)
        {
        for(int i=0;i<dbgrid->Columns->Count;i++)
          EWS->Cells->set_Item(row,i+1,dbgrid->Columns->Items[i]->Field->AsString);
        row++;
        dbgrid->DataSource->DataSet->Next();
        }
      EWB->SaveCopyAs(FileName);
      }
    }
   catch(...)
    {
     ShowMessage("这台电脑没有安装excel,不能保存!");
    }
 }
__finally
 {
 Screen->Cursor=crDefault;
 dbgrid->DataSource->DataSet->EnableControls();
 EWB->Close(false);
 EA->Quit();
 EA->Disconnect();
 }
}

#7


这个T不赖,打个记号先!

#8


to  hotxu 
好吧,多谢。

#9


关注

#1


内容比较多我有一个文档你要不要?

#2


我做了一个最简的例子:
//---------------------------------------------------------------------------

#ifndef MainH
#define MainH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <utilcls.h>
#include <math.h>

#include "CCEdit.h"
#include <Db.hpp>
#include <DBGrids.hpp>
#include <DBTables.hpp>
#include <Grids.hpp>
#include <ADODB.hpp>
#define  PG OlePropertyGet
#define  PS OlePropertySet
#define  FN OleFunction
#define  PR OleProcedure
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
        TButton *Button1;
        TButton *Button2;
        TCCEdit *CCEdit1;
        TButton *Button3;
        TDBGrid *DBGrid1;
        TDataSource *DataSource1;
        TDatabase *MyDB;
        TButton *Button4;
        TQuery *MyQuery;
        TLabel *Label1;
        TEdit *Edit1;
        TEdit *Edit2;
        TEdit *Edit3;
        TEdit *Edit4;
        TEdit *Edit5;
        TLabel *Label2;
        TLabel *Label3;
        TLabel *Label4;
        TLabel *Label5;
        TLabel *Label6;
        TButton *Button5;
        void __fastcall Button1Click(TObject *Sender);
        void __fastcall Button4Click(TObject *Sender);
        void __fastcall Button2Click(TObject *Sender);
        void __fastcall Button3Click(TObject *Sender);
        void __fastcall Button5Click(TObject *Sender);
private: // User declarations
public: // User declarations
        __fastcall TForm1(TComponent* Owner);
protected:
        virtual void __fastcall WndProc(TMessage &Message);

};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

.cpp File
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Main.h"
#include "utilcls.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "CCEdit"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::WndProc(TMessage &Message)
{
        if(Message.Msg==WM_SYSCOMMAND)
        {
                if(Message.WParam==SC_CLOSE || Message.WParam==SC_SCREENSAVE)
                {
                        Message.WParam=0;
                }
        }
        TForm::WndProc(Message);
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
        Variant V,WB1,SH1;
        V=Variant::CreateObject("Excel.Application");
        V.PS("Visible",true);
        V.PG("WorkBooks").PR("Open","c:\\Chenbin\\BCB\\Excel\\Book1.xls");
        WB1=V.PG("ActiveWorkBook");
        SH1=WB1.PG("ActiveSheet");
        CCEdit1->Text=SH1.PG("Cells",1,1).PG("Value");
        for(int i=1;i<=10;i++)
        {
                for(int j=1;j<=10;j++)
                {
                        //AnsiString &S=IntToStr(i*100+j);
                        wchar_t* oleStr = SysAllocString(L"Hello");



                        SH1.PG("Cells",i,j).PS("Value",Variant(oleStr));
                }
        }
        V.PR("Save");
        //V.FN("Close");
        V.PR("Quit");
        V=Unassigned;
        WB1=Unassigned;
        SH1=Unassigned;
        Sys
        ShowMessage("Excel has been opened by BCB");
        SysFreeString(oleStr);



}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{

       // Button5->Enabled=false;
        MyDB->Connected=true;
        MyQuery->Close();
        MyQuery->SQL->Clear();
        MyQuery->SQL->Add("insert into Excel2000(Name,Gender,Age,Nationality,Address) Values(:Name,:Gender,:Age,:Nationality,:Address)");
        MyQuery->ParamByName("Name")->AsString=Edit1->Text;
        MyQuery->ParamByName("Gender")->AsString=Edit2->Text;
        MyQuery->ParamByName("Age")->AsInteger=StrToInt(Edit3->Text);
        MyQuery->ParamByName("Nationality")->AsString=Edit4->Text;
        MyQuery->ParamByName("Address")->AsString=Edit5->Text;
        MyQuery->ExecSQL();
        MyQuery->Close();
        MyQuery->SQL->Clear();
        MyQuery->SQL->Add("select  * from Excel2000");
        MyQuery->Open();


}
//---------------------------------------------------------------------------



void __fastcall TForm1::Button2Click(TObject *Sender)
{
        int button;
        button=Application->MessageBox("Do you really want to exit thisform?","Confirmation",MB_YESNO+MB_ICONQUESTION);
        if(button==IDYES)
        {
                Application->Terminate();
        }




}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button3Click(TObject *Sender)
{
        MyDB->Connected=true;
        MyQuery->Close();
        MyQuery->SQL->Clear();
        MyQuery->SQL->Add("select * from Excel2000");
        MyQuery->Open();
       //Button5->Enabled=false;
        
        




}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button5Click(TObject *Sender)
{
       /* Button3->Enabled=false;
        Button4->Enabled=false;
        ADOConnection->Connected=true;
        ADOQuery->Close();
        ADOQuery->SQL->Clear();
        ADOQuery->SQL->Add("select * from Excel2000");
        ADOQuery->Open();
                               */
                               
        



        



}
//---------------------------------------------------------------------------
这里面的内容比较多,可以搜索以前的贴子。

#3


我也想要我的邮箱:jb9802@sohu.com

#4


为什么就不用SERVER控件呢?

#5


我感觉Server的函数名称太奇怪了。不如用Variant

#6


给你一个我以前写的用server的例子
/ExportExcel为将query的查询结果转存为excel文件的函数
void __fastcall TForm2::ExportExcel(TExcelApplication *EA,TExcelWorkbook *EWB,TExcelWorksheet *EWS,AnsiString FileName,TDBGridEh *dbgrid)
{
RangePtr range;
AnsiString colstring;
try
 {
  Screen->Cursor=crHourGlass;
  dbgrid->DataSource->DataSet->DisableControls();
   try
    {
    EA->Connect();
    EWB->ConnectTo(EA->Workbooks->Add(Null,0));
    EWS->ConnectTo(EWB->Worksheets->get_Item("Sheet1"));
    if(dbgrid->DataSource->DataSet->Active)
      {
      dbgrid->DataSource->DataSet->First();
      //for中的代码完成列宽度及标题的设置(excel最多支持256个列)
      for(int i=0;i<dbgrid->Columns->Count;i++)
       {
       if(i/26>0)

          {
          if(i%26!=0)
            {
            colstring=char(i/26+64);
            colstring=colstring+char(i%26+65);
            }
          else
            {
            colstring=char(i/26+64);
            colstring=colstring+'A';
            }
          }
       else
          colstring=char(i%26+65);
       range=EWS->Get_Range(colstring+"1",colstring+"1");
       range->set_ColumnWidth(dbgrid->Columns->Items[i]->Field->DisplayWidth);
       EWS->Cells->set_Item(1,i+1,dbgrid->Columns->Items[i]->Title->Caption);
       }
      int row=2;
      //把grid中的数据写入excel
      while(!dbgrid->DataSource->DataSet->Eof)
        {
        for(int i=0;i<dbgrid->Columns->Count;i++)
          EWS->Cells->set_Item(row,i+1,dbgrid->Columns->Items[i]->Field->AsString);
        row++;
        dbgrid->DataSource->DataSet->Next();
        }
      EWB->SaveCopyAs(FileName);
      }
    }
   catch(...)
    {
     ShowMessage("这台电脑没有安装excel,不能保存!");
    }
 }
__finally
 {
 Screen->Cursor=crDefault;
 dbgrid->DataSource->DataSet->EnableControls();
 EWB->Close(false);
 EA->Quit();
 EA->Disconnect();
 }
}

#7


这个T不赖,打个记号先!

#8


to  hotxu 
好吧,多谢。

#9


关注