关于BCB中,Excel的一些操作(存底)

将一个Excel或sheet的部分内容复制到另一个Excel或sheet,并设置边框

//---------------------------------------------------------------------------

 

void __fastcall TForm1::Button1Click(TObject *Sender)

{

   Variant ex,wb1,wb2,sh1,sh2,ERange,EBorders;

   AnsiString temp1;

   AnsiString temp2;

   AnsiString strRange;

   int  r = 1,icol = 1, irow = 1, c = 1;

   ex=CreateOleObject("Excel.Application");

   ex.OlePropertySet("Visible",true);

   wb2=ex.OlePropertyGet("Workbooks").OleFunction("Open","c:\\abc.xls") ;

   wb1=ex.OlePropertyGet("Workbooks").OleFunction("open","c:\\123.xls");

   sh2=wb2.OlePropertyGet("ActiveSheet");

   sh1=wb1.OlePropertyGet("ActiveSheet");

   temp2=sh2.OlePropertyGet("Cells",7,6).OlePropertyGet("Value");

   sh1.OlePropertyGet("Cells",7,6).OlePropertySet("Value",temp2.c_str());

   temp1=sh1.OlePropertyGet("Cells",7,6).OlePropertyGet("Value");

   Edit1->Text= temp1;

 

   while(irow-20)//for(irow=5;irow<9;irow++)

   {

      for(icol=1,c=1;icol<10;icol++)

      {

           temp2=sh2.OlePropertyGet("Cells",r,c++).OlePropertyGet("Value");

           sh1.OlePropertyGet("Cells",irow,icol).OlePropertySet("Value",temp2.c_str());

      }

       strRange = "A"+IntToStr(irow)+":I"+IntToStr(irow);

       ERange = sh1.OlePropertyGet("Range",strRange.c_str());

       EBorders = ERange.OlePropertyGet("Borders");

       EBorders.OlePropertySet("linestyle",xlContinuous);

       EBorders.OlePropertySet("weight",xlThin);

       EBorders.OlePropertySet("colorindex",xlAutomatic);

       irow++;

       r++;

   }

 }

//---------------------------------------------------------------------------

 

选择某一个sheet,明重新命名

void __fastcall TForm1::Button1Click(TObject *Sender)

{

  Variant ex,wb,newxls,sh;

    try

    {

      ex=CreateOleObject("Excel.Application");//启动Excel

      ex.OlePropertySet("Visible",(Variant)true);//使Excel启动后可见

      ex.OlePropertyGet("WorkBooks").OleProcedure("ADD",3);//新建一新工作薄(加上这一句,会有两个Excel窗口,同时关闭)

      ex.OlePropertySet("Windowstate",1);//Excel启动后窗体状态:1(xlNormal)正常显示(Excel上次关闭时是什么状态,启动后就是什么状态),2(xlMinimized)最小化(不是缩小到任务栏),3(xlMaximized)最大化

    

      // newxls=(ex.OlePropertyGet("Workbooks")).OleFunction("Add");//①//使用ExcelApp的Exec方法新建一有3个工作表的默认工作薄

      newxls=(ex.OlePropertyGet("Workbooks")).OleFunction("Add",1);//创建有单个工作表的工作簿

      newxls=ex.OlePropertyGet("workbooks").OleFunction("open", "c:\\123.xls");//打开已存在的文件,使用时可将上面关于新建①的那行屏蔽掉

      //sh=newxls.OlePropertyGet("ActiveSheet");

   }

   catch(...)

   {

      ShowMessage("启动Excel出错,可能沒有安裝Excel");

      return;

   }

   sh.OlePropertyGet("Application").OlePropertySet("DisplayAlerts",true); //打开Excel的警告提示,如提示保存等

   newxls.OlePropertyGet("Sheets", 2).OleProcedure("Select");//选择第二工作表

   //sh = newxls.OlePropertyGet("ActiveSheet");//选择第二工作表

   sh.OlePropertySet("Name", "我们的家园");//重命名当前工作表

   int nSheetCount=newxls.OlePropertyGet("Sheets").OlePropertyGet("Count");//取得工作表总数

   Edit1->Text=nSheetCount;

}

