/ Forside / Teknologi / Udvikling / Delphi/Pascal / Nyhedsindlæg
Login
Glemt dit kodeord?
Brugernavn

Kodeord


Reklame
Top 10 brugere
Delphi/Pascal
#NavnPoint
oldwiking 603
jrossing 525
rpje 520
EXTERMINA.. 500
gandalf 460
gubi 270
DJ_Puden 250
PARKENSS 230
technet 210
10  jdjespers.. 200
Shellexecute ???
Fra : Lasse Madsen


Dato : 29-11-02 13:46

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



 
 
Pølle (29-11-2002)
Kommentar
Fra : Pølle


Dato : 29-11-02 16:54

Hej lasse

Det er nemmere end du tror.

*.CSV er sandsynligvis registreret til at blive åbnet af Excel, hvilket er
tilfældet på min PC.
ShellExecute(0,'open',Pchar(OpenDialog1.Filename), nil,nil,SW_SHOW); //
OpenDialog1.Filename indeholder nemlig også stien, når du har acepteret
filen vha. OpenDialog1

Er navnet ikke registreret til at blive åbnet af Excel, så gør du sådan:
ShellExecute(0,'open',Pchar("C:\Stien til Excel\Excel.exe"
"OpenDialog1.Filename"), nil,nil,SW_SHOW);
Anfårselstegnene ( " ) skal medtages, når der er mellemrum. Derfor sætter
jeg altid ( " ).

Tilsvarende kan du skrive 'Print' istedet for 'Open', hvis du ønsker at
printe *.CSV filen.

Mvh
Pølle

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);
ShellExecute(0,'open',Pchar(Filename), nil,nil,SW_SHOW);
end;

end;



René Jensen (29-11-2002)
Kommentar
Fra : René Jensen


Dato : 29-11-02 17:23

Lasse Madsen wrote:
> jeg kunne godt tænke mig at jeg via opendialog kunne åbne en af de csv filer
> og automatisk starte excel op med filen.

F.eks. som nedenstående kode; min Office installation har automatisk
lagt sig min PATH, så det kan godt være at du bliver nød til at tilføje
fuld sti til EXCEL.EXE.

with OpenDialog1 do
begin
Filter := 'Microsoft Excel dokumenter (*.csv, *.xls)|*.CSV;*.XLS|Alle
filtyper (*.*)|*.*';
Title := 'Åbn Microsoft Excel dokument';
if Execute then
if FileExists(FileName) then
ShellExecute(Handle, PChar('open'), PChar('EXCEL.EXE'),
PChar(FileName), nil, SW_SHOW)
else
begin
MessageBeep(MB_ICONASTERISK);
MessageDlg('Filen eksisterer ikke!', mtError, [mbOk], 0);
end;
end;

Med venlig hilsen,
René Jensen


Mors (06-12-2002)
Kommentar
Fra : Mors


Dato : 06-12-02 08:44

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
>
>



Søg
Reklame
Statistik
Spørgsmål : 177483
Tips : 31964
Nyheder : 719565
Indlæg : 6408395
Brugere : 218884

Månedens bedste
Årets bedste
Sidste års bedste