起步软件技术论坛-X3

 找回密码
 立即注册
搜索
楼主: dupit

[结][急:DispatchHelper怎么使用啊?帮我看看这个如何写?[请求]**

[复制链接]
 楼主| 发表于 2008-2-20 16:33:54 | 显示全部楼层
怎么办啊?你们难道都没这个问题吗?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-2-20 17:28:38 | 显示全部楼层
有人回答吗?
回复 支持 反对

使用道具 举报

发表于 2008-2-20 17:44:59 | 显示全部楼层
不好意思,记错了
odocs := app.PropertyGet('WorkBooks',[])
docs := System.DispatchHelper.Create(odocs);
  odoc1 := docs.PropertyGet('Item',[1]);
  doc1 := System.DispatchHelper.Create(odoc1);

要释放的是 odocs 和 odoc1
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-2-20 17:52:05 | 显示全部楼层
怎么释放啊?这个又不是OBJECT对象。
回复 支持 反对

使用道具 举报

发表于 2008-2-20 18:07:26 | 显示全部楼层
(odocs as System.IDisposable).dispose;
(odoc1 as System.IDisposable).dispose;
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-2-21 09:11:39 | 显示全部楼层
这样写不行的,不能强行转化。

错误信息.jpg

11.84 KB, 下载次数: 229

回复 支持 反对

使用道具 举报

发表于 2008-2-21 09:49:03 | 显示全部楼层
把你的完整代码贴上来我看看
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-2-21 09:55:01 | 显示全部楼层
static function THSZYJ.DbtreelistToExcel(DataTreeList:TDataTreeList;
           dtlcTemp:TDataTreeListColumn;AFileName:string):integer;//输出treelist数据到excel文件,dtlcTemp为自动缩进的列
const
  ciLineWidth=2;
var
  ExcelApp,workbook:   Object;
  i,j,k:integer;
  app, docs, doc,worksheets, worksheet,cell,temp: System.DispatchHelper;
  tmpNode:TTreeListNode;
begin
  ExcelApp:=ComObj.CreateOleObject('Excel.Application');
  try
    app := System.DispatchHelper.Create(ExcelApp);
    app.PropertyPut('Visible', [false]);
    docs := System.DispatchHelper.Create(app.PropertyGet('WorkBooks',[]));
    docs.InvokeMethod('ADD', []);

    workbook := docs.PropertyGet('Count',[]);
    if ObjectHelper.ToInt(workbook)>0 then
    begin
      doc := System.DispatchHelper.Create(docs.PropertyGet('Item',[1]));
      worksheets := System.DispatchHelper.Create(doc.PropertyGet('Sheets',[]));
      worksheet := System.DispatchHelper.Create(worksheets.PropertyGet('Item',[1]));
      //增加列名
      j:=1;k:=0;
      for i:=0 to DataTreeList.ColumnCount-1 do
      begin
        if DataTreeList.Columns.Visible then
        begin
          inc(k);
          cell := System.DispatchHelper.Create(worksheet.PropertyGet('Cells',[j,k]));
          cell.PropertyPut('Cells', [DataTreeList.Columns.Caption]);
          temp := System.DispatchHelper.Create(cell.PropertyGet('Font',[]));
          temp.PropertyPut('Name', ['宋体']);
          temp.PropertyPut('Bold', [True]);
           temp := System.DispatchHelper.Create(cell.PropertyGet('Borders',[1]));
          temp.PropertyPut('Weight', [ciLineWidth]);
          temp := System.DispatchHelper.Create(cell.PropertyGet('Borders',[2]));
          temp.PropertyPut('Weight', [ciLineWidth]);
          temp := System.DispatchHelper.Create(cell.PropertyGet('Borders',[3]));
          temp.PropertyPut('Weight', [ciLineWidth]);
          temp := System.DispatchHelper.Create(cell.PropertyGet('Borders',[4]));
          temp.PropertyPut('Weight', [ciLineWidth]);
           temp := System.DispatchHelper.Create(worksheet.PropertyGet('Columns',[k]));
          temp.PropertyPut('ColumnWidth', [DataTreeList.Columns.Width div 8]);
        end;
      end;
    end;
    //增加数据
    while true do
    begin
      if not assigned(DataTreeList.TopNode) then
        break;
      if not assigned(tmpNode) then
        tmpNode:=DataTreeList.TopNode
      else
        tmpNode:=tmpNode.GetNext;
      if not assigned(tmpNode) then
        break;
      inc(j);k:=0;
      for i:=0 to DataTreeList.ColumnCount-1 do
      begin
        if DataTreeList.Columns.Visible then
        begin
          inc(k);
          cell := System.DispatchHelper.Create(worksheet.PropertyGet('Cells',[j,k]));
          if dtlcTemp=DataTreeList.Columns then
            cell.PropertyPut('Cells', [{lpad('',tmpNode.Level*2,' ')+}tmpNode.Strings])//lpad功能与oracle中的lpad一样。
          else
            cell.PropertyPut('Cells', [tmpNode.Strings]);
          temp := System.DispatchHelper.Create(cell.PropertyGet('Borders',[1]));
          temp.PropertyPut('Weight', [ciLineWidth]);
           temp := System.DispatchHelper.Create(cell.PropertyGet('Borders',[2]));
          temp.PropertyPut('Weight', [ciLineWidth]);
          temp := System.DispatchHelper.Create(cell.PropertyGet('Borders',[3]));
          temp.PropertyPut('Weight', [ciLineWidth]);
           temp := System.DispatchHelper.Create(cell.PropertyGet('Borders',[4]));
          temp.PropertyPut('Weight', [ciLineWidth]);
         end;
      end;   
    end;
    doc.InvokeMethod('SaveCopyAs',[AFileName]);
     app.PropertyPut('DisplayAlerts', [False]);
  finally
    docs.InvokeMethod('Close', []);
    app.InvokeMethod('Quit', []);
  {  (worksheet as System.IDisposable).dispose;
    (worksheets as System.IDisposable).dispose; }
  {  (doc as System.IDisposable).dispose;
    (docs as System.IDisposable).dispose;     }
    //(App as System.IDisposable).dispose;

    //(workbook as System.IDisposable).dispose;
    (ExcelApp as System.IDisposable).dispose;
  end;
end;


调用的代码
  if sdExcel.Execute then
  begin
    HSZYJ.THSZYJ.DbtreelistToExcel(DataTreeListSJ,DataTreeListSJWDMLMC,sdExcel.FileName);
  end;
回复 支持 反对

使用道具 举报

发表于 2008-2-21 10:02:39 | 显示全部楼层
看23楼和25楼
docs := System.DispatchHelper.Create(app.PropertyGet('WorkBooks',[]));
改成
odocs := app.PropertyGet('WorkBooks',[])
docs := System.DispatchHelper.Create(odocs);

释放代码是
(odocs as System.IDisposable).dispose;
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-2-21 10:24:39 | 显示全部楼层
同样的,还是没释放掉。你能不能弄个最简单的例子试一下。就一个datatreelist控件,随便显示数据,然后倒出为excel文件。现在问题的现象是文件已经生成了,都在程序还未关闭之前,那个excel文件无法打开,因为excel进程还在。只有程序关闭才能释放excel进程。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Justep Inc.

GMT+8, 2025-7-7 09:51 , Processed in 0.043604 second(s), 16 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表