//---------------------------------------------------------------------------

 

将Excel文件从一个地方复制到另一个地方,并重新命名

 

void __fastcall TForm1::Button1Click(TObject *Sender)

{

  Variant ex,wb,sh;

  ex = CreateOleObject("Excel.Application");

  ex.OlePropertySet("Visible",true);

  wb = ex.OlePropertyGet("WorkBooks").OleFunction("Open","c:\\123.xls");

  sh = wb.OlePropertyGet("activesheet");

  //sh.OlePropertyGet("Columns",1).OlePropertySet("Value","123456789") ;

  sh.OlePropertyGet("Range","A1:A9").OlePropertySet("Value","19") ;

  //wb.OleProcedure("SaveAs","c:\\report\\456.xls");

  CopyFile("c:\\123456.xls","c:\\report\\1456.xls",true);  //将123456.xls复制到report文件夹中并命名为1456.xls

  CopyFile("c:\\123456.xls","c:\\report\\2456.xls",false);

  AnsiString FN = GetCurrentDir()+"\\report\\456.xls";// C:\Program Files\Borland\CBuilder5\Projects\report\456.xls

  Edit1->Text = FN;

  if(!FileExists(FN))

  {

  Application->MessageBox("报表模版文件不存在","错误",MB_ICONSTOP|MB_OK);

  return;

  }

  Application->MessageBox("成功完成","提示",MB_ICONINFORMATION|MB_OK);

  CopyFile("c:\\123456.xls",FN.c_str(),false);

 

}

//---------------------------------------------------------------------------

 

 

输入当前时间和日期

 

//---------------------------------------------------------------------------

 

void __fastcall TForm1::Button1Click(TObject *Sender)

{

AnsiString WDate,ctime,strPtrDate;

//DateSeparator = '-';

//ShortDateFormat = "yyyy/m/d";

WDate = DateToStr(Date());

ctime = TimeToStr(Time());

AnsiString strYear = strPtrDate.SubString(1,4);

strPtrDate = strPtrDate.SubString(6,strPtrDate.Length()-5);

AnsiString strMonth = strPtrDate.SubString(1,strPtrDate.Pos("-")-1);

AnsiString strDay = strPtrDate.SubString(strPtrDate.Pos("-")+1, strPtrDate.Length()- strPtrDate.Pos("-"));

strPtrDate = strYear+"年"+strMonth+"月"+strDay+"日";

Edit3->Text=strPtrDate;

Edit1->Text = WDate;

Edit2->Text = ctime;

}

//---------------------------------------------------------------------------

 

方法一

 

//---------------------------------------------------------------------------

 

#include <vcl.h>

#pragma hdrstop

 

#include "sheet_datename_simple.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

#pragma resource "*.dfm"

#include <Utilcls.h>

#include "Excel_2K_SRVR.h"

#include "ComObj.hpp"

TForm1 *Form1;

//---------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)

        : TForm(Owner)

{

}

//---------------------------------------------------------------------------

 

void __fastcall TForm1::Button1Click(TObject *Sender)

