Unit excel;
interface
Uses
Windows,ActiveX,Messages,SysUtils,Classes,Graphics,Controls,Forms,Dialogs,StdCtrls,ComObj;
{ ComObj是操作OLE对象的函数集}
function ReadExcel(filename:string;sheet:string;Rows:Integer;Columns:integer):string;
procedure WriteExcel(filename:string;sheet:string;Rows:Integer;Columns:integer;txt:string);
var
ExcelApp:Variant;
ExcelBook: Variant;
ExcelSheet: Variant;
implementation
procedure WriteExcel(filename:string;sheet:string;Rows:Integer;Columns:integer;txt:string);
begin
{创建OLE对象:Excel Application与WordBook}
CoInitialize(nil);
ExcelApp:=CreateOleObject('Excel.Application');
if not FileExists(filename) then
begin
ExcelBook:=ExcelApp.Workbooks.Add;
ExcelSheet:=ExcelBook.WorkSheets[Sheet];
ExcelSheet.Cells[Rows,Columns]:=txt;
ExcelBook.SaveAs(filename);
ExcelBook.close;
ExcelApp.quit;
end;
if FileExists(filename) then
begin
ExcelBook:=ExcelApp.Workbooks.Open(FileName);
ExcelSheet:=ExcelBook.WorkSheets[Sheet];
ExcelSheet.Cells[Rows,Columns]:=txt;
ExcelBook.Save;
ExcelBook.close;
ExcelApp.quit;
end;
exit;
end;
function ReadExcel(filename:string;sheet:string;Rows:Integer;Columns:integer):string;
begin
{创建OLE对象:Excel Application与WordBook}
ExcelApp:=CreateOleObject('Excel.Application');
ExcelBook:=ExcelApp.WorkBooks.Open(FileName);
ExcelSheet:=ExcelBook.WorksSheets[sheet];
result:=ExcelSheet.Cells[Rows,Columns];
excelbook.close;
ExcelApp.Quit;
exit;
end;
end.