【例子介绍】
excel 转 txt
【相关图片】
【源码结构】
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Grids; 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; FExcel.DisplayAlerts := False; FWorkBook.Close; FExcel.Quit; end; end; end.
评论