目前真正最全的OLE操作Excel的完整代码

#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);//插入图片

  //使指定单元格里面的数字以文本形式存储:
  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).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等

后面还有2倍的代码,由于字符数量限制,剩下的自己到网站上去看吧……