OLE excel

最全的OLE操作Excel的完整代码(转载) - [技术文档]
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://wsqj.blogbus.com/logs/32271364.html

#include<Utilcls.h>
#include "Excel_2K_SRVR.h"
//#include "ComObj.hpp"
/*-------------------------------------------------
//谨慎的思考310032649原创文章
//目前真正最全的OLE操作Excel的完整代码
//版本:2007.01.15.01
//C++Builder专家组www.3322ee.com原创文章
//转载请保留本版权信息,谢谢合作
--------------------------------------------------/
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专家组www.3322ee.com原创文章
//转载请保留本版权信息,谢谢合作
--------------------------------------------------/
//新建工作表并重命名:
    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);//插入图片

/*-------------------------------------------------------------------------------------------------------------
参阅应用于示例特性
从原有文件中创建图片。返回一个 Shape 对象,该对象代表新图片。

expression.AddPicture(FileName, LinkToFile, SaveWithDocument, Left, Top, Width, Height)

expression      必需。该表达式返回“应用于”列表中的对象之一。

FileName      String 类型,必需。要创建的 OLE 对象的源文件。

LinkToFile      MsoTriState 类型,必需。要链接至的文件。

MsoTriState 可为以下 MsoTriState 常量之一。 
msoCTrue 
msoFalse 使图片成为其源文件的独立副本。 
msoTriStateMixed 
msoTriStateToggle 
msoTrue 建立图片与其源文件之间的链接。

SaveWithDocument      MsoTriState 类型,必需。将图片与文档一起保存。

MsoTriState 可为以下 MsoTriState 常量之一。 
msoCTrue 
msoFalse 在文档中只保存链接信息。 
msoTriStateMixed 
msoTriStateToggle 
msoTrue 将链接图片与该图片插入的文档一起保存。如果 LinkToFile 为 msoFalse,则该参数必须为 msoTrue。

Left      Single 类型,必需。相对于文档的左上角,以磅为单位给出图片左上角的位置。

Top      Single 类型,必需。相对于文档的顶部,以磅为单位给出图片左上角的位置。

Width      Single 类型,必需。以磅为单位给出图片的宽度。

Height      Single 类型,必需。以磅为单位给出图片的高度。

示例
本示例向 myDocument 添加由文件“Music.bmp”创建的图片。插入的图片链接到其源文件,并与 myDocument 一起保存。

Set myDocument = Worksheets(1)
myDocument.Shapes.AddPicture _
    "c:\microsoft office\clipart\music.bmp", _
    True, True, 100, 100, 70, 70

c++ builder 示例:Sh1.OlePropertyGet("Shapes").OleFunction("AddPicture",filename.c_str(),false,true,Sh1.OlePropertyGet("Range",("D"+IntToStr(row)).c_str()).OlePropertyGet("Left")+1,Sh1.OlePropertyGet("Range",("D"+IntToStr(row)).c_str()).OlePropertyGet("Top")+2,50,25);
------------------------------------------------------------------------------------------------------------*/

//使指定单元格里面的数字以文本形式存储:
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专家组www.3322ee.com原创文章
//转载请保留本版权信息,谢谢合作
--------------------------------------------------/
//设置字体颜色:
//sh.OlePropertyGet("Cells").OlePropertyGet("Font").OlePropertySet("Color",RGB(0,0,255));//设置所有单元格的字体颜色
//sh.OlePropertyGet("Cells",1,1).OlePropertyGet("Font").OlePropertySet("ColorIndex",3);//设置指定单元格的字体颜色,这里为红色
//sh.OlePropertyGet("Cells").OlePropertyGet("Font").OlePropertySet("ColorIndex",3);//设置所有单元格的字体颜色,这里为红色

//sh.OlePropertyGet("Columns",3).OlePropertyGet("Characters",1,4).OlePropertyGet("Font").OlePropertySet("ColorInder",3);
//设置单元格中部分字符的颜色
--注:zhuActiveCell.Characters(Start:=1, Length:=4).Font.ColorIndex = 3 有问题还无法使用.

//设置列宽度:
//sh.OlePropertyGet("Columns",3).OlePropertySet("ColumnWidth",28);//设置第3列的列宽为28
//sh.OlePropertyGet("Columns").OlePropertySet("ColumnWidth",28);//设置所有列的列宽为28


//设置行高度:
//sh.OlePropertyGet("Rows").OlePropertySet("RowHeight",28);//设置所有行的高度为28
//sh.OlePropertyGet("Rows",3).OlePropertySet("RowHeight",28);//设置指定行的高度为28

