Delphi7实现excel文件跟txt文件的相互转换
Delphi7实现excel文件和txt文件的相互转换
小弟最近刚刚接触Delphi,希望各位大神慷慨相助啊~~
求救内容:
excel文件和txt文件的相互转换,不用打开文件,转换后存在同目录下的同名文件中~
跪谢啊~
------解决方案--------------------
Delphi7,Office2007测试通过
小弟最近刚刚接触Delphi,希望各位大神慷慨相助啊~~
求救内容:
excel文件和txt文件的相互转换,不用打开文件,转换后存在同目录下的同名文件中~
跪谢啊~
------解决方案--------------------
Delphi7,Office2007测试通过
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.