{

Variant ex,wb,sh;

try

{

  ex = CreateOleObject("Excel.Application");

}

catch(...)

{

Application->MessageBox("无法启动Excel","错误",MB_ICONSTOP|MB_OK);

return;

}

ex.OlePropertySet("Visible",false);

AnsiString NDate;

DateSeparator = '-';

NDate = DateToStr(Date());

AnsiString DateYear = NDate.SubString(1,4);

NDate = NDate.SubString(6,NDate.Length()-5);

AnsiString DateMonth = NDate.SubString(1,NDate.Pos("-")-1);

AnsiString DateDay = NDate.SubString(NDate.Pos("-")+1,NDate.Length()-NDate.Pos("-"));

NDate = DateMonth+"月"+DateDay+"日";//sheet的名字

AnsiString TDate = DateMonth+"月"+DateDay+"日 报表";//标题

AnsiString Direc = "c:\\"+DateMonth+"月.xls";//路径

if(FileExists(Direc))

{

  wb = ex.OlePropertyGet("workbooks").OleFunction("open",Direc.c_str());

  sh = wb.OlePropertyGet("Activesheet");

  AnsiString Na = sh.OlePropertyGet("Name");

  if(Na!=NDate)

  {

    Variant bef1,aft1;

    int count = wb.OlePropertyGet("sheets").OlePropertyGet("count");

    aft1=wb.OlePropertyGet("sheets",count);

    wb.OlePropertyGet("sheets").OleProcedure("Add",bef1.NoParam(),aft1);

    sh = wb.OlePropertyGet("Activesheet");

    sh.OlePropertySet("Name",NDate.c_str());

    sh.OlePropertyGet("Cells",1,1).OlePropertySet("Value",TDate.c_str());

    sh.OlePropertyGet("Cells",2,1).OlePropertySet("Value","列名1");

    sh.OlePropertyGet("Cells",2,2).OlePropertySet("Value","列名2");

    sh.OlePropertyGet("Cells",2,3).OlePropertySet("Value","列名3");

    sh.OlePropertyGet("Cells",2,4).OlePropertySet("Value","列名4");

    sh.OlePropertyGet("Cells",2,5).OlePropertySet("Value","列名5");

    sh.OlePropertyGet("Cells",2,6).OlePropertySet("Value","列名6");

    sh.OlePropertyGet("Cells",2,7).OlePropertySet("Value","列名7");

  }

 

}

else

{

  wb = ex.OlePropertyGet("workbooks").OleFunction("Add",1);

  wb.OleFunction("SaveAs",Direc.c_str());

  sh = wb.OlePropertyGet("Activesheet");

  sh.OlePropertySet("Name",NDate.c_str());

  sh.OlePropertyGet("Cells",1,1).OlePropertySet("Value",TDate.c_str());

  sh.OlePropertyGet("Cells",2,1).OlePropertySet("Value","列名1");

  sh.OlePropertyGet("Cells",2,2).OlePropertySet("Value","列名2");

  sh.OlePropertyGet("Cells",2,3).OlePropertySet("Value","列名3");

  sh.OlePropertyGet("Cells",2,4).OlePropertySet("Value","列名4");

  sh.OlePropertyGet("Cells",2,5).OlePropertySet("Value","列名5");

  sh.OlePropertyGet("Cells",2,6).OlePropertySet("Value","列名6");

  sh.OlePropertyGet("Cells",2,7).OlePropertySet("Value","列名7");

}

wb.OleProcedure("Save");        //保存表格

wb.OleProcedure("Close");                 //关闭表格

ex.OleFunction("Quit");                             //退出Excel

 

}

//---------------------------------------------------------------------------

 

 

方法二:

 

 

 

//---------------------------------------------------------------------------

 

#include <vcl.h>

#pragma hdrstop

 

#include "sheet_name_date_1.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

#pragma resource "*.dfm"

#include <Utilcls.h>

#include <Excel_2K_SRVR.h>

#include <ComObj.hpp>

 

TForm1 *Form1;

//---------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)

        : TForm(Owner)

{

}

//---------------------------------------------------------------------------

 

void __fastcall TForm1::Button1Click(TObject *Sender)

