源码下载: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();
}
}