Einzelnen Beitrag anzeigen

Kostas

Registriert seit: 14. Mai 2003
Ort: Gerstrhofen
1.058 Beiträge
 
Delphi 10 Seattle Enterprise
 
#6

Re: Excel import in stringgrid

  Alt 4. Okt 2009, 20:58
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:
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;
Gruß Kostas.
Miniaturansicht angehängter Grafiken
excel2_113.png   excel_151.png  
  Mit Zitat antworten Zitat