{

  Variant ex,wb,sh;

  try

  {

    ex = CreateOleObject("Excel.Application");//启动Excel

    ex.OlePropertySet("Visible",(Variant)false);  //使Excel启动后可见

  }

  catch(...)

  {

   Application->MessageBox("无法启动Excel!","错误",MB_ICONSTOP|MB_OK);

  }

  AnsiString WDate;

  DateSeparator = '-';

  ShortDateFormat = "yyyy/m/d";

  WDate = DateToStr(Date());                 //取当前日期

  AnsiString DateYear = WDate.SubString(1,4);

  WDate = WDate.SubString(6,WDate.Length()-5);

  AnsiString DateMonth = WDate.SubString(1,WDate.Pos("-")-1);

  AnsiString DateDay = WDate.SubString(WDate.Pos("-")+1,WDate.Length()-WDate.Pos("-"));

  WDate = DateMonth + "月" + DateDay + "日";              //sheet的名字

  AnsiString EDate = DateMonth+"月"+DateDay+"日报表";      //报表标题

  AnsiString Direc = "c:\\" + DateMonth +"月.xls";    //报表路径

  if(DateDay=="1")

  {

    if(FileExists(Direc))

    {

      wb = ex.OlePropertyGet("workbooks").OleFunction("open",Direc.c_str());//打开本月报表工作簿

      sh = wb.OlePropertyGet("Activesheet");

      return;

    }

    else

    {

      wb=ex.OlePropertyGet("workbooks").OleFunction("Add",1);  //建立新的报表工作簿

      wb.OleFunction("SaveAs",Direc.c_str());

      sh = wb.OlePropertyGet("Activesheet");

 

      sh.OlePropertySet("Name",WDate.c_str());

      sh.OlePropertyGet("cells",1,1).OlePropertySet("Value",EDate.c_str());

      sh.OlePropertyGet("cells",2,1).OlePropertySet("Value","列名1");

      sh.OlePropertyGet("cells",2,2).OlePropertySet("Value","列名2");

      sh.OlePropertyGet("cells",2,3).OlePropertySet("Value","列名3");

      sh.OlePropertyGet("cells",2,4).OlePropertySet("Value","列名4");

      sh.OlePropertyGet("cells",2,5).OlePropertySet("Value","列名5");

      sh.OlePropertyGet("cells",2,6).OlePropertySet("Value","列名6");

      sh.OlePropertyGet("cells",2,7).OlePropertySet("Value","列名7");

    }

  }

  else

  {

    if(FileExists(Direc))

    {

      wb = ex.OlePropertyGet("workbooks").OleFunction("open",Direc.c_str());//打开本月报表工作簿

      sh = wb.OlePropertyGet("Activesheet");

      AnsiString EName = sh.OlePropertyGet("Name");

      if(EName!=WDate)

      { Variant bef1,aft1;

        int icount = wb.OlePropertyGet("sheets").OlePropertyGet("count");

        aft1 = wb.OlePropertyGet("sheets",icount);

        wb.OlePropertyGet("sheets").OleProcedure("Add",bef1.NoParam(),aft1);

        sh = wb.OlePropertyGet("Activesheet");

        sh.OlePropertySet("Name",WDate.c_str());

        sh.OlePropertyGet("cells",1,1).OlePropertySet("Value",EDate.c_str());

        sh.OlePropertyGet("cells",2,1).OlePropertySet("Value","列名1");

        sh.OlePropertyGet("cells",2,2).OlePropertySet("Value","列名2");

        sh.OlePropertyGet("cells",2,3).OlePropertySet("Value","列名3");

        sh.OlePropertyGet("cells",2,4).OlePropertySet("Value","列名4");

        sh.OlePropertyGet("cells",2,5).OlePropertySet("Value","列名5");

        sh.OlePropertyGet("cells",2,6).OlePropertySet("Value","列名6");

        sh.OlePropertyGet("cells",2,7).OlePropertySet("Value","列名7");

      }

      else

 

      return;

    }

    else

     {

      wb=ex.OlePropertyGet("workbooks").OleFunction("Add",1);  //建立新的报表工作簿

 

      wb.OleFunction("SaveAs",Direc.c_str());

      sh = wb.OlePropertyGet("Activesheet");

      sh.OlePropertySet("Name",WDate.c_str());

      sh.OlePropertyGet("cells",1,1).OlePropertySet("Value",EDate.c_str());

      sh.OlePropertyGet("cells",2,1).OlePropertySet("Value","列名1");

      sh.OlePropertyGet("cells",2,2).OlePropertySet("Value","列名2");

      sh.OlePropertyGet("cells",2,3).OlePropertySet("Value","列名3");

      sh.OlePropertyGet("cells",2,4).OlePropertySet("Value","列名4");

      sh.OlePropertyGet("cells",2,5).OlePropertySet("Value","列名5");

      sh.OlePropertyGet("cells",2,6).OlePropertySet("Value","列名6");

      sh.OlePropertyGet("cells",2,7).OlePropertySet("Value","列名7");

     }

  }

  wb.OleProcedure("Save");    //保存表格

  wb.OleProcedure("Close");   //关闭表格

  ex.OleFunction("Quit");     //退出Excel

 }

 

                                                           

