Her er noget der virker
Hilsen / Mors
Unit Unit1;
Interface
Uses
Windows, Messages, SysUtils, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls, ComObj, ComCtrls, Grids;
Type
TForm1 = class(TForm)
Button1 : TButton;
OpenDialog1 : TOpenDialog;
StringGrid1: TStringGrid;
Procedure Button1Click(Sender : TObject);
Private
{ Private declarations }
Public
{ Public declarations }
End;
Var
Form1: TForm1;
Implementation
{$R *.DFM}
//--------------------------------------------------------------------
// With this function you can reduce the number of separation
// characters in a line to just one separator.
// This can be handy when values are separated by tabs or spaces.
//--------------------------------------------------------------------
Function ReduceSeparator(Const Ch : Char;
Const S : ANSIString) : ANSIString;
Var
L,I : DWORD;
Bo : Boolean;
Begin
Result := '';
L := Length(S);
Bo := True;
For I := 1 to L do
Begin
If S[I] <> Ch then
Begin
Result := Result + S[I];
Bo := False; // no separator
End;
If (S[I] = Ch) and (Bo = False) then
Begin
Result := Result + S[I];
Bo := True; // skip the next separators, if found
End;
End;
End;
//--------------------------------------------------------------------
// In some applications a simular visual basic script procedure is
// defined, which can also be very handy parsing strings.
// With this procedure you can easily read exported files into your
// application.
// F.i. Excel and Outlook (addressbook) are able to export in "csv"
// format. (CSV = Comma Separated Values)
//--------------------------------------------------------------------
Procedure SplitString(Const Ch : Char; Const AStr : ANSIString;
Var StrArr : TStringList);
Var
AStr1 : ANSIString;
P,L : Integer;
Begin
// Make a copy of the input string (Const)
AStr1 := AStr;
// Empty the stringlist
StrArr.Clear;
Repeat
P := 0;
L := Length(AStr1);
If L > 0 then
Begin
// Check for the first occurence of the separator (Ch)
P := Pos(Ch,AStr1);
If P > 1 then
// Copy the field into the stringlist
StrArr.Add(Copy(AStr1,1,P - 1));
If P = 1 then
// The field is empty
// The separator is the first char of the input string
StrArr.Add('');
If P > 0 then
Begin
// Remove the field from the input string
AStr1 := Copy(AStr1,P + 1,L - P);
// Correct the length of the input string
L := Length(AStr1);
End;
If (P = 0) and (L > 0) then
// Add the last field to stringlist
// A separator (Ch) at the end is not required
StrArr.Add(AStr1);
End;
Until (P = 0);
End;
//--------------------------------------------------------------------
Procedure TForm1.Button1Click(Sender : TObject);
Var
f : TextFile;
asStr : ANSIString;
slA : TStringList;
Row,Column : Integer;
Excel : OLEVariant; // Don't forget: Uses ComObj
Begin
// Set caption of open dialog
OpenDialog1.Title := 'Open file...';
// Set filter of open dialog
OpenDialog1.Filter := 'Comma separated value files|*.csv|All files|*.*';
// Set the default filter index of the open dialog
OpenDialog1.FilterIndex := 0;
If OpenDialog1.Execute then
Begin
// Thanks to article: 479 written by Berhard Angerer
Excel := CreateOLEObject('Excel.Application');
// Add workbook
// Standard containing 3 sheets
Excel.WorkBooks.Add;
// Show Excel
Excel.Visible := True;
// Initialise row
Row := 1;
// Change sheet name into "Demo"
/// Excel.WorkBooks[1].WorkSheets[1].Name := 'Demo';
// You can also access this worksheet with:
// Excel.WorkBooks[1].Worksheet['Sheet1']...
// but that's not very wise:
// In the English version of Excel a sheet is called: "Sheet".
// In the Dutch version of Excel a sheet is called: "Blad".
// So when you use: "Sheet" you'll get an error in all the
// versions of Excel, in which a sheet is not called: "Sheet".
AssignFile(f,OpenDialog1.Filename);
Reset(f);
// Create the stringlist
slA := TStringList.Create;
Repeat
If not Eof(f) then
Begin
Readln(f,asStr);
// If you want to reduce the columns on a row;
// with CSV files it's recommended not to do so;
// but you can by "un-commenting" the follwing line:
// asStr := ReduceSeparator(',',asStr);
// Parse the line
SplitString(',',asStr,slA);
// Copy the fields into Excel
For Column := 0 to slA.Count - 1 do
// Excel.Worksheets[1].Cells[Row,Column + 1].Value :=
slA.Strings[Column];
stringgrid1.Cells[row,column+1] := slA.Strings[Column];
// Next row
Inc(Row);
End;
Until Eof(f);
// Close the file
CloseFile(f);
// Free the stringlist
slA.Free;
End;
End;
//--------------------------------------------------------------------
End. // of unit1
//--------------------------------------------------------------------
**********************
"Lasse Madsen" <lasse.madsen@elektronik.dk> skrev i en meddelelse
news:3de760e7$0$170$edfadb0f@dread15.news.tele.dk...
> Hej !
>
> Jeg har et program der genere nogle CSV filer ....
>
> jeg kunne godt tænke mig at jeg via opendialog kunne åbne en af de csv
filer
> og automatisk starte excel op med filen.
>
> med koden neden for starter excel op men jeg kan ikke rigtigt finde ud af
at
> få excel til at åbne filen som opendialog peger på ?
>
> procedure TForm1.openClick(Sender: TObject);
> begin
> with openDialog1 do
> if Execute
> then begin
> OpenDialog1.Filename := Filename;
> ShellExecute(Handle,'open','c:\program files\microsoft
> office\office\excel.exe',
> nil,nil,SW_SHOW);
> end;
> end;
>
> Kan i hjælpe mig med det ?
>
> M.v.h.
> L. Madsen
>
>