最全的OLE操作Excel的完整代码(2)

/*-------------------------------------------------
//目前真正最全的OLE操作Excel的完整代码
//版本:2007.01.15.01
//C++Builder专家组www.3322ee.com原创文章
//转载请保留本版权信息,谢谢合作
--------------------------------------------------/
//打印设定:
//sh.OlePropertyGet("PageSetup").OlePropertySet("PrintTitleRows","$2:$2");//打印标题列
//sh.OlePropertyGet("PageSetup").OlePropertySet("PrintTitleColumns","$A:$A");//打印标题栏
//sh.OlePropertyGet("PageSetup").OlePropertySet("PrintArea","$A$1:$I$15");//打印范围
//sh.OlePropertyGet("PageSetup").OlePropertySet("PrintHeadings",false);//栏名列号
//sh.OlePropertyGet("PageSetup").OlePropertySet("PrintGridlines",false);//打印网格线
//sh.OlePropertyGet("PageSetup").OlePropertySet("PrintComments",xlPrintNoComments);//批注"无"
//sh.OlePropertyGet("PageSetup").OlePropertySet("PrintQuality",300);//打印品质
//sh.OlePropertyGet("PageSetup").OlePropertySet("CenterHorizontally",true);//水平置中
//sh.OlePropertyGet("PageSetup").OlePropertySet("CenterVertically",false);//垂直置中
//sh.OlePropertyGet("PageSetup").OlePropertySet("Orientation",xlLandscape);//横向打印 (xlPortrait 纵向打印)
//sh.OlePropertyGet("PageSetup").OlePropertySet("Draft",false);//草稿品质
//sh.OlePropertyGet("PageSetup").OlePropertySet("PaperSize",xlPaperA4);//纸张大小
//sh.OlePropertyGet("PageSetup").OlePropertySet("FirstPageNumber",xlAutomatic);//起始页码
//sh.OlePropertyGet("PageSetup").OlePropertySet("Order",xlDownThenOver);//循栏打印 (xlOverThenDown 循列打印)
//sh.OlePropertyGet("PageSetup").OlePropertySet("BlackAndWhite",False);//储存格单色打印
//sh.OlePropertyGet("PageSetup").OlePropertySet("Zoom",100);//缩放比例

//设定单元格背景色:
//sh.OlePropertyGet("Cells",2,2).OlePropertyGet("Interior").OlePropertySet("ColorIndex",3);//设定指定单元格的背景色

//设定图样:xlGray50可以是1,2,3等数字
//sh.OlePropertyGet("Cells",4,2).OlePropertyGet("Interior").OlePropertySet("Pattern",xlGray50);
//sh.OlePropertyGet("Range","A1:C10").OlePropertyGet("Interior").OlePropertySet("Pattern",xlGray50);//在指定区域设置图样

//设置单元格中的文字距左:
//sh.OlePropertyGet("Cells",1,1).OlePropertySet("HorizontalAlignment",xlLeft);//设置单元格中的文字距左
//sh.OlePropertyGet("Cells",1,1).OlePropertySet("VerticalAlignment",xlCenter);//作用不明(垂直和水平居中?)
//设置单元格中的文字距中:
sh.OlePropertyGet("Cells",1,1).OlePropertySet("HorizontalAlignment",xlCenter);//设置单元格中的文字距中
//sh.OlePropertyGet("Cells",1,1).OlePropertySet("VerticalAlignment",xlCenter);//作用不明
//设置单元格中的文字距右:
//sh.OlePropertyGet("Cells",1,1).OlePropertySet("HorizontalAlignment",xlRight);//设置单元格中的文字距右
//sh.OlePropertyGet("Cells",1,1).OlePropertySet("VerticalAlignment",xlCenter);//作用不明
/*-------------------------------------------------
//目前真正最全的OLE操作Excel的完整代码
//版本:2007.01.15.01
//C++Builder专家组www.3322ee.com原创文章
//转载请保留本版权信息,谢谢合作
--------------------------------------------------/
//工作表操作:
//sh.OlePropertySet("Name", "Sheet的新名字");//重命名当前工作表
//sh.OleFunction("Delete");//删除当前工作表

//画单元格单条边框:
//sh.OlePropertyGet("Cells",11,2).OlePropertyGet("Borders").OlePropertyGet("Item",xlEdgeLeft).OlePropertySet("Weight",xlThin);//画单元格左边框
//sh.OlePropertyGet("Cells",11,2).OlePropertyGet("Borders").OlePropertyGet("Item",xlEdgeLeft).OlePropertySet("LineStyle",xlContinuous);//画单元格左边框
//sh.OlePropertyGet("Cells",11,2).OlePropertyGet("Borders").OlePropertyGet("Item",xlEdgeRight).OlePropertySet("Weight",xlThin);//画单元格右边框
//sh.OlePropertyGet("Cells",11,2).OlePropertyGet("Borders").OlePropertyGet("Item",xlEdgeRight).OlePropertySet("LineStyle",xlContinuous);//画单元格右边框
//sh.OlePropertyGet("Cells",11,2).OlePropertyGet("Borders").OlePropertyGet("Item",xlEdgeTop).OlePropertySet("Weight",xlThin);//画单元格上边框
//sh.OlePropertyGet("Cells",11,2).OlePropertyGet("Borders").OlePropertyGet("Item",xlEdgeTop).OlePropertySet("LineStyle",xlContinuous);//画单元格上边框
//sh.OlePropertyGet("Cells",11,2).OlePropertyGet("Borders").OlePropertyGet("Item",xlEdgeBottom).OlePropertySet("Weight",xlThin);//画单元格下边框
//sh.OlePropertyGet("Cells",11,2).OlePropertyGet("Borders").OlePropertyGet("Item",xlEdgeBottom).OlePropertySet("LineStyle",xlContinuous);//画单元格下边框
//sh.OlePropertyGet("Range","A1:C10").OlePropertyGet("Borders").OlePropertyGet("Item",xlDiagonalDown).OlePropertySet("Weight",4);//将指定区域的单元格用粗实线划掉

//画单元格完整边框:
//sh.OlePropertyGet("Range","A1:C10").OlePropertyGet("Borders").OlePropertySet("linestyle",2);//给指定区域的所有单元格加上边框,最后的数字:1=细实线框,2=短距虚线框,3=长距虚线框,4=点划线,5=双点划线,6=粗点划线
//sh.OlePropertyGet("Range","A1:C10").OlePropertyGet("Borders").OlePropertySet("weight",1);//给指定区域的所有单元格加上边框,最后的数字不大于4
//sh.OlePropertyGet("Range","A1:C10").OlePropertyGet("Borders").OlePropertySet("colorindex",3);//给指定区域的所有单元格加上边框,最后的数字表示颜色
//sh.OlePropertyGet("Cells",11,2).OlePropertyGet("Borders").OlePropertySet("LineStyle",xlContinuous);//给指定单元格四周加上实线边框

//去掉指定区域的边框:
//sh.OlePropertyGet("Range","A1:C10").OlePropertyGet("Borders").OlePropertySet("linestyle",xlNone);//去掉指定区域的边框

//画单元格单面边框参数说明:
//xlDiagonalDown:绘制从左上到右下角的斜线
//xlDiagonalUp:绘制从左下到右上角的斜线
//xlEdgeBottom:画单元格下边框
//xlEdgeLeft:画单元格左边框
//xlEdgeRight:画单元格右边框
//xlEdgeTop:画单元格上边框
/*-------------------------------------------------
//目前真正最全的OLE操作Excel的完整代码
//版本:2007.01.15.01
//C++Builder专家组www.3322ee.com原创文章
//转载请保留本版权信息,谢谢合作
--------------------------------------------------/
    try{
      //sh.OleFunction("SaveAs", "c:\\123.xls");//保存工作簿,当文件已经存在时会有提示,如果不覆盖会出错,所以正式使用时要预先处理或拦截错误
      //sh.OleFunction("SaveAs", WideString("c:\\"+ADODataSet1->FieldByName("creater")->AsString.Trim()+".xls"));//保存工作簿,当文件已经存在时会有提示,如果不覆盖会出错,所以正式使用时要预先处理或拦截错误
      //newxls.OleFunction("Close");//关闭表格
      //ex.OleFunction("Quit");//退出Excel,释放OLE对象
    }catch(...){
      return;
    }

//结束,如果没有如下代码,EXCEL线程直到应用程序退出才结束:
Variant Axl,Workbook,AxSheet,nms,bef,aft;
Axl=Unassigned;
Workbook=Unassigned;
AxSheet=Unassigned;
bef=Unassigned;
aft=Unassigned;
nms=Unassigned;
//ShowMessage("Well Done boy!");//弹出对话框
}

//图表部分,备用:------------------------------
/*-------------------------------------------------
//目前真正最全的OLE操作Excel的完整代码
//版本:2007.01.15.01
//C++Builder专家组www.3322ee.com原创文章
//转载请保留本版权信息,谢谢合作
--------------------------------------------------/
1)
//开一个新的图表工作簿
Variant Chart;
Chart = ex.Exec(PropertyGet("Charts")).Exec(Function("Add"));
ex.Exec(PropertySet("Visible") << true);
Chart.Exec(PropertySet("Type") << -4100);

2)
    //滚动图表
    for(int nRotate=5; nRotate <= 180; nRotate += 5){
      Chart.Exec(PropertySet("Rotation") << nRotate);
    }
    for(int nRotate = 175; nRotate >= 0; nRotate -= 5){
      Chart.Exec(PropertySet("Rotation") << nRotate);
    }
/*-------------------------------------------------
//目前真正最全的OLE操作Excel的完整代码
//版本:2007.01.15.01
//C++Builder专家组www.3322ee.com原创文章
//转载请保留本版权信息,谢谢合作
--------------------------------------------------/
3)
      //使用ExcelApp的Exec方法新建一新工作薄
      newxls=(ex.OlePropertyGet("Workbooks")).OleFunction("Add");//默认工作簿
      //newxls=(ex.OlePropertyGet("Workbooks")).OleFunction("Add",1);//单工作表
      //newxls=(ex.OlePropertyGet("Workbooks")).OleFunction("Add",2);//图表
      //newxls=(ex.OlePropertyGet("Workbooks")).OleFunction("Add",3);//宏表
      //newxls=(ex.OlePropertyGet("Workbooks")).OleFunction("Add",4);//国际通用宏表
      //newxls=(ex.OlePropertyGet("Workbooks")).OleFunction("Add",5);//与默认的相同
      //newxls=(ex.OlePropertyGet("Workbooks")).OleFunction("Add",6);//工作簿且只有一个表
◎ 在工作表最前面插入一行
Sheet1.PG("Rows", 1).PR("Insert");

◎ 删除一行
ExcelApp.PG("Rows", 2).PR("Delete"); //将第2行删除
★窗口属性★

◎ 显示属性
ExcelApp.PS("Windowstate", 3); //最大化显示
1---------xlNormal //正常显示
2---------xlMinimized //最小化显示
3---------xlMaximized //最大化显示

◎ 状态栏属性
ExcelApp.PS("StatusBar", "您好,请您稍等。正在查询!");
ExcelApp.PS("StatusBar", false); //还原成默认值

◎ 标题属性:
ExcelApp.PS("Caption", "查询系统");

3、操作图表

★添加图表

Variant Chart; 
Chart = ExcelApp.Exec(PropertyGet("Charts")).Exec(Function("Add"));
ExcelApp.Exec(PropertySet("Visible") << true);
Chart.Exec(PropertySet("Type") << -4100);

★滚动图表

for(int nRotate=5; nRotate <= 180; nRotate += 5)
{
Chart.Exec(PropertySet("Rotation") << nRotate);
}
for (int nRotate = 175; nRotate >= 0; nRotate -= 5)
{
Chart.Exec(PropertySet("Rotation") << nRotate);
}

#include "comobj.hpp"
//---------------------------------------------------------------------------
// 对指定Excel文件中的指定列进行排序
// strExcelFileName : excel文件名
// nCol : 指定的列号
// nSortStyle : 1:升序,2:降序
void SortExcelColumn(String strExcelFileName, int nCol, int nSortStyle)
{
Variant vExcelApp, vWorkbook, vRange;
vExcelApp = Variant::CreateObject("Excel.Application");
vExcelApp.OlePropertySet("Visible", false);
vExcelApp.OlePropertyGet("WorkBooks").OleProcedure("Open", strExcelFileName.c_str());
vWorkbook = vExcelApp.OlePropertyGet("ActiveWorkbook");
vExcelApp.OlePropertyGet("Columns", nCol).OleProcedure("Select");
vExcelApp.OlePropertyGet("ActiveSheet").OlePropertyGet("Cells", 1, nCol).OleProcedure("Select");
vRange = vExcelApp.OlePropertyGet("Selection");
vRange.Exec(Function("Sort")<<vExcelApp.OlePropertyGet("Selection")<<nSortStyle);
vWorkbook.OleProcedure("Save");
vWorkbook.OleProcedure("Close");
vExcelApp.OleFunction("Quit");
vWorkbook = Unassigned;
vExcelApp = Unassigned;
ShowMessage("ok");
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
// 对C:\123\123.xls文件中第一个Sheet的第四列进行升序排序
SortExcelColumn("C:\\123\\123.xls", 4, 1);
}
//

EXCEL之Range,cells,offset,end用法 
一.Range属性

1.选择单个单元格(例如A5)

              Range("A5").Select

2.选择一个单元格区域(例如A6:A10)

              Range("A6:A10").Select

3.选择一些不相邻的单元格(例如A1,B6,C8)

              Range("A1,B6,C8").Select

4.选择一些不相邻的单元格和单元格区域(例如A11:D11,B7,C9)

              Range("A11:D11,B7,C9").Select

二.Cells属性

1.选择单个单元格(例如A5)

              Cells(5,1).Select                   Cells(5,A).Select

2.选择一个单元格区域(例如A6:A10)

              Range(Cells(6,1),Cells(10,1)).Select

3.选择工作表中的所有单元格

              Cells.Select

三.Offset属性

1.选择单元格A1下面一行和右边三列的单元格

              Range("A1").Offset(1,3).Select

2.选择单元格D15上面两行和左边一列的单元格

              Range("D15").Offset(-2,-1).Select

3.选择同列单元格(上一行)

              ActiveCell.Offset(-1,0).Select

4.重新选取区域

              ActiveCell.Offset(2,2).Resize(2,4).Select

四.END属性(移动到连续有内容的单元格)

1.选择任何行的最后一个单元格

              ActiveCell.End(xlToRight).Select

2.选择任何行的最前一个单元格

              ActiveCell.End(xlToLeft).Select

3.选择任何列的最后一个单元格

              ActiveCell.End(xlDown).Select

1.选择任何列的最前一个单元格

              ActiveCell.End(xlUp).Select

//----------------------------------------------------------------------------------------------------------
摘要:利用C++ Builder 5.0可视化的设计工具,强大方便的数据库开发功能和Excel强大的电子表格处理功能,为处理多重报表和复杂报表提供一个良好的方法。 
关键词:OLE 自动化客户程序 电子表格 
Abstract: The paper provides a good method to make use of powerful database exploitation function of C++ Builder and powerful electron table management function of Excel 2000. 
Key words: OLE,automation client programs,electron table 
一 引言 
C++ builder 5.0是Inprise公司推出的基于C++语言的可视化开发语言,不仅具有新颖的可视化设计工具,还配有数据库引擎,可以通过SQL连接或ODBC访问多种数据库,并具有开发基于Client/Server模式的数据库应用程序的能力。当我们进行数据库软件开发的时候,一般要进行大量报表设计,尤其当涉及到设计多重报表或交叉报表时,我们会觉得力不从心。利用EXCEL强大的电子表格处理功能,可以解决C++ Builder 自带报表工具功能少的缺陷。 
C++ Builder调用Excel常用的方法有两种:一种是利用OLE技术调用Excel。OLE(对象的连接和嵌入)是微软公司提出的标准,它提供了一种用源于不同应用程序的信息创建复合文档的强有力方法。第二种是利用ODBC数据库技术调用Excel。第一种方法比较简单,方便,本文讨论的是第一种方法。 
二 系统功能要求 
随着电子化办公的推广和保险业务的全面开展,账本式的管理方法已不能对数据庞大的电子化设备进行合理有效的管理,这就要求电子化的管理方式,通过计算机和公司局域网实现对设备进行管理。我们开发的电子化管理软件由电子化设备管理数据库、电子化设备耗材管理数据库组成,信息技术管理人员可以对本数据库进行添加、删除、修改、查询等操作,并且可以打印各种查询表格,表格按照EXCEL格式进行打印,并且保存归档。 
C++Builder软件自带创建报表工具,如QReport控件。但是相对于EXCEL强大的电子表格处理功能和方便的操作而言,QReport控件功能就显得很苍白。在该管理软件中,我们将查询结果自动生成EXCEL表格,然后在EXCEL环境下进行打印或者保存文件。具体函数实现在下面的段落中进行详细说明,并且都给出示例。 
三 函数的实现 
要在应用程序中控制Excel2000的运行,首先必须在编制自动化客户程序时使其头文件要包含Comobj.hpp和Utilcls.h。 
即:#include 
#include 
C++ Builder开发者把Excel自动化对象的功能包装在下面的四个Ole Object Class函数中,应用人员可以很方便地进行调用。 
设置对象属性:Variant OlePropertySet(属性名,参数……); 
获得对象属性:void OlePropertyGet(属性名,参数……); 
调用对象方法:1) Variant OleFunction(函数名,参数……); 
2) void OleProcedure(过程名,参数……); 
C++ Builder中使用OLE控制Excel2000,必须掌握Excel2000的自动化对象及Microsoft Word Visual Basic帮助文件中的关于Excel的对象、方法和属性。对象是一个Excel元素,属性是对象的一个特性或操作的一个方面,方法是对象可以进行的动作。 
1、Excel中常用的对象是:Application,Workbooks,Worksheets等。 
(1) 创建应用对象:如: 
Variant ex; 
ex=Variant::CreateObject ("Excel.Application"); 
或者 ex=CreateOleObject ("Excel.Application"); 
(2) 创建工作簿对象: 
Variant wb; 
wb=ex.OlePropertyGet("ActiveWorkBook"); 
(3) 创建工作表对象: 
Variant sheet; 
sheet=wb.OlePropertyGet("ActiveSheet"); 
(4) 创建区域对象: 
Variant range; 
range=sheet.OlePropertyGet("Range","A1:A10"); 
2、常用的属性操作: 
(1)新建EXCEL文件: 
(a):新建系统模板的工作簿 
ex.OlePropertyGet("workbooks").OleFunction("Add") //默认工作簿 
ex.OlePropertyGet("workbooks").OleFunction("Add",1) //单工作表 
ex.OlePropertyGet("workbooks").OleFunction("Add",2) //图表 
ex.OlePropertyGet("workbooks").OleFunction("Add",3) //宏表 
ex.OlePropertyGet("workbooks").OleFunction("Add",4) //国际通用宏表 
ex.OlePropertyGet("workbooks").OleFunction("Add",5) //与默认的相同 
ex.OlePropertyGet("workbooks").OleFunction("Add",6) //工作簿且只有一个表 
(b):新建自己创建的模板的工作簿 
ex.OlePropertyGet("workbooks").OleFunction("Add","C:\\WINDOWS\\Profiles\\test2\\Application Data\\Microsoft\\Templates\\result.xlt"); // 后面写上模板的完全路径,注意"\\" 
(2)打开工作簿: 
ex.OlePropertyGet("workbooks").OleFunction("open","路径名.xls") 
(3)保存工作簿: 
wb.OleFunction("Save"); //表格保存 
wb..OleFunction("SaveAs","文件名"); //表格保存为,文件路径注意用"\\" 
(4)退出EXCEL: 
ex.OleFunction ("Quit"); 
(5)设置字体: 
(a):设置单元格字体 
sheet.OlePropertyGet("Cells",1,1).OlePropertyGet("Font").OlePropertySet("Name","隶书"); 
sheet.OlePropertyGet("Cells",2,3).OlePropertyGet("Font").OlePropertySet("size",28); 
(b):设置所选区域字体 
range.OlePropertyGet("Cells").OlePropertyGet("Font").OlePropertySet("size",28); range.OlePropertyGet("Cells").OlePropertyGet("Font").OlePropertySet("Color", 
RGB(0,0,255)); 
其中参数的设置: 
Font---Name : "隶书" //字体名称 
----Size : 12 //字体大小 
----Color : RGB(*,*,*) //颜色 
-----Underline : true/false //下划线 
-----Italic: true/false //斜体 
(6)单元格的合并: 
(a) range1=sheet.OlePropertyGet("Range", "A1:A2"); //A1和A2单元格合并 
(b) AnsiString Str="A"+IntToStr(j)+":"+"C"+IntToStr(j); 
range1=sheet.OlePropertyGet("Range",Str); //可以用变量控制单元格合并 
range1.OleFunction("Merge" , false); 
(7)赋值语句: 
(a):指定单元格赋值 
sheet.OlePropertyGet("Cells",3,6).OlePropertySet("Value",str); 
sheet.OlePropertyGet("Cells",j,1).OlePropertySet("Value","共查到记录:"+IntToStr(j-6)); 
(b):所选区域单元格赋值 
range.OlePropertyGet("Cells").OlePropertySet("Value",10); 
(c):所选区域行赋值 
range.OlePropertyGet("Rows",1).OlePropertySet("Value",1234); 
(d):工作表列赋值 
sheet.OlePropertyGet("Columns",1).OlePropertySet("Value",1234); 
(8)取值语句: 
AnsiString abc=sheet.OlePropertyGet("Cells",120,1).OlePropertyGet("Value"); 
(9)区域选择: 
range.OlePropertyGet("Cells").OleFunction("Select"); 
(10)窗口属性: 
(a)显示属性 
ex.OlePropertySet("Windowstate",3); //最大化显示 
参数 1---------xlNormal //正常显示 
2---------xlMinimized //最小化显示 
3---------xlMaximized //最大化显示 
(b)状态栏属性 
ex.OlePropertySet ("StatusBar","您好,请您稍等。正在查询!"); 
ex.OlePropertySet ("StatusBar", false); //还原成默认值 
(c)标题属性: 
ex.OlePropertySet("Caption","查询系统"); 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值