Delphi操作EXCEL

Uses ExcelXP, Vcl.OleServer, Vcl.OleAuto;


//用Delphi组件TExcelApplication的操作方法.

function TFormExcelImport.ActiveExcelFile(const FileName :String): Boolean;
var
  i: Integer;
  // LCID: Integer;
  ExcelApplication1: TExcelApplication;
  ExcelWorkbook1: TExcelWorkbook;
  ExcelWorksheet1: TExcelWorksheet;
begin
  Result := False;
  try
    ExcelApplication1 := TExcelApplication.Create(Application);
    ExcelWorkbook1 := TExcelWorkbook.Create(Application);
    ExcelWorksheet1 := TExcelWorksheet.Create(Application);
    ExcelApplication1.ConnectKind := ckRunningOrNew;
    ExcelApplication1.Connect;
    // LCID := GetUserDefaultLCID;
    if (ExcelApplication1.Workbooks.Count = 0) then
    begin
      ExcelApplication1.Visible[0] := False;
      ExcelApplication1.Caption := 'HKAClient Loading Microsoft Excel';
    end;
    // 打开工作簿并关联工作簿变量.
    ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks.Open(FileName,
      False, True, EmptyParam, EmptyParam, EmptyParam, True, EmptyParam,
      EmptyParam, EmptyParam, False, EmptyParam, EmptyParam, EmptyParam,
      EmptyParam, 0));
    // 关联工作表变量
    ExcelWorksheet1.ConnectTo(ExcelWorkbook1.ActiveSheet as _WorkSheet);
    ListBoxSheetName.Clear;
    for i := 1 to ExcelWorkbook1.Sheets.Count do
    begin
      ListBoxSheetName.Items.Add((ExcelWorkbook1.Sheets[i] as _WorkSheet).Name);
    end;

    ExcelWorksheet1.Disconnect;
    // ExcelWorkbook1.Disconnect;
    ExcelWorkbook1.Close(False, FileName, EmptyParam, 0);
    // ExcelApplication1.Workbooks.Close(0); //会关闭所有已经打开的Excel文档.
    ExcelApplication1.Disconnect;
    if (ExcelApplication1.Workbooks.Count = 0) or
      (ExcelApplication1.ConnectKind = ckNewInstance) then
      ExcelApplication1.Quit;
    Result := True;
  finally
    ExcelWorksheet1.Free;
    ExcelWorkbook1.Free;
    ExcelApplication1.Free;
  end;
end;


//用ComObj对象.

function TFormExcelImport.OpenExcelFile(const FileName:String): Boolean;
var
  ExcelApp: OleVariant;
  i: Integer;
begin
  Result := False;
  ExcelApp := CreateOleObject('Excel.Application');
  ExcelApp.Caption := 'HKAClient Loading Microsoft Excel';
  ExcelApp.Visible := False;
  ExcelApp.Workbooks.Open(FileName);
  ListBoxSheetName.Clear;
  for i := 1 to ExcelApp.Sheets.Count do
  begin
    ListBoxSheetName.Items.Add(ExcelApp.ActiveWorkbook.Sheets[i].Name);
  end;
 
  ExcelApp.Workbooks.Close;
  ExcelApp.Quit;
  Result := True;
end;



使用方便,功能强大。 type FileCheckResult = (fcrNotExistend,fcrNotXSLFile,fcrValidXSL); //文件不存在,不是XSL文件,合法的XSL文件 TOLEExcel = class(TComponent) private FExcelCreated: Boolean; FVisible: Boolean; FExcel: Variant; //Excel程序对象 FWorkBook: Variant; //Excel工作簿对象 FWorkSheet: Variant; //Excel工作簿 工作表对象 FCellFont: TFont; //单元格字体对象 FTitleFont: TFont; // FFontChanged: Boolean; FIgnoreFont: Boolean; FFileName: TFileName; //********************************************自己添加*****************************// FCreateFromFile:Boolean; //指示是否打开已有文件 FExcelCaption:string; //用程序打开Excel的窗体标 //*********************************来自U_Report*****************************// FRCPrePage:Integer; //每页显示的记录数 FMax:Integer; //最大的数组个数 procedure SetExcelCellFont(var Cell: Variant); procedure SetExcelTitleFont(var Cell: Variant); procedure GetTableColumnName(const Table: TTable; var Cell: Variant); procedure GetQueryColumnName(const Query: TQuery; var Cell: Variant); procedure GetFixedCols(const StringGrid: TStringGrid; var Cell: Variant); procedure GetFixedRows(const StringGrid: TStringGrid; var Cell: Variant); procedure GetStringGridBody(const StringGrid: TStringGrid; var Cell: Variant); protected procedure SetCellFont(NewFont: TFont); procedure SetTitleFont(NewFont: TFont); procedure SetVisible(DoShow: Boolean); function GetCell( ARow,ACol: Integer): string; procedure SetCell(ACol, ARow: Integer; const Value: string); function GetDateCell(ACol, ARow: Integer): TDateTime; procedure SetDateCell(ACol, ARow: Integer; const Value: TDateTime); //*********************************************自己添加************************************// procedure SetCaption(ACaption:string);//设置打开文件后,Excel主程序的窗体标题 function GetCapiton:string;//返回打开文件后,Excel主程序的窗体标题 public constructor Create(AOwner: TComponent); override; destructor Destroy; override; procedure C
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值