C++ Builder XE 读写EXCEL表格内容与设置属性

源码下载:https://download.csdn.net/download/lzksword/14039806

//---------------------------------------------------------------------------
#include <Comobj.hpp>   //OLE需要
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"


TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
//★打开工作簿★
Form1->OpenDialog1->Title="请选择导入的文件!";
//设置对话框
//OpenDialog1->InitialDir=Path;          //"f:/";
//初始化路径
Form1->OpenDialog1->Filter="Text Files(*.xls)|*.xls|ALL Files(*.*)|*.*";
//设置文件过滤
Form1->OpenDialog1->DefaultExt="XLS";
//设置缺省扩展名
if(Form1->OpenDialog1->Execute())
{
     Variant Ex,Wb,Sh1;
     String tems=Form1->OpenDialog1->FileName.c_str() ;  //GetCurrentDir()+
     try
      {
        Ex =GetActiveOleObject(L"Excel.Application") ;
        int co= Ex.OlePropertyGet(L"Workbooks").OlePropertyGet(L"Count");
        for(int j=1;j<=co;j++)
        {
          if( Ex.OlePropertyGet(L"Workbooks",j).OlePropertyGet(L"Name")== tems)
          {
            MessageBox(Form1->Handle,L"myexcel已经被打开,不可继续。",0, 48 ) ;
            Ex = Unassigned ;
            //Using= false ;
            return;
          }
        }
      }
      catch(...)
      {
        try
        {
          Ex = CreateOleObject(L"Excel.Application");
        }
        catch(...)
        {
          MessageBox(Form1->Handle ,L"启动Excel出错,可能是没有安装Excel。",0, 16);
          Ex = Unassigned;
          //Using = false ;
          return;
        }
      }
      Screen->Cursor= crHourGlass;
      //打开表格文档
      Ex.OlePropertyGet(L"Workbooks").OleFunction(L"Open",StringToOleStr(tems),false,true);     //文件,更新链接,只读
      Sleep(500);
      Application->ProcessMessages() ;
      //设置为显示为真
      Ex.OlePropertySet(L"Visible", true) ;
      //WB当前活动工作薄
      Wb = Ex.OlePropertyGet(L"ActiveWorkBook");
      //获得指定的Sheet
      Sh1 = Wb.OlePropertyGet(L"Sheets",1);
      //SetWindowPos(Ex.OlePropertyGet(L"hWnd").byref,0,0,0,0,0,0x43);   //最前
      //窗口最大化
      Ex.OlePropertyGet(L"ActiveWindow").OlePropertySet(L"WindowState", 2);
      //写入表格(2,2),(2,4),(3,2)
      Sh1.OlePropertyGet(L"Cells",2,2).OlePropertySet(L"Value", StringToOleStr(String(L"你的字符")));
      Sh1.OlePropertyGet(L"Cells",2,4).OlePropertySet(L"Value", StringToOleStr(FormatDateTime(L"yyyy-M-d", Now())));   //时间
      Sh1.OlePropertyGet(L"Cells",3,2).OlePropertySet(L"Value",12.365) ;   //数字
      //用Get则能得到单元格(1,1)的Value属性,font-bold, Italic,size, name,color(rgb)
      Memo1->Lines->Append( "内容: "+Sh1.OlePropertyGet(L"Cells",1,1).OlePropertyGet("Value"));
      Memo1->Lines->Append( "粗体: "+Sh1.OlePropertyGet(L"Cells",1,1).OlePropertyGet("Font").OlePropertyGet("Bold") );
      //Memo1->Lines->Append( "样式: "+Sh1.OlePropertyGet(L"Cells",1,1).OlePropertyGet("Font").OlePropertyGet("FontStyle"));
      Memo1->Lines->Append( "字号: "+Sh1.OlePropertyGet(L"Cells",1,1).OlePropertyGet("Font").OlePropertyGet("Size"));
      Memo1->Lines->Append( "斜体: "+Sh1.OlePropertyGet(L"Cells",1,1).OlePropertyGet("Font").OlePropertyGet("Italic"));
      Memo1->Lines->Append( "字体: "+Sh1.OlePropertyGet(L"Cells",1,1).OlePropertyGet("Font").OlePropertyGet("name"));
      Memo1->Lines->Append( "颜色: "+Sh1.OlePropertyGet(L"Cells",1,1).OlePropertyGet("Font").OlePropertyGet("color"));
      //Memo1->Lines->Append( "字体大小: "+Sh1.OlePropertyGet(L"Cells",1,1).OlePropertyGet("Font").OlePropertyGet("FontSize") );
      //恢复鼠标指针
      Screen->Cursor=crDefault ;
      //保存表格文档
      Wb.OlePropertySet(L"Saved",true) ;   //
      Sh1.OleFunction(L"Activate") ;
      Sh1 = Unassigned;
      Wb  = Unassigned;
      Ex  = Unassigned;
      Application->ProcessMessages();
  }


}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lzksword

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值