//设置字体:
//sh.OlePropertyGet("Cells", 1, 1).OlePropertyGet("Font").OlePropertySet("Name", "隶书");//设置第一行第一列单元格的字体
//sh.OlePropertyGet("Cells").OlePropertyGet("Font").OlePropertySet("Name", "隶书");//设置所有单元格的字体

//字体大小:
//sh.OlePropertyGet("Cells", 1, 1).OlePropertyGet("Font").OlePropertySet("Size",10);//设置第一行第一列单元格的字体大小为10号
//sh.OlePropertyGet("Cells").OlePropertyGet("Font").OlePropertySet("Size",10);//设置所有单元格的字体大小为10号

//使用粗体:
//sh.OlePropertyGet("Cells",1,1).OlePropertyGet("Font").OlePropertySet("Bold",true);//设置第一行第一列单元格的字体为粗体字
//sh.OlePropertyGet("Cells").OlePropertyGet("Font").OlePropertySet("Bold",true);//设置所有单元格的字体为粗体字

//插入和删除行:
//sh.OlePropertyGet("Rows", 1).OleProcedure("Insert");//在指定位置插入行
//sh.OlePropertyGet("Rows", 1).OleProcedure("Delete");//将指定的行删除

//设置单元格的数字显示格式:
//sh.OlePropertyGet("Cells", 1, 1).OlePropertySet("NumberFormatLocal", "0.00%");//设置指定单元格格式为小数百分比
//sh.OlePropertyGet("Cells").OlePropertySet("NumberFormatLocal", "0.00%");//设置所有单元格格式为小数百分比
//sh.OlePropertyGet("Rows",1).OlePropertySet("NumberFormatLocal", "0.00%");//设置指定行单元格格式为小数百分比,当不加“,1”时为所有行
//sh.OlePropertyGet("Columns",1).OlePropertySet("NumberFormatLocal", "0.00%");//设置指定列单元格格式为小数百分比,当不加“,1”时为所有列
//sh.OlePropertyGet("Cells",1,1).OlePropertySet("NumberFormatLocal","#,##0.00");//指定单元格如果是数字就保留两位小数

