码迷,mamicode.com
首页 > Windows程序 > 详细

delphi读取excel

时间:2019-11-09 00:04:41      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:vat   book   ant   str   file   ring   visible   roc   eol   

unit uExcelUnit;

interface

uses
  Winapi.Windows, System.SysUtils, System.Variants, System.Classes, System.Win.ComObj;

function OpenExcel(filename: string): Variant;
function ReadCell(excel: Variant; sheetIndex: Integer; row, col: Integer): string;
procedure WriteCell(excel: Variant; sheetIndex: Integer; row, col: Integer; value: string);
procedure CloseExcel(excel: Variant);

function OpenReadExcel(filename: string; sheetIndex, row, col: Integer): string;
function OpenWriteExcel(filename: string; sheetIndex, row, col: Integer; value: string): string;

implementation


function OpenReadExcel(filename: string; sheetIndex, row, col: Integer): string;
var
  excel: Variant;
begin
  excel := OpenExcel(filename);
  try
    Result := ReadCell(excel, sheetIndex, row, col);
  finally
    CloseExcel(excel);
  end;
end;

function OpenWriteExcel(filename: string; sheetIndex, row, col: Integer; value: string): string;
var
  excel: Variant;
begin
  excel := OpenExcel(filename);
  try
    WriteCell(excel, sheetIndex, row, col, value);
  finally
    CloseExcel(excel);
  end;
end;

function OpenExcel(filename: string): Variant;
begin
  Result := CreateOleObject(Excel.Application);
  Result.Visible := True;
  Result.WorkBooks.Open(filename);
  Result.Visible := False;
end;

function ReadCell(excel: Variant; sheetIndex: Integer; row, col: Integer): string;
begin
  excel.WorkSheets[sheetIndex].Activate;
  Result := string(excel.Cells[row, col]);
end;

procedure WriteCell(excel: Variant; sheetIndex: Integer; row, col: Integer; value: string);
begin
  excel.WorkSheets[sheetIndex].Activate;
  excel.Cells[row, col] := value;
  excel.ActiveWorkBook.Save;
end;

procedure CloseExcel(excel: Variant);
begin
  excel.Quit;
end;

end.

 

delphi读取excel

标签:vat   book   ant   str   file   ring   visible   roc   eol   

原文地址:https://www.cnblogs.com/onlyou13/p/11823722.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!