ExcelCopyToStringGrid Fonksiyonu

Delphi'de kod yazma ile ilgili sorularınızı bu foruma yazabilirsiniz.
Cevapla
bymerag
Üye
Mesajlar: 18
Kayıt: 15 Mar 2008 10:57

ExcelCopyToStringGrid Fonksiyonu

Mesaj gönderen bymerag »

Kod: Tümünü seç

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;



Arkadaşlar Merhaba yukarıda verdiğim kodu nasıl kullanabilirim
Yardım ederseniz çok sevinirim
kolay Gelsin
Kullanıcı avatarı
mrmarman
Üye
Mesajlar: 4741
Kayıt: 09 Ara 2003 08:13
Konum: İstanbul
İletişim:

Re: ExcelCopyToStringGrid Fonksiyonu

Mesaj gönderen mrmarman »

Merhaba.

- Cevaplanmamış sorulara bakınıyordum da dikkatimi çekti.
- kodun kaynağı konusunda fikir beyan edemiyorum ama yazan kişinin kendi projesinde excel'den veri almak konusunda dinamik bir yapı oluşturmak istediğini değerlendiriyorum.
- Merakınızı gidermek adına, kullanım şeklini sizin için incelediğim fonksiyonun açılımını aşağıda veriyorum.

Ne iş yaptığını şöyle özetleyim. Parametre ile daha önce açtığınız bir EXCEL dosyasından bilgileri alıp yine parametre ile belirttiğiniz StringGrid'e yerleştiren bir fonksiyon.
a. Excel'den hangi satır/sütundan başlayıp kaç satır/sütun alacağınızı yine parametreler ile belirtiyorsunuz.
b. StringGrid'de ise aktarım gerçekleşecek satır/sütunu belirtiyorsunuz.
c. Ayrıca StringGrid için daha önce içerik varsa temizlemesini
d. StringGrid için otomatik sütun genişliği ayarlaması yapmasını isteyebiliyorsunuz.


Kodun düzenli (okuması kolay) hali:

Kod: Tümünü seç

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;

Kullanımı : Açıklayıcı olsun diye değişkenler tanımlayıp isimlerini uzun tuttum.
Not: Uses'a ComObj ekleyiniz.

Kod: Tümünü seç

// Uses ComObj
procedure TForm1.BitBtn1Click(Sender: TObject);
Var
  xExcel      : OleVariant;
  strDosyaAdi : String;
  iExcelBasSatir,
  iExcelBasSutun,
  iExcelAlinacakSatirSayisi,
  iExcelAlinacakSutunSayisi,
  iStringGridBaslanacakSatir,
  iStringGridBaslanacakSutun : Integer;

  boolStringGridOnceTemizle,
  boolStringGridOtomatikSutunGenisligi : Boolean;
begin
  strDosyaAdi := 'd:\Belgelerim\exceldosyam.xls';
  Try
    xExcel := CreateOleObject('Excel.Application');
    xExcel.Workbooks.Open( strDosyaAdi );
  Except
    MessageDlg('İşlem Başarısız...', mtError, [mbOk], 0);
    Exit;
  End;
  xExcel.Visible := True;
  iExcelBasSatir := 2;
  iExcelBasSutun := 1;
  iExcelAlinacakSatirSayisi  := 100;
  iExcelAlinacakSutunSayisi  := 5;
  iStringGridBaslanacakSatir := 0;
  iStringGridBaslanacakSutun := 0;

  boolStringGridOnceTemizle            := True;
  boolStringGridOtomatikSutunGenisligi := True;

  ExcelCopyToStringGrid(  xExcel,
                          iExcelBasSatir,
                          iExcelBasSutun,
                          iExcelAlinacakSatirSayisi,
                          iExcelAlinacakSutunSayisi,
                          StringGrid1,
                          iStringGridBaslanacakSatir,
                          iStringGridBaslanacakSutun,
                          boolStringGridOtomatikSutunGenisligi,
                          boolStringGridOnceTemizle
                        );
  xExcel.Workbooks.Close;
end;
Başarılar. :bravo:
Resim
Resim ....Resim
Cevapla