![]() |
Re: Vorhandene Excelliste bearbeiten
Wie greifst du aktuell denn drauf zu?
Und zeig mal bitte die aktuelle Zeile. |
Re: Vorhandene Excelliste bearbeiten
Delphi-Quellcode:
procedure TFMain.Excellfuellen(Sender: TObject);
var Workbook, Worksheet, Filename, Excel: OleVariant; OpenDialog:TOpenDialog; begin OpenDialog:=TOpenDialog.Create(Self); try OpenDialog.InitialDir:=ExtractFilePath(Application.ExeName); OpenDialog.Filter:='xls|*.xls'; if OpenDialog.Execute then begin try Excel := CreateOleObject('Excel.Application'); Excel.Visible := true; { für die testphase sinnvoll } Filename:=OpenDialog.FileName; Workbook:=Excel.Workbooks.Open(filename, emptyParam, emptyParam, emptyParam, emptyParam, emptyParam, emptyParam, emptyParam, emptyParam, emptyParam, emptyParam, emptyParam, emptyParam); // Excel.Workbooks.Add; Excel.ActiveWorkBook.Saved := True; // ... verhindert unliebsame Dialoge //Workbook.Worksheet.UsedRange[LCID].Rows.Count; except ShowMessage('Excel konnte nicht gestartet werden !'); end; end finally OpenDialog.free; end; end; Steig noch nicht so ganz durch hab mein excel sonst immer anders angesprochen:
Delphi-Quellcode:
procedure TFMain.DBGridToExcel(DBGrid:TDBGrid; StartSpalte, StartZeile:integer);
type TSpalten = array[1..256] of string; // Funktion für das exportieren der Datensätze in Excel function CreateSpalten:TSpalten; var i, j, x:integer; abbruch:boolean; begin x:=1; abbruch:=false; j:=0; while (j <= 26) and not abbruch do begin i:=1; while (i <= 26) and not abbruch do begin if j = 0 then Result[x]:=chr(i+64) else Result[x]:=chr(j+64)+chr(i+64); inc(i); inc(x); abbruch:=(x > 256); end; inc(j); end; end; var Excel:TExcelApplication; i, Zeile, lcid:integer; Workbook:_Workbook; Sheet, Zelle, Inhalt:Variant; Spalten:TSpalten; begin if assigned(DBGrid) and assigned(DBGrid.DataSource) and assigned(DBGrid.DataSource.DataSet) then begin if DBGrid.DataSource.DataSet.Active then begin Excel:=TExcelApplication.Create(nil); try lcid:=GetUserDefaultLCID; Excel.Connect; Excel.Visible[lcid]:=true; Excel.UserControl:=true; Workbook:=Excel.Workbooks.Add(EmptyParam, lcid); Spalten:=CreateSpalten; Zeile:=StartZeile; for i:=43 to DBGrid.FieldCount do begin Inhalt:=DBGrid.Fields[i-1].DisplayName; Zelle:=Excel.Cells.Range[ Spalten[i+StartSpalte-1]+inttostr(Zeile), Spalten[i+StartSpalte-1]+inttostr(Zeile) ]; Zelle.Value:=Inhalt; Zelle.Font.Bold:=true; end; DBGrid.DataSource.DataSet.First; while not DBGrid.DataSource.DataSet.Eof do begin inc(Zeile); for i:=1 to DBGrid.FieldCount do begin Inhalt:=DBGrid.DataSource.DataSet.FieldByName( DBGrid.Fields[i-1].FieldName ).AsString; Zelle:=Excel.Cells.Range[ Spalten[i+StartSpalte-1]+inttostr(Zeile), Spalten[i+StartSpalte-1]+inttostr(Zeile) ]; Zelle.Value:=Inhalt; end; DBGrid.DataSource.DataSet.Next; end; Sheet:=Workbook.ActiveSheet; Sheet.Columns[ Spalten[StartSpalte]+':'+Spalten[StartSpalte+DBGrid.FieldCount] ].EntireColumn.AutoFit; finally Excel.Disconnect; Excel.free; end; end; end; end; |
Re: Vorhandene Excelliste bearbeiten
Dann füge nochmal folgendes hinzu, dass du auf ein Worksheet zugreifen kannst.
Delphi-Quellcode:
var WS : _WorkSheet; ... begin ... WS := Workbook.ActiveSheet as _Worksheet; WS.UsedRange[LCID].Rows.Count; end; |
Re: Vorhandene Excelliste bearbeiten
Das der Code fehlt macht Sinn aber es kommt leider die Gleiche Fehlermeldung wie zuvor :gruebel:
|
Re: Vorhandene Excelliste bearbeiten
Hallo Reinhardtinho,
mit Rows.Count ermittelt man die letzte Zeile von Excel, das ist 65536. Ich Denke das Lill Jens die letzte belegte Zelle in einer Zeile meint. Beispiel in VBA: Sub LetztebelegteZelle() strAntwort = MsgBox(Rows.Count) End Sub Bis bald Chemiker |
Re: Vorhandene Excelliste bearbeiten
Ich habe ein VB-Makro, welches mir aus einer Exceltabelle mit Daten ein SQL-Skript erstellt. Das ist die Schleife, die die komplette Tabelle durcharbeitet.
Code:
Dieser Teil geht jede Zeile und Spalte durch.
For iWks = 1 To ActiveWorkbook.Worksheets.Count
Open sPath & "Inhalt-Kostenstellen.sql" For Output As #1 Set rng = ActiveWorkbook.Worksheets(iWks).Range("A1").CurrentRegion Print #1, "delete from kostenstellen where mandant > 0;" For iRow = 2 To rng.Rows.Count sTxt = sTxt & "INSERT INTO Kostenstellen(Mandant,Abteilung,Kostenstelle,Bezeichnung,CompanyID,Gesellschaft,Title) VALUES (" For iCol = 1 To rng.Columns.Count - 1 If iCol = rng.Columns.Count - 1 Then sTxt = sTxt & "'" & ActiveWorkbook.Worksheets(iWks).Cells(iRow, iCol).Value & "'); " Else If iCol = 1 Then sTxt = sTxt & ActiveWorkbook.Worksheets(iWks).Cells(iRow, iCol).Value & ", " Else sTxt = sTxt & "'" & ActiveWorkbook.Worksheets(iWks).Cells(iRow, iCol).Value & "', " End If End If Next iCol Print #1, Left(sTxt, Len(sTxt) - 1) sTxt = "" Next iRow Close #1 Next iWks |
Re: Vorhandene Excelliste bearbeiten
@Chemiker:
Also bei mir erhalte ich die letzte gefüllte Zeile (mit UsedRange[LCID].Rows.Count). Delphi 5 und die Komponente TExcelApplication. MFG Lorenz |
Re: Vorhandene Excelliste bearbeiten
Hallo Reinhardtinho,
habe leider UsedRange überlesen. Wenn man so, den benutzten Bereich anspricht, sollte man nur wissen, dass nicht die letzte mit Daten gefüllte Zeile angesprochen wird. Das Bedeutet das es reicht eine Zelle zu formatieren sagen wir einmal auf eine Uhrzeit, oder man Zellen einfärbt und schon wird diese Zelle zur letzten Zelle. Bis bald Chemiker |
Re: Vorhandene Excelliste bearbeiten
Hallo,
mein Vorschlag ist zwar recht einfach gestrickt, funktionier jedoch. Mein PC hat bis Zeile 10.000 ca. 6 Sekunden benötigt. Grüße Simmi [delphi] uses ...., ComObj; var excel: Variant; function EXCELStarten (var m_EXCEL : Variant): boolean; begin try m_EXCEL := CreateOleObject('Excel.Application'); Result := TRUE; except ShowMessage('Excel konnte nicht gestartet werden!'); Result := FALSE; Exit end; end; Function TForm1.Zeile_Nr: Longword; //Excel 2007 hat mehr als 65535 Zeilen var i : Longword; s : string; begin excel.workbooks.open(OpenDialog1.FileName); excel.sheets['Tabelle1'].activate; i:=1; repeat s:=excel.cells[i,Spalte]; //Spalte mit Daten wählen if s<>'' then inc(i); until s=''; Zeile_Nr:=i; end; procedure TForm1.Button1Click(Sender: TObject); begin if not OpenDialog1.Execute then exit; if not EXCELStarten (Excel) then exit; showmessage('Nächste freie Zeile ist '+IntToStr(Zeile_nr)); end; |
Re: Vorhandene Excelliste bearbeiten
Hallo simmi,
was passiert, wenn Du innerhalb der Spalte eine Leere Zelle hast, dann hört die Funktion auf zu zählen und gibt ein falsches Ergebnis zurück. Bis bald Chemiker |
Alle Zeitangaben in WEZ +1. Es ist jetzt 14:53 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz