Lazarus中文社区

 找回密码
 立即注册(注册审核可向QQ群索取)

QQ登录

只需一步,快速开始

Lazarus IDE and 组件 下载地址版权申明
查看: 4826|回复: 2

Lazarus 控制OpenOffice 电子表格的类

[复制链接]

该用户从未签到

发表于 2011-1-25 15:10:07 | 显示全部楼层 |阅读模式
//这个类如果还需要增加功能,请参考OOoAPI接口说明。




unit UOffice;


{$mode objfpc}{$H+}


interface


uses
   Classes, SysUtils, Windows, Variants, ComObj, ActiveX, Controls,
  Graphics,ExtCtrls, ComCtrls, StdCtrls, db, Dialogs,forms;




  type


  TOOo=class(TObject)
  private
  //  fOpenOffice->fDesktop->fDocument->fSheets->fCells
    fOpenOffice : Variant;
    fDocument : Variant;
    fConnected : boolean;
    fDocumentOpened : boolean;
    fDesktop : Variant;


    function MakePropertyValue(PropName:string; PropValue:variant):variant;
  public
     IsNeedToANSI:boolean;
     Constructor Create;
     destructor Destroy; override;
     function Connect : boolean;
     procedure Disconnect;


     function OpenDocument(Filename:string):boolean;
     procedure SaveToPDF(FileName:string);
     procedure CloseDocument;
     function Create_NewCalcDoc:boolean;


     procedure output_ListView_to_Calc(ls:TListView);
     procedure output_DataSet_to_Calc(ls:TDataSet);
  end;
//这个类想按照原来的delphi代码写的,没有成功呢,正在努力。
  TMSoffice=class(TObject)
  private


  public
     procedure output_listView_to_Excel(ls:TListView);
  end;








implementation
{ TMSoffice }


procedure TMSoffice.output_listView_to_Excel(ls:TListView);
var
  ExcelApp,workbook,sheet: Variant;
  i,k:integer;
  FileName:string;
  DlgSave:TsaveDialog;
begin
  DlgSave:=TsaveDialog.Create(nil);
  DlgSave.Filter:='*.xls|*.xls';
  if DlgSave.Execute then
  Begin
         ExcelApp := CreateOleObject( 'Excel.Application' );
         excelapp.visible:=false;
         workbook:=excelapp.workbooks.add;
         workbook.active;
         sheet:=workbook.worksheets.add('mysheet');
         ExcelApp.Quit;
         ExcelApp:=Unassigned;
         DlgSave.Destroy;
  end;
end;










{ TOOo }


procedure TOOo.CloseDocument;
begin
    if fDocumentOpened then
    begin
       fDocument.Close(false);


       fDocumentOpened := false;
       fDocument := Unassigned;


       fDesktop.Terminate;
       fDesktop := UnAssigned;
    end;
end;


function TOOo.Connect: boolean;
begin
    if  VarIsEmpty(fOpenOffice) then
        fOpenOffice := CreateOleObject('com.sun.star.ServiceManager');


    fConnected := not (VarIsEmpty(fOpenOffice) or VarIsNull(fOpenOffice));
    Result := fConnected;
end;


constructor TOOo.Create;
begin
  inherited;
  isNeedtoANSI:=true;
  CoInitialize(nil);
end;


destructor TOOo.Destroy;
begin
  CoUninitialize;
  inherited;
end;


procedure TOOo.Disconnect;
begin
   // if fDocumentOpened then
   //    CloseDocument;


    fConnected := false;
    fOpenOffice := Unassigned;
end;


function TOOo.MakePropertyValue(PropName: string;
  PropValue: variant): variant;
var
   Struct: variant;
begin
    Struct := fOpenOffice.Bridge_GetStruct('com.sun.star.beans.PropertyValue');
    Struct.Name := PropName;
    Struct.Value := PropValue;
    Result := Struct;
end;


function TOOo.Create_NewCalcDoc: boolean;
var
   wProperties : Variant;
   wViewSettings : Variant;
   wController : Variant;
begin
   if not fConnected then
      abort;


   fDesktop := fOpenOffice.createInstance('com.sun.star.frame.Desktop');


   wProperties := VarArrayCreate([0, 0], varVariant);
   wProperties[0] := MakePropertyValue('Hidden', True);


   fDocument := fDesktop.loadComponentFromURL('private:factory/scalc' , '_blank', 0, wProperties);


   fDocumentOpened := not (VarIsEmpty(fDocument) or VarIsNull(fDocument));
   result := fDocumentOpened;
end;


function TOOo.OpenDocument(Filename: string): boolean;
var
   wProperties : Variant;
   wViewSettings : Variant;
   wController : Variant;
