![]() |
Excel import in stringgrid
Hi,
ich möchte eine Excel Tabelle in ein Stringgrid importieren und verwende den Code von SwissDelphi
Delphi-Quellcode:
Allerdings bekomme ich immer wieder den Fehler
function Xls_To_StringGrid(AGrid: TStringGrid; AXLSFile: string): Boolean;
const xlCellTypeLastCell = $0000000B; var XLApp, Sheet: OLEVariant; RangeMatrix: Variant; x, y, k, r: Integer; begin Result := False; // Create Excel-OLE Object XLApp := CreateOleObject('Excel.Application'); try // Hide Excel XLApp.Visible := False; // Open the Workbook XLApp.Workbooks.Open(AXLSFile); // Sheet := XLApp.Workbooks[1].WorkSheets[1]; Sheet := XLApp.Workbooks[ExtractFileName(AXLSFile)].WorkSheets[1]; // In order to know the dimension of the WorkSheet, i.e the number of rows // and the number of columns, we activate the last non-empty cell of it Sheet.Cells.SpecialCells(xlCellTypeLastCell, EmptyParam).Activate; // Get the value of the last row x := XLApp.ActiveCell.Row; // Get the value of the last column y := XLApp.ActiveCell.Column; // Set Stringgrid's row &col dimensions. AGrid.RowCount := x; AGrid.ColCount := y; // Assign the Variant associated with the WorkSheet to the Delphi Variant RangeMatrix := XLApp.Range['A1', XLApp.Cells.Item[X, Y]].Value; // Define the loop for filling in the TStringGrid k := 1; repeat for r := 1 to y do AGrid.Cells[(r - 1), (k - 1)] := RangeMatrix[K, R]; Inc(k, 1); AGrid.RowCount := k + 1; until k > x; // Unassign the Delphi Variant Matrix RangeMatrix := Unassigned; finally // Quit Excel if not VarIsEmpty(XLApp) then begin // XLApp.DisplayAlerts := False; XLApp.Quit; XLAPP := Unassigned; Sheet := Unassigned; Result := True; end; end; end; Erste Gelegenheit für Exception bei $76F142EB. Exception-Klasse EOleException mit Meldung 'Die Activate-Methode des Range-Objektes konnte nicht ausgeführt werden'. Prozess ExcelTostringgrid.exe (8108) |
Re: Excel import in stringgrid
Habe mal gerade eine kleine Test-App geschrieben.
Läuft ohne Fehlermeldung durch und lädt die Daten ins Stringgrid. Excel 2003, D2007. Wann / Wo kommt denn der Fehler (welche Zeile)? Gruß, Gerald |
Re: Excel import in stringgrid
Auf das Activate kann man verzichten, wenn mit UsedRange gearbeitet wird:
Delphi-Quellcode:
[edit=mkinzler]Delphi-Tag eingefügt Mfg, mkinzler[/edit]
x := XLSheet.UsedRange.Columns.Count;
y := XLSheet.UsedRange.Rows.Count; |
Re: Excel import in stringgrid
Hi
ich hatte das gleiche Problem und habe mir mit einer bescheidenen Krücke geholfen: Die letzte Zelle selber step by step ermitteln :roll: Das Problem tritt nicht mit jedem Excelfile auf (weis aber nicht wann und wann nicht) Zitat:
Grüße Ansgar |
Re: Excel import in stringgrid
Excel erzeugt nur so viele Spalten und Zeilen wie tatsächlich benötigt werden, auch wenn mehr leere Spalten angezeigt werden. Ruft man Methoden von Zell-Objekten auf die nicht existieren, gibt es Zugriffsverletzungen. Um die tatsächlich vorhandenen Zeilen und Spalten zu ermitteln, wird in dem Beispiel Excel aufgefordert, die letzte vorhandene Zelle auszuwählen. Anschließend wird die Position der aktiven Zelle ermittelt.
Sicherer ist es direkt den genutzten Bereich (UsedRange) des XLSheet abzufragen. Dieses Range-Objekt kann man auch sofort nutzen, um an die Daten der Zellen zu gelangen. In dem Beispiel wird dafür erst ein neues Range-Objekt (RangeMatrix) erzeugt. |
Re: Excel import in stringgrid
Liste der Anhänge anzeigen (Anzahl: 2)
Hallo Zusammen,
auch ich beschäftige mich gerade mit dem Beispiel von SwissDelphi. Normale Execl files lassen sich damit recht einfach ansprechen. Aktuell habe ich ein file das schön Bund gemischt einzelen Zellen zusammenverbunuden sind. Das problem ist nun, das nicht elle Zellen ausgelesen werdne können. Ich kann das Excel-File leider nicht als csv wegschreiben, denn ich benötige auch die Farbinformation einzelner Zellen. Also nicht nur die Zelleninhalte sonder auch die Attribute. Ich muss also aus das excel-File zugreifen können. ich habe es mit XLApp.ActiveCell... Sheet.UsedRange.Columns... hilft beides nicht. Generell mal die Frage, gibt es eine Möglichkeit auf alle Zellen des Sheets zuzugreifen? Im screen shot ist eine einfaches excel sheet aufgebaut. Sobald zwei Zellen zusammen verbunden sind funktioniert es nicht mehr. Das Grid wird nicht vollständig aufgebaut und es kommt eine Fehlermeldung das Excel eine Problem verursacht hat. Hat jemand eine Idee? Das Ist meine source.
Delphi-Quellcode:
Gruß Kostas.
function TForm1.Xls_To_StringGrid(AGrid: TStringGrid; AXLSFile: string): Boolean;
const xlCellTypeLastCell = $0000000B; var XLApp, Sheet: OLEVariant; RangeMatrix: Variant; x, y, k, r: Integer; begin Result := False; // Create Excel-OLE Object XLApp := CreateOleObject('Excel.Application'); try // Hide Excel XLApp.Visible := False; // Open the Workbook XLApp.Workbooks.Open(AXLSFile); // Sheet := XLApp.Workbooks[1].WorkSheets[1]; // bsm(XLApp.Workbooks[ExtractFileName(AXLSFile)].WorkSheets.Count); Sheet := XLApp.Workbooks[ExtractFileName(AXLSFile)].WorkSheets[1]; // bsm(Sheet.name); // In order to know the dimension of the WorkSheet, i.e the number of rows // and the number of columns, we activate the last non-empty cell of it //Sheet.Cells.SpecialCells(xlCellTypeLastCell, EmptyParam).Activate; // Get the value of the last row // x := XLApp.ActiveCell.Row; // Get the value of the last column // y := XLApp.ActiveCell.Column; x := Sheet.UsedRange.Columns.Count; y := Sheet.UsedRange.Rows.Count; // Set Stringgrid's row &col dimensions. AGrid.RowCount := x; AGrid.ColCount := y; // Assign the Variant associated with the WorkSheet to the Delphi Variant RangeMatrix := XLApp.Range['A1', XLApp.Cells.Item[X, Y]].Value; // Define the loop for filling in the TStringGrid k := 1; repeat for r := 1 to y do AGrid.Cells[(r - 1), (k - 1)] := RangeMatrix[K, R]; Inc(k, 1); AGrid.RowCount := k + 1; until k > x; // Unassign the Delphi Variant Matrix RangeMatrix := Unassigned; finally // Quit Excel if not VarIsEmpty(XLApp) then begin XLApp.DisplayAlerts := False; XLApp.Quit; XLAPP := Unassigned; Sheet := Unassigned; Result := True; end; end; end; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 16: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 by Thomas Breitkreuz