Today, Delphi is one of the popular desktop programming tool. There are thousand even million programmer in this world using delphi as their favorit tool. This site try to collect the examples, tips and tricks of Delphi programming. We collect, test them and redistribute the collection of delphi programming for you.

Export Excel File to Text File

Author: admin | Category: Files, Tips and Tricks

//This is how to convert Excel file to as text file
//ExcelSaveAsText function taken from Torry's Delphi Page http://www.swissdelphicenter.ch/torry/
//Use Button1 (TButton) and try the code
 
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
 
type
  TForm1 = class(TForm)
    Button1: TButton;
    OpenDialog1: TOpenDialog;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    StrExcelFile:String;
  end;
 
var
  Form1: TForm1;
 
implementation
 
uses
ComObj;
 
{$R *.dfm}
 
function ExcelSaveAsText(ExcelFile, TextFile: TFileName): Boolean;
const
  xlText = -4158;
var
  ExcelApp: OleVariant;
  vTemp1, vTemp2, vTemp3: OLEVariant;
begin
  Result := False;
  try
    ExcelApp := CreateOleObject('Excel.Application');
  except
    // Fehler beim öffnen von Excel...
    // Error occured...
    Exit;
  end;
  try
    ExcelApp.Workbooks.Open(ExcelFile);
    ExcelApp.DisplayAlerts := False;
    vTemp3 := False;
    vTemp2 := xlText;
    vTemp1 := TextFile;
    ExcelApp.ActiveWorkbook.SaveAs(vTemp1, vTemp2, vTemp3);
    Result := True;
  finally
    ExcelApp.Quit;
    ExcelApp := Unassigned;
  end;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
  begin
    StrExcelFile := OpenDialog1.FileName;
    ExcelSaveAsText(StrExcelFile,'C:\ExportResult.txt') ;
    Showmessage('Saved as '+StrExcelFile);
  end;
end;
 
end.

Tags: ,