Colin,

> a) How do I create an Excel compatible file from a delimited text file ?

This should get you started:

var
  LCID   : Integer;
  sInFileName  : String; // Text file name incl path info
  sOutFileName : String; // Text file name incl path info
begin
  // You can have Excel server components on your form/dm or you can
  // create Excel servers
  xlApp   := TExcelApplication.Create(nil);
  xlBook  := TExcelWorkbook.Create(nil);
  // when you need to be able to access a spreadsheet
  //xlSheet := TExcelWorksheet.Create(nil);

  // note that a few options are available for connect kind
  xlApp.ConnectKind := ckNewInstance;
  xlBook.ConnectKind := ckNewInstance;
  //xlSheet.ConnectKind := ckNewInstance;

  xlApp.Connect;
  xlApp.Workbooks.Add(Null,0);
  xlBook.ConnectTo(xlApp.Workbooks[1]);
  //xlSheet.ConnectTo(xlBook.Sheets[1] as _Worksheet);

  LCID   := GetUserDefaultLCID;
  // aFieldInfo : Array of Variant; see below
  xlApp.Workbooks.OpenText(sInFileName, xlWindows, 1, xlDelimited,
xlDoubleQuote,
      False, False, False, True, False, False, False, aFieldInfo, False,
LCID);

  // Give a new file name or whatever you need to do

  // Save .xls file
  xlBook.SaveAs(sOutFileName, xlExcel9795, EmptyParam, EmptyParam,
      EmptyParam, EmptyParam, OleVariant(1), EmptyParam,
      EmptyParam, EmptyParam, EmptyParam, LCID);

  xlBook.Close(True, EmptyParam, EmptyParam, LCID);
  //xlSheet.Disconnect;
  xlBook.Disconnect;
  xlApp.Disconnect;

  xlApp.Free;
  xlBook.Free;
  //xlSheet.Free;

~~~~~~~~~~~~~~
  // for number of columns in your text file
  for i := 0 to iCols - 1 do begin
    // load field info array
    SetLength(aFieldInfo, i + 1);
    aFieldInfo[i] := VarArrayOf([i + 1, 1]);
  end;

Please note that this code has been cut and pasted from different routines
and have not been tested and does not have any exception handling. Should
work though I believe. Tell me if you want some more samples.

Hope this helps.
Best regards,
Sergei Stenkov
Analyst Programmer
New Tel Limited
[EMAIL PROTECTED]
Tel: 9937 9909

---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED] 
with body of "unsubscribe delphi"
Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/

Reply via email to