Einzelnen Beitrag anzeigen

Hallo_Thomas

Registriert seit: 18. Apr 2005
Ort: Dresden
405 Beiträge
 
Delphi 2005 Professional
 
#1

Frage zum Import/Export von Excel Dateien

  Alt 26. Sep 2006, 12:26
Datenbank: sonstiges • Zugriff über: sonstiges
Hallo, ich hab zu den folgenden Code mal zwei Fragen:

1.In den uses wird ExcelXP verwendet, ist die Form auch lauffähig ohne Excel, bzw. wie kann ich abfragen ob Excel vorhanden ist?

2.Wie Frage ich die verwendeten Zeilen ab? Habs mit ws.Rows.Count probiert, doch da erhalte ich immer nur 65536 als Wert.

Danke für eure Hilfe!

Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, JvComponentBase, JvDBGridExport,ExcelXP, OleServer;

type
  TForm1 = class(TForm)
    Button1: TButton;
    OpenDialog1: TOpenDialog;
    Button2: TButton;
    SaveDialog1: TSaveDialog;
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  s:string;
  excel: TExcelApplication;
  wb: _WorkBook;
  ws: _WorkSheet;
  lcid: INTEGER;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  lcid := GetUserDefaultLCID;
  excel := TExcelApplication.Create(Form1);
  excel.Connect;
  opendialog1.Filter:= 'Exceldateien (*.xls)|*.XLS';
  IF not opendialog1.Execute THEN exit;
   s:= opendialog1.FileName;
  // Exceldatei laden (test.xls)
  wb := excel.Workbooks.Open(s, emptyParam, emptyParam, emptyParam, emptyParam,
  emptyParam, emptyParam, emptyParam, emptyParam, emptyParam, emptyParam,
  emptyParam, emptyParam,emptyParam, emptyParam,lcid);

  // erstes Worksheet auswählen
  ws := wb.Sheets[1] as _WorkSheet;
  // Feld A1 ausgeben
  ShowMessage(ws.Cells.Item[2,1].TEXT); //Zeile:Spalte
  // Hier in dein Grid übergeben
//...
  wb.Close(FALSE, emptyParam, emptyParam, lcid);
  excel.Quit;

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  lcid := GetUserDefaultLCID;
  excel := TExcelApplication.Create(Form1);
  excel.Connect;
  Savedialog1.Filter:= 'Exceldateien (*.xls)|*.XLS';
  IF not Savedialog1.Execute THEN exit;
   s:= savedialog1.FileName;
  // Exceldatei laden (test.xls)
  wb := excel.Workbooks.Add(emptyParam,lcid);
  ws := wb.Sheets[1] as _WorkSheet;


  ws.Cells.Item[1,1]:='Hallo';
  ws.SaveAs(s, emptyParam, emptyParam, emptyParam, emptyParam, emptyParam, emptyParam, emptyParam, emptyParam, emptyParam);

  wb.Close(FALSE, emptyParam, emptyParam, lcid);
  excel.Quit;
end;

end.
Angehängte Dateien
Dateityp: exe project1_493.exe (464,5 KB, 13x aufgerufen)
Dateityp: zip excel_903.zip (5,2 KB, 17x aufgerufen)
  Mit Zitat antworten Zitat