//---------------------------------------------------------------------------

 

   

 

 

 

 

//---------------------------------------------------------------------------

 

#include <vcl.h>

#pragma hdrstop

 

#include "sheet_name_date.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

#pragma resource "*.dfm"

#include <Utilcls.h>

#include <Excel_2K_SRVR.h>

#include <ComObj.hpp>

 

TForm1 *Form1;

//---------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)

        : TForm(Owner)

{

}

//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)

{

  Variant ex,wb,sh;

  ex = CreateOleObject("Excel.Application");//启动Excel

  ex.OlePropertySet("Visible",(Variant)true);  //使Excel启动后可见

  AnsiString WDate;

  DateSeparator = '-';

  ShortDateFormat = "yyyy/m/d";

  WDate = DateToStr(Date());                 //取当前日期

  AnsiString DateYear = WDate.SubString(1,4);

  WDate = WDate.SubString(6,WDate.Length()-5);

  AnsiString DateMonth = WDate.SubString(1,WDate.Pos("-")-1);

  AnsiString DateDay = WDate.SubString(WDate.Pos("-")+1,WDate.Length()-WDate.Pos("-"));

  WDate = DateMonth + "月" + DateDay + "日";

  AnsiString EDate = DateMonth+"月"+DateDay+"报表";      //报表标题

  AnsiString Direc = "c:\\" + DateMonth +".xls";    //报表路径

  if(DateDay=="1")

  {

    wb=ex.OlePropertyGet("workbooks").OleFunction("Add",1);  //建立新的报表工作簿

 

    wb.OleFunction("SaveAs",Direc.c_str());

  }

  else

  {

    try

    {

 

      wb = ex.OleFunction("Open",Direc.c_str());               //打开本月报表工作簿

    }

    catch(...)

    {

     wb=ex.OlePropertyGet("workbooks").OleFunction("Add",1);  //建立新的报表工作簿

 

     wb.OleFunction("SaveAs",Direc.c_str());

    }

  }

  int icount = wb.OlePropertyGet("sheets").OlePropertyGet("count");

 

  if(icount==1)

    sh = wb.OlePropertyGet("Activesheet");

 

  else

  {

    Variant bef1;

    wb.OlePropertyGet("sheets").OleProcedure("Add",bef1.NoParam(),icount);

    sh = wb.OlePropertyGet("Activesheet");

  }

  AnsiString EName = sh.OlePropertyGet("Name");

  if(EName!=WDate)

  {

    sh.OlePropertySet("Name",WDate.c_str());

 

    sh.OlePropertyGet("cells",1,1).OlePropertySet("Value",EDate.c_str());

    sh.OlePropertyGet("cells",2,1).OlePropertySet("Value","列名1");

    sh.OlePropertyGet("cells",2,2).OlePropertySet("Value","列名2");

    sh.OlePropertyGet("cells",2,3).OlePropertySet("Value","列名3");

    sh.OlePropertyGet("cells",2,4).OlePropertySet("Value","列名4");

    sh.OlePropertyGet("cells",2,5).OlePropertySet("Value","列名5");

    sh.OlePropertyGet("cells",2,6).OlePropertySet("Value","列名6");

    sh.OlePropertyGet("cells",2,7).OlePropertySet("Value","列名7");

  }

  else

  return;

 

}

