AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Sprachen und Entwicklungsumgebungen Sonstige Fragen zu Delphi Delphi Problem mit einer function( excel in stringgrid)
Thema durchsuchen
Ansicht
Themen-Optionen

Problem mit einer function( excel in stringgrid)

Ein Thema von NikoMitDaMacht · begonnen am 6. Aug 2004 · letzter Beitrag vom 9. Aug 2004
Antwort Antwort
Benutzerbild von NikoMitDaMacht
NikoMitDaMacht

Registriert seit: 27. Apr 2004
Ort: Freiburg
298 Beiträge
 
Delphi 6 Enterprise
 
#1

Problem mit einer function( excel in stringgrid)

  Alt 6. Aug 2004, 15:55
Hallo!

Ich ein grosses problem mit folgender fuction:
von http://www.advdelphisys.com/Code_Lib...pyToStringGrid
Delphi-Quellcode:
Function ExcelCopyToStringGrid(
  Excel : Variant;
  ExcelFirstRow : Integer;
  ExcelFirstCol : Integer;
  ExcelLastRow : Integer;
  ExcelLastCol : Integer;
  StringGrid : TStringGrid;
  StringGridFirstRow : Integer;
  StringGridFirstCol : Integer;
  SizeStringGridToFit : Boolean; {Make the StringGrid the same size as the input range} 
  ClearStringGridFirst : Boolean {cells outside input range in StringGrid are cleared} 
  ): Boolean;
Var
  C,R : Integer;
Begin
  Result := False;
  If ExcelLastCol < ExcelFirstCol Then Exit;
  If ExcelLastRow < ExcelFirstRow Then Exit;
  If (ExcelFirstRow < 1) Or (ExcelFirstRow > 255) Then Exit;
  If (ExcelFirstCol < 1) Or (ExcelFirstCol > 30000) Then Exit;
  If (ExcelLastRow < 1) Or (ExcelLastRow > 255) Then Exit;
  If (ExcelLastCol < 1) Or (ExcelLastCol > 30000) Then Exit;
  If StringGrid = nil Then Exit;
  If SizeStringGridToFit Then
  Begin
    StringGrid.ColCount := ExcelLastCol - ExcelFirstCol + StringGridFirstCol + 1;
    StringGrid.RowCount := ExcelLastRow - ExcelFirstRow + StringGridFirstRow + 1;
  End;
  If ClearStringGridFirst Then
  Begin
    C := StringGrid.ColCount;
    R := StringGrid.RowCount;
    StringGrid.ColCount := 1;
    StringGrid.RowCount := 1;
    StringGrid.Cells[0,0] := '';
    StringGrid.ColCount := C;
    StringGrid.RowCount := R;
  End;

  Result := True;
  For R := ExcelFirstRow To ExcelLastRow Do
  Begin
    For C := ExcelFirstCol To ExcelLastCol Do
    Begin
      Try
        StringGrid.Cells[
          C - ExcelFirstCol + StringGridFirstCol,
          R - ExcelFirstRow + StringGridFirstRow] :=
            Excel.Cells[R, C];
      Except
        Result := False;
      End;
    End;
  End;
End;
mein problen ist dass ich nich weiss was ich für excel einstezten soll.
Wahrscheinelich :" excel : excelworksheet1.?"
weiter weiss ich nicht
Niko
Alles was ein Ende hat, hat auch ein Anfang
  Mit Zitat antworten Zitat
Poolspieler

Registriert seit: 9. Aug 2004
165 Beiträge
 
Delphi 10.3 Rio
 
#2

Re: Problem mit einer function( excel in stringgrid)

  Alt 9. Aug 2004, 10:56
Hallo,
ich habe mich schon mal mehrere Wochen mit Excelautomation herumgeschlagen, das macht nicht wirklich Spaß, weil vor allem alles soooo wunderbar dokumentiert ist...

Auf jeden Fall, die Lösung Deines Problems:

Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, comobj;

type
  TForm1 = class(TForm)
    Button1: TButton;
    StringGrid1: TStringGrid;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

Function ExcelCopyToStringGrid(
  Excel : Variant;
  ExcelFirstRow : Integer;
  ExcelFirstCol : Integer;
  ExcelLastRow : Integer;
  ExcelLastCol : Integer;
  StringGrid : TStringGrid;
  StringGridFirstRow : Integer;
  StringGridFirstCol : Integer;
  SizeStringGridToFit : Boolean; {Make the StringGrid the same size as the input range} 
  ClearStringGridFirst : Boolean {cells outside input range in StringGrid are cleared} 
  ): Boolean;
Var
  C,R : Integer;
Begin
  Result := False;
  If ExcelLastCol < ExcelFirstCol Then Exit;
  If ExcelLastRow < ExcelFirstRow Then Exit;
  If (ExcelFirstRow < 1) Or (ExcelFirstRow > 255) Then Exit;
  If (ExcelFirstCol < 1) Or (ExcelFirstCol > 30000) Then Exit;
  If (ExcelLastRow < 1) Or (ExcelLastRow > 255) Then Exit;
  If (ExcelLastCol < 1) Or (ExcelLastCol > 30000) Then Exit;
  If StringGrid = nil Then Exit;
  If SizeStringGridToFit Then
  Begin
    StringGrid.ColCount := ExcelLastCol - ExcelFirstCol + StringGridFirstCol + 1;
    StringGrid.RowCount := ExcelLastRow - ExcelFirstRow + StringGridFirstRow + 1;
  End;
  If ClearStringGridFirst Then
  Begin
    C := StringGrid.ColCount;
    R := StringGrid.RowCount;
    StringGrid.ColCount := 1;
    StringGrid.RowCount := 1;
    StringGrid.Cells[0,0] := '';
    StringGrid.ColCount := C;
    StringGrid.RowCount := R;
  End;

  Result := True;
  For R := ExcelFirstRow To ExcelLastRow Do
  Begin
    For C := ExcelFirstCol To ExcelLastCol Do
    Begin
      Try
        StringGrid.Cells[
          C - ExcelFirstCol + StringGridFirstCol,
          R - ExcelFirstRow + StringGridFirstRow] :=
            Excel.Cells[R, C];
      Except
        Result := False;
      End;
    End;
  End;
End;

procedure TForm1.Button1Click(Sender: TObject);
var
  excel: Variant;
begin
 excel := createoleobject('excel.application');
 excel.workbooks.open ('c:\mappe1.xls');
 ExcelCopyToStringGrid(excel, 1, 1, 14, 7, StringGrid1, 0, 0, true, true);
 excel.application.quit;
end;

end.
Vergiss comobj in der uses-clausel nicht!

Gruß und viel Spaß damit

Poolspieler
Andreas
  Mit Zitat antworten Zitat
Benutzerbild von Luckie
Luckie

Registriert seit: 29. Mai 2002
37.621 Beiträge
 
Delphi 2006 Professional
 
#3

Re: Problem mit einer function( excel in stringgrid)

  Alt 9. Aug 2004, 14:25
Habe ich ein deja vú? Das haben wir doch erst vor kurzem alles durchgekaut mit dir - oder?
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 09:57 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz