最全的OLE操作Excel的完整代码
时间:2025-05-15
时间:2025-05-15
最全的OLE操作Excel的完整代码
#include<Utilcls.h>
#include "Excel_2K_SRVR.h"
//#include "ComObj.hpp"
/*-------------------------------------------------
//谨慎的思考310032649原创文章
//目前真正最全的OLE操作Excel的完整代码
//版本:2007.01.15.01
//C++Builder专家组http://www.77cn.com.cn原创文章
//转载请保留本版权信息,谢谢合作
--------------------------------------------------/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString str1="asvasd";//要入库的数据
Variant ex,newxls,sh;
try{
ex=CreateOleObject("Excel.Application");//启动Excel
ex.OlePropertySet("Visible",(Variant)true);//使Excel启动后可见
//ex.OlePropertyGet("WorkBooks").OleProcedure("ADD");//新建一新工作薄(加上这一句,会有两个Excel窗口,同时关闭)
//ex.OlePropertySet("Visible",(Variant)false);//使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;
}
//Excel的警告提示:
sh.OlePropertyGet("Application").OlePropertySet("DisplayAlerts",false);//关闭Excel的警告提示,如提示保存等
//sh.OlePropertyGet("Application").OlePropertySet("DisplayAlerts",true);//打开Excel的警告提示,如提示保存等
//选择工作表:
//newxls.OlePropertyGet("Sheets", 2).OleProcedure("Select");//选择第二工作表
//sh = newxls.OlePropertyGet("ActiveSheet");//选择第二工作表
//重命名工作表:
//sh.OlePropertySet("Name", "Sheet的新名字");//重命名当前工作表
//取得工作表总数:
int nSheetCount=newxls.OlePropertyGet("Sheets").OlePropertyGet("Count");//取得工作表总数
Edit1->Text=nSheetCount;
/*-------------------------------------------------
//目前真正最全的OLE操作Excel的完整代码
//版本:2007.01.15.01
//C++Builder专家组http://www.77cn.com.cn原创文章
//转载请保留本版权信息,谢谢合作
--------------------------------------------------/
//新建工作表并重命名:
try{
Variant bef1,aft1;
int count=ex.OlePropertyGet("sheets").OlePropertyGet("count");
aft1=ex.OlePropertyGet("sheets",count);
ex.OlePropertyGet("sheets").OleProcedure("Add",bef1.NoParam() , aft1);
sh = ex.OlePropertyGet("ActiveSheet");
sh.OlePropertySet("Name","增加的sheet的名字");//名字不能重复
}catch(...){
//ShowMessage ("There's something wrong with your excel file./nPlease check it!"); }
//指定状态栏显示的文本:
//ex.OlePropertySet ("StatusBar","您好,请您稍等。正在查询!");//设置
//ex.OlePropertySet ("StatusBar", false);//还原成默认值
//指定标题:Exec窗口标题栏最左边显示的文本
//ex.OlePropertySet("Caption","查询系统");
//插入图片:
sh.OlePropertyGet("Shapes").OleFunction("AddPicture","c:\\123.gif",false,true,sh.OlePropertyGet("Range","B10").OlePropertyGet("Left"),sh.OlePropertyGet("Range","B10").OlePropertyGet("Top"),-1,-1);//插入图片
//使指定单元格里面的数字以文本形式存储:
sh.OlePropertyGet("Cells", 2, 2).OlePropertySet("NumberFormatLocal", "@");//使指定单元格里面的数字以文本形式存储,可以省略
//使用下划线:
//sh.OlePropertyGet("Cells").OlePropertyGet("Font").OlePropertySet("Underline",true);//在所有单元格中使用下划线
//sh.OlePropertyGet("Cells",1,1).OlePropertyGet("Font").OlePropertySet("Underline",true);//在指定单元格中使用下划线
//使用删除线:
sh.OlePropertyGet("Cells",i,1).OlePropertyGet("Font").OlePropertySet("Strikethrough",true);//在指定单元格中使用删除线
//使用斜体:
//sh.OlePropertyGet("Cells").OlePropertyGet("Font").OlePropertySet("Italic",true);//在所有单元格中使用斜体
//sh.OlePropertyGet("Cells",1,1).OlePropertyGet("Font").OlePropertySet("Italic",true);//在指定单元格中使用斜体
/*-------------------------------------------------
//目前真正最全的OLE操作Excel的完整代码
//版本:2007.01.15.01
//C++Builder专家组http://www.77cn.com.cn原创文章
//转载请保留本版权信息,谢谢合作