OleContainer.CreateObject('Excel.Application',False);为什么提示不支持次接口!??
哪个高人知道??或者给个完整的在Olecontainer里用代码新建一个Excel的过程

解决方案 »

  1.   


    //看看这个
    unit fOLEContainerMain;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, Grids, DBGrids, ExtCtrls, DBCtrls, DB, StdCtrls, Buttons,
      OleCtnrs, DBClient;type
      TForm1 = class(TForm)
        dsBLOBDATA: TDataSource;
        DBNavigator1: TDBNavigator;
        DBGrid1: TDBGrid;
        OleContainer1: TOleContainer;
        bbtnLoad: TBitBtn;
        bbtnShow: TBitBtn;
        OpenDialog1: TOpenDialog;
        BitBtn1: TBitBtn;
        procedure bbtnLoadClick(Sender: TObject);
        procedure bbtnShowClick(Sender: TObject);
        procedure BitBtn1Click(Sender: TObject);
      private
        { Private declarations }
        function GetFileExtension(const sFileName : String) : String;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementationuses udmOLEContainer;{$R *.dfm}procedure TForm1.bbtnLoadClick(Sender: TObject);
    var
      fnOle : String;  function BlobContentToString(const sFileName : String) : String;
      begin
        with TFileStream.Create(sFileName, fmOpenRead) do
        begin
          try
            SetLength(Result, Size);
            Read(Pointer(Result)^, Size);
          finally
            Free;
          end;
        end;
      end;begin
      if (OpenDialog1.Execute) then
      begin
        fnOle := OpenDialog1.FileName;
        dmOLEContainer.scdsBLOBData.Edit;
        dmOLEContainer.scdsBLOBData.FieldByName('BLOBDATA').AsString :=
          BlobContentToString(fnOle);
        dmOLEContainer.scdsBLOBData.FieldByName('EXT').Value :=
          GetFileExtension(fnOle);    dmOLEContainer.scdsBLOBData.Post;
      end;
    end;procedure TForm1.bbtnShowClick(Sender: TObject);
    var
      sFileName : String;
      bs : TClientBlobStream;
    begin
      bs := TClientBlobStream.Create(dmOLEContainer.scdsBLOBDataBLOBDATA, bmRead);
      try
        sFileName := ExtractFilePath(Application.ExeName) + 'tempBlob';
        sFileName := sFileName + '.' + dmOLEContainer.scdsBLOBData.FieldByName('EXT').AsString;
        bs.Seek(soFromBeginning, 0);
        bs.SaveToFile(sFileName);
        OleContainer1.CreateObjectFromFile(sFileName, False);
      finally
        bs.Free;
      end;
    end;function TForm1.GetFileExtension(const sFileName: String): String;
    begin
      Result := Copy(sFileName, Length(sFileName) - 2, 3);
    end;procedure TForm1.BitBtn1Click(Sender: TObject);
    begin
      dmOLEContainer.scdsBLOBData.ApplyUpdates(0);
    end;end.
      

  2.   

    ……
    uses comobj;
    ……
    procedure TForm1.Button1Click(Sender: TObject);
    var
      excelx,excely,sqlStr:string;
      StrValue:array [1..24] of string;
      tmpStr:integer;
      ExcelApp,WorkBook:Variant;
      i,j,ExcelRowCount:integer;
    begin 
      try
        ExcelApp := CreateOleObject('Excel.Application');//创建一个OleObject
        WorkBook := ExcelApp.WorkBooks.Open(ExcelPath);//使用opendialog对话框指定
        //excel档路径
        ExcelApp.Visible := false;
        ExcelRowCount := WorkBook.WorkSheets[1].UsedRange.Rows.Count;
        ProgressBar1.Max := ExcelRowCount - 1;
        for i := 2 to ExcelRowCount do
        //第一行为字段名信息。不取。
        begin
          ProgressBar1.Position := i ;
          label2.Caption := '总共 '+IntToStr(ProgressBar1.Max) + '条记录,正在导入第'+ IntToStr(i-1)+'条记录';
          application.ProcessMessages ;
          //excelx := excelapp.Cells[i,1].Value;
          //excely := excelapp.Cells[i,2].Value;
          for j:=1 to 24 do
            StrValue[j]:= excelapp.Cells[i,j].Value;
          
          ……
          ……
          ……
        end;
        label2.Caption := '总共 '+IntToStr(ProgressBar1.Max) + '条记录导入完成';
        ProgressBar1.Position :=ProgressBar1.Max;
      finally
      WorkBook.Close;
      ExcelApp.Quit;
      ExcelApp := Unassigned;
      WorkBook := Unassigned;
    end;
      

  3.   

    为什么提示不支持次接口!?? 我想可能是你的进程里面已经开了EXECL这个进程啦,你到任务管理器->进程,在里面把EXECL干掉,经常会有,因为在你打开EXECL,
    可能由于异常什么的,EXECL退出,但是进程里面没有结束,在你下次程序访问时,就有可能报不支持接口。你在写一个结束进程的函数,把进程结束掉。 uses 
       TLHelp32, ShellAPI ;
    procedure  CloseProcess(procName : string ) ;
    var
        lppe:tprocessentry32;
        sshandle:thandle;
        hh:hwnd;
        found:boolean;
    begin
          sshandle:=createtoolhelp32snapshot(TH32CS_SNAPALL,0);
          found:=process32first(sshandle,lppe);
          while   found   do
          begin                                      
              if   uppercase(extractfilename(lppe.szExeFile))=procName  then //'WINWORD.EXE'   then
              begin
                  hh:=OpenProcess(PROCESS_ALL_ACCESS,true,lppe.th32ProcessID);
                  TerminateProcess(hh,0);
              end;
              found:=process32next(sshandle,lppe);
          end;end ;{
    关闭进程 过程
    参数 : AFileName  :进程名字
    }
    procedure EndProcess(AFileName: string);
    const
    PROCESS_TERMINATE = $0001;
    var
    ContinueLoop: BOOL;
    FSnapShotHandle: THandle;
    FProcessEntry32: TProcessEntry32;
    begin
      FSnapShotHandle := CreateToolhelp32SnapShot(TH32CS_SNAPPROCESS, 0);
      FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
      ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
      while integer(ContinueLoop) <> 0 do
      begin
            if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
            UpperCase(AFileName))
            or (UpperCase(FProcessEntry32.szExeFile ) =
            UpperCase(AFileName))) then
            TerminateProcess(OpenProcess(PROCESS_TERMINATE, BOOL(0),
            FProcessEntry32.th32ProcessID), 0);
            ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
      end;
    end;