//---------------------------------------------------------------------------

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
完整版:https://download.csdn.net/download/qq_27595745/89522468 【课程大纲】 1-1 什么是java 1-2 认识java语言 1-3 java平台的体系结构 1-4 java SE环境安装和配置 2-1 java程序简介 2-2 计算机的程序 2-3 java程序 2-4 java类库组织结构和文档 2-5 java虚拟机简介 2-6 java的垃圾回收器 2-7 java上机练习 3-1 java语言基础入门 3-2 数据的分类 3-3 标识符、关键字和常量 3-4 运算符 3-5 表达式 3-6 顺序结构和选择结构 3-7 循环语句 3-8 跳转语句 3-9 MyEclipse工具介绍 3-10 java基础知识章节练习 4-1 一维数组 4-2 数组应用 4-3 多维数组 4-4 排序算法 4-5 增强for循环 4-6 数组和排序算法章节练习 5-0 抽象和封装 5-1 面向过程的设计思想 5-2 面向对象的设计思想 5-3 抽象 5-4 封装 5-5 属性 5-6 方法的定义 5-7 this关键字 5-8 javaBean 5-9 包 package 5-10 抽象和封装章节练习 6-0 继承和多态 6-1 继承 6-2 object类 6-3 多态 6-4 访问修饰符 6-5 static修饰符 6-6 final修饰符 6-7 abstract修饰符 6-8 接口 6-9 继承和多态 章节练习 7-1 面向对象的分析与设计简介 7-2 对象模型建立 7-3 类之间的关系 7-4 软件的可维护与复用设计原则 7-5 面向对象的设计与分析 章节练习 8-1 内部类与包装器 8-2 对象包装器 8-3 装箱和拆箱 8-4 练习题 9-1 常用类介绍 9-2 StringBuffer和String Builder类 9-3 Rintime类的使用 9-4 日期类简介 9-5 java程序国际化的实现 9-6 Random类和Math类 9-7 枚举 9-8 练习题 10-1 java异常处理 10-2 认识异常 10-3 使用try和catch捕获异常 10-4 使用throw和throws引发异常 10-5 finally关键字 10-6 getMessage和printStackTrace方法 10-7 异常分类 10-8 自定义异常类 10-9 练习题 11-1 Java集合框架和泛型机制 11-2 Collection接口 11-3 Set接口实现类 11-4 List接口实现类 11-5 Map接口 11-6 Collections类 11-7 泛型概述 11-8 练习题 12-1 多线程 12-2 线程的生命周期 12-3 线程的调度和优先级 12-4 线程的同步 12-5 集合类的同步问题 12-6 用Timer类调度任务 12-7 练习题 13-1 Java IO 13-2 Java IO原理 13-3 流类的结构 13-4 文件流 13-5 缓冲流 13-6 转换流 13-7 数据流 13-8 打印流 13-9 对象流 13-10 随机存取文件流 13-11 zip文件流 13-12 练习题 14-1 图形用户界面设计 14-2 事件处理机制 14-3 AWT常用组件 14-4 swing简介 14-5 可视化开发swing组件 14-6 声音的播放和处理 14-7 2D图形的绘制 14-8 练习题 15-1 反射 15-2 使用Java反射机制 15-3 反射与动态代理 15-4 练习题 16-1 Java标注 16-2 JDK内置的基本标注类型 16-3 自定义标注类型 16-4 对标注进行标注 16-5 利用反射获取标注信息 16-6 练习题 17-1 顶目实战1-单机版五子棋游戏 17-2 总体设计 17-3 代码实现 17-4 程序的运行与发布 17-5 手动生成可执行JAR文件 17-6 练习题 18-1 Java数据库编程 18-2 JDBC类和接口 18-3 JDBC操作SQL 18-4 JDBC基本示例 18-5 JDBC应用示例 18-6 练习题 19-1 。。。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值