//文本自动换行:
//sh.OlePropertyGet("Cells", 1, 1).OlePropertySet("WrapText", true);//设置指定单元格的文本自动换行
//sh.OlePropertyGet("Cells").OlePropertySet("WrapText", true);//设置所有单元格的文本自动换行
//sh.OlePropertyGet("Columns",1).OlePropertySet("WrapText", true);//设置指定列的单元格的文本自动换行, 当不加“,1”时为所有列
//sh.OlePropertyGet("Rows",1).OlePropertySet("WrapText", true);//设置指定行的单元格的文本自动换行, 当不加“,1”时为所有行
/*-------------------------------------------------
//目前真正最全的OLE操作Excel的完整代码
//版本:2007.01.15.01
//C++Builder专家组www.3322ee.com原创文章
//转载请保留本版权信息,谢谢合作
--------------------------------------------------/
//指定单元格的合并:
//String strRange ="A2:A3:C3";//指定合并的单元格的矩形范围
//sh.OlePropertyGet("Range", strRange.c_str()).OleFunction("Merge", false);//将指定的单元格合并
//sh.OlePropertyGet("Range", "A2:A3:C3").OleFunction("Merge", false);//将指定的单元格合并

//给单元格赋值:
//sh.OlePropertySet("Cells","第 &P 頁,共 &N 頁");//给所有单元格赋值
sh.OlePropertyGet("Cells",1,1).OlePropertySet( "Value",str1.c_str());//给指定单元格赋值
//sh.OlePropertyGet("Rows",1).OlePropertySet("Value",1234);//给整个行的单元格赋值
//sh.OlePropertyGet("Columns",1).OlePropertySet("Value",1234);//给指定的整个列赋值

//从单元格中取值:
//AnsiString abc=sh.OlePropertyGet("Cells",1,1).OlePropertyGet("Value");//取指定单元格的值

//选择单元格:
//sh.OlePropertyGet("Cells").OleFunction("Select");//选择所有单元格
//sh.OlePropertyGet("Cells",3,3).OleFunction("Select");//选择指定单元格(指定的单元格获得焦点)
/*-------------------------------------------------
//目前真正最全的OLE操作Excel的完整代码
//版本:2007.01.15.01
//C++Builder专家组www.3322ee.com原创文章
//转载请保留本版权信息,谢谢合作
--------------------------------------------------/
//区域操作:
//sh.OlePropertyGet("Range","A1:A10");//创建区域对象
//sh.OlePropertyGet("Range","A1:C10").OlePropertyGet("Cells").OlePropertySet("Value",10);//给指定区域的单元格赋值
//sh.OlePropertyGet("Range","A1:A10").OlePropertyGet("Cells").OleFunction("Select");//选中指定区域的单元格
//sh.OlePropertyGet("Range","A1:A10").OlePropertyGet("Font").OlePropertySet("Name", "隶书");//设置指定区域中所有单元格的字体
//sh.OlePropertyGet("Range","A1:C10").OlePropertyGet("Font").OlePropertySet("Underline",true);//设置指定区域的所有单元格中使用下划线
//sh.OlePropertyGet("Range","A1:C10").OlePropertyGet("Font").OlePropertySet("Italic",true);//设置指定区域中所有单元格中使用斜体
//sh.OlePropertyGet("Range","A1:C10").OlePropertyGet("Font").OlePropertySet("Color",RGB(0,0,255));//设置所有单元格的字体颜色
//sh.OlePropertyGet("Range","A1:C10").OlePropertyGet("Font").OlePropertySet("Size",10);//设置指定区域中所有单元格字体的大小为10号
//sh.OlePropertyGet("Range","A1:C10").OlePropertyGet("Font").OlePropertySet("Bold",true);//设置指定区域中所有单元格的字体为粗体字
//sh.OlePropertyGet("Range","A1:C10").OleProcedure("Insert");//在指定区域前面增加指定列的单元格,指定区域外不受影响
//sh.OlePropertyGet("Range","A1:C10").OleProcedure("Delete");//将指定的行删除
//sh.OlePropertyGet("Range","A1:C10").OlePropertySet("NumberFormatLocal", "0.00%");//设置指定区域中的所有单元格格式为小数百分比
//sh.OlePropertyGet("Range","A1:C10").OlePropertySet("WrapText", true);//设置指定区域中所有的单元格中的文本自动换行
//sh.OlePropertyGet("Range","A1:C10").OlePropertySet("ColumnWidth",28);//设置指定区域所包含的所有列的列宽为28
//sh.OlePropertyGet("Range","A1:C10").OlePropertySet("RowHeight",28);//设置指定区域所包含的所有行的高度为28
//sh.OlePropertyGet("Range","A1:C20").OlePropertySet("DirectDependents","sdasd");//将选定区域中所有含公式的单元格填充成指定值,"sdasd"可以替换成不加引号的TRUE等

//单元格中的数据计算:
//sh.OlePropertyGet("Cells",5,6).OlePropertySet("FormulaR1C1","=SUM(R[-3]C:R[-1]C)");//竖排加法运算
//sh.OlePropertyGet("Cells",2,9).OlePropertySet("FormulaR1C1","=SUM(RC[-3]:RC[-1])");//横向加法运算
//sh.OlePropertyGet("Cells",5,8).OlePropertySet("FormulaR1C1","=SUM(R[-3]C[-2]:R[-1]C[-1])");//横竖向加法运算

//页眉页脚设置:打印预览才能看见
sh.OlePropertyGet("PageSetup").OlePropertySet("LeftHeader","sdfgs");//左上角页眉设置
sh.OlePropertyGet("PageSetup").OlePropertySet("CenterHeader","sadfs");//中间页眉设置
sh.OlePropertyGet("PageSetup").OlePropertySet("RightHeader","345s");//右上角页眉设置
sh.OlePropertyGet("PageSetup").OlePropertySet("LeftFooter","234fasdf");//左下角页脚设置
sh.OlePropertyGet("PageSetup").OlePropertySet("CenterFooter","第 &P 页,共 &N 页");//中间页脚设置(这里显示第几页,共几页)
sh.OlePropertyGet("PageSetup").OlePropertySet("RightFooter","sd23");//右下脚页角设置

//边界设定:单位为:point , 一 point = 1 / 72 inches 
//sh.OlePropertyGet("PageSetup").OlePropertySet("LeftMargin",15);
//sh.OlePropertyGet("PageSetup").OlePropertySet("RightMargin",15);
//sh.OlePropertyGet("PageSetup").OlePropertySet("TopMargin",15);
//sh.OlePropertyGet("PageSetup").OlePropertySet("BottomMargin",15);
//sh.OlePropertyGet("PageSetup").OlePropertySet("HeaderMargin",15);
//sh.OlePropertyGet("PageSetup").OlePropertySet("FooterMargin",15);

/*-------------------------------------------------
//目前真正最全的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","查询系统"); 
四 结论和展望 
我们已将上面的技术应用于我们为某保险公司做的电子化设备管理软件中,运行良好,使用方便,维护性好,人机界面友好,功能强大,既利用C++ Builder 强大的开发数据库软件的功能,又利用了Excel强大的电子表格处理功能和表格打印功能,使我们的软件具有专业水平。上面的程序在C++Builder5.0和Excel2000下调试通过。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值