中国程序员联盟 正在重新改版中ing 不便之处还请见谅 改版后将内容涉及java delphi .net php
 
  首页 | 数据库开发 | 网络通讯 | 多线程 | 多媒体开发 | 图像处理 | 程序人生 | 系统函数 | 控件开发 | Web服务
 
  当前位置:笨鱼delphi技术网>数据库开发>代码分析>文章内容

delphi 如何在DELPHI里调用MS office

来源:国外 关于:William Gerbert 发布时间:2007-07-18   [收藏] [推荐]
How can you remote control MS Office applications from your Delphi application? The Answer is to use a TOLEContainer.
It requires some interface knowledge to use the right object(s) and their properties. Some samples are added to Delphi demos, but all of them are targeted at MSWord. I have posted examples for Internet Explorer elsewhere and here is a sample for MSExcel:

// procedure is activated when OleOject activates user interface
// procedure copies TStringGrid content to an (OleObject) Excel sheet
procedure TForm1.OleContainer1Activate(Sender: TObject);
var
  ExcelSheet: Variant;
  Count,
    Curent: Variant;
  i,
    j: Integer;
begin
  // first we read how many sheets are open in a specified Excel document
  Count := OleContainer1.OleObject.Application.Sheets.Count;
  // then we read the number of a sheet to witch user wants to add StringGrid content
  Curent := StrToInt(OKBottomDlg.Edit2.Text);
  if Curent <> 0 then
  begin
    if Curent <= Count then
      // if the sheet with index Curent exist then copy content
    begin
      // first we activate the desiered sheet object
      OleContainer1.OleObject.Application.Sheets[Count].Activate;
      // pass the object to a variant variable
      ExcelSheet := OleContainer1.OleObject.Application.ActiveSheet;
      // now we can do what ever we like with it
      ExcelSheet.name := OKBottomDlg.Edit3.Text + IntToStr(Count);
      for i := 0 to StringGrid1.RowCount do
      begin
        for j := 0 to StringGrid1.ColCount do
        begin
          ExcelSheet.Cells(i, j) := StringGrid1.Cells[j, i]
        end
      end;
      // here we copy the content
    end
    else // else if the sheet we are trying to access doesn't exsist
    begin
      // we add new sheets untill the requested
      // user's index is reached ( curent variable )
      for i := Count + 1 to Curent do
      begin
        OleContainer1.OleObject.Application.Sheets.Add
      end;
      // again we do as above
      OleContainer1.OleObject.Application.Sheets[Curent].Activate;
      ExcelSheet := OleContainer1.OleObject.Application.ActiveSheet;
      ExcelSheet.name := OKBottomDlg.Edit3.Text + IntToStr(Count);
      for i := 0 to StringGrid1.RowCount do
      begin
        for j := 0 to StringGrid1.ColCount do
        begin
          ExcelSheet.Cells(i, j) := StringGrid1.Cells[j, i]
        end
      end;
    end
  end;
end;

[浏览: 次]   
上一篇:delphi COM+对象缓冲池   下一篇:delphi 如何得到MS office 语系
[收藏] [推荐] [返回顶部] [打印本页] [关闭窗口]  
    评论加载中…
google adsense热点文章
·delphi Delphi_动态数组介绍----Delphi
·delphi 检查IP地址
·delphi DBExpress 提高
·delphi 复制Excel列到二维数组
·delphi Delphi_VCL消息处理机制的内幕
·delphi 产生不重复的随机数
·delphi 缓冲池
·delphi 在WORD文档里添加页签
·delphi MIDAS——多层分布式应用程序服
·delphi Asta多层应用实现
·delphi Datamodule的应用方式
·delphi DBExpress 入门
     delphi技术网 | firefox 下载 | Avant Browser下载 | dedecms 技术网 | drupal 爱好者 | php 技术网
  Copyright@www.delphichm.com,2006-2009.All Rights Reserved.
 
程序员联盟 | delphi Java .net|QQ:707102932