begin
   if not fConnected then
      abort;


   fDesktop := fOpenOffice.createInstance('com.sun.star.frame.Desktop');


   wProperties := VarArrayCreate([0, 0], varVariant);
   wProperties[0] := MakePropertyValue('Hidden', True);


   fDocument := fDesktop.loadComponentFromURL('file:///'+ StringReplace(FileName, '\', '/', [rfIgnoreCase, rfReplaceAll]) , '_blank', 0, wProperties);


   fDocumentOpened := not (VarIsEmpty(fDocument) or VarIsNull(fDocument));
   result := fDocumentOpened;
end;
procedure TOOo.output_ListView_to_Calc(ls:TListView);
var
   oSheet,oCell,tshs:variant;
   i,j,k:integer;
begin
  if fDocumentOpened then
  begin
      tshs:=fDocument.getSheets;
      tshs.insertNewByName('MySheet',0);
      oSheet:=tshs.getByName('MySheet');


     for i:=0 to ls.Columns.Count-1 do
      begin
          oCell:=oSheet.getCellByPosition (i,0);
          if isNeedtoANSI then oCell.setstring(UTF8decode(ls.Columns.Caption))
          else oCell.setstring(ls.Columns.Caption);
      end;
      j:=1;
      for k:=0 to ls.Items.Count-1 do
      begin
          i:=0;
          oCell:=oSheet.getCellByPosition (i,j);
          if isNeedtoANSI then oCell.setstring(UTF8decode(ls.Items[k].Caption))
          else oCell.setstring(ls.Items[k].Caption);
          for i:=1 to ls.Items[k].SubItems.Count do
          begin
              oCell:=oSheet.getCellByPosition (i,j);
              if isNeedtoANSI then  oCell.setstring(UTF8decode(ls.Items[k].SubItems[i-1]))
              else oCell.setstring(ls.Items[k].SubItems[i-1]);
          end;
          inc(j);
      end;
      oCell:=Unassigned;
      oSheet:=Unassigned;
  end;
end;


procedure TOOo.output_DataSet_to_Calc(ls:TDataSet);
var
   oSheet,oCell,tshs:variant;
   i,j,k:integer;
begin
  if fDocumentOpened then
  begin
      tshs:=fDocument.getSheets;
      tshs.insertNewByName('MySheet',0);
      oSheet:=tshs.getByName('MySheet');


     for i:=0 to ls.Fields.Count-1 do
      begin
          oCell:=oSheet.getCellByPosition (i,0);
          if  isNeedtoANSI then oCell.setstring(UTF8decode(ANSItoUTF8(ls.Fields.FieldName)))
          else oCell.setstring(ls.Fields.FieldName);
      end;
      j:=1; ls.First;
      for k:=0 to ls.RecordCount-1 do
      begin
          for i:=0 to ls.Fields.Count-1 do
          begin
              oCell:=oSheet.getCellByPosition (i,j);
              if  isNeedtoANSI then  oCell.setstring(UTF8decode(ANSItoUTF8(ls.Fields.DisplayText)))
              else oCell.setstring(ls.Fields.DisplayText);
          end;
          inc(j); ls.Next;
      end;
      oCell:=Unassigned;
      oSheet:=Unassigned;
  end;
end;










procedure TOOo.SaveToPDF(FileName: string);
var
   wProperties: variant;


begin
   if not (fConnected and fDocumentOpened) then
      abort;


   wProperties := VarArrayCreate([0, 3], varVariant);
   wProperties[0] := MakePropertyValue('FilterName', 'writer_pdf_Export');


   wProperties[1] := MakePropertyValue('CompressionMode', '1');
   wProperties[2] := MakePropertyValue(&#39ages', 'All');
   wProperties[3] := MakePropertyValue('Overwrite', TRUE);


   fDocument.StoreToURL('file:///'+ StringReplace(FileName, '\', '/', [rfIgnoreCase, rfReplaceAll]), wProperties);
end;


end.
                          

评分

参与人数 1威望 +6 收起 理由
猫工 + 6 优秀文章

查看全部评分

回复

使用道具 举报

该用户从未签到

发表于 2011-1-25 20:34:44 | 显示全部楼层
能在linux等平台用吗?
回复 支持 反对

使用道具 举报

该用户从未签到

 楼主| 发表于 2011-1-26 09:12:41 | 显示全部楼层
没玩过linux,基于win的COM,linux应该是不可以的。
回复 支持 反对

使用道具 举报

*滑块验证:

本版积分规则

QQ|手机版|小黑屋|Lazarus中国|Lazarus中文社区 ( 鄂ICP备16006501号-1 )

GMT+8, 2025-5-2 07:15 , Processed in 0.085699 second(s), 12 queries , Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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