2,497
社区成员
发帖
与我相关
我的任务
分享
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, BaseGrid, AdvGrid;
type
TForm1 = class(TForm)
Button1: TButton;
OpenDialog1: TOpenDialog;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses Comobj, Clipbrd;
procedure TForm1.Button1Click(Sender: TObject);
var
FExcel: Variant;
FWorkbook: Variant;
FWorksheet: Variant;
slText: TStringList;
begin
if not OpenDialog1.Execute then
exit;
Screen.Cursor := crHourGlass;
try
FExcel := CreateOleObject('excel.application');
except
Screen.cursor := crDefault;
MessageDlg('Could not start Microsoft Excel!', mtError, [mbCancel], 0);
Exit;
end;
try
FWorkBook := FExcel.WorkBooks.Open(OpenDialog1.Filename);
//FWorkSheet := FWorkBook.WorkSheets.Add;
FWorkSheet := FWorkBook.WorkSheets[1];
FWorkSheet.UsedRange.Copy;
slText := TStringList.Create;
try
slText.Text := Clipboard.AsText;
slText.SaveToFile(ChangeFileExt(OpenDialog1.Filename, '.txt'));
finally
slText.Free;
end;
finally
Screen.Cursor := crDefault;
FWorkBook.Close;
FExcel.Quit;
end;
end;
end.