Seçilen tüm kayıtları listeleme...

Delphi'de kod yazma ile ilgili sorularınızı bu foruma yazabilirsiniz.
Cevapla
Kullanıcı avatarı
hido
Üye
Mesajlar: 268
Kayıt: 29 Mar 2014 04:32

Seçilen tüm kayıtları listeleme...

Mesaj gönderen hido »

Selamlar;

Exceldeki verileri listview de listeliyorum, fakat excel birden fazla kayıt seçtiğimde sadece bir tane excelin verilerini listeliyor, seçilen diğer excelleri de listviewde eklenmesini nasıl sağlaya bilirim?

Kod: Tümünü seç

procedure TForm1.BtnAraClick(Sender: TObject);
const
  xlCellTypeLastCell = $0000000B;
var
  Kayit: TListItem;
  Buldum: String;
  Book: Variant;
  Excel, Sheet: Variant;
  Kolon, SSay: Integer;
begin
  try
    Excel := CreateOleObject('Excel.Application');
    begin
      OpenDialog.Filter := 'Excel *.xls,*.xlsx|*.xls;*.xlsx';
      OpenDialog.Options := OpenDialog.Options + [ofAllowMultiSelect];
      // OpenDialog.FilterIndex := 1;
      if OpenDialog.Execute then
      begin
        Book := Excel.WorkBooks.Open(OpenDialog.FileName);
        Sheet := Book.worksheets[12];
        SSay := Excel.ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell,
          EmptyParam).Row;
        ListView1.Items.Clear;
        ListView1.Items.BeginUpdate;
        try
          for Kolon := 1 to SSay do
          begin
            Buldum := Copy(Sheet.Cells[Kolon, 3].Text, 1, 1);
            if Pos(Buldum, 'E') > 0 then
            begin
              Screen.Cursor := crHourGlass;
              Kayit := ListView1.Items.Add;
              Kayit.Caption := IntToStr(ListView1.Items.Count);
              Kayit.SubItems.Add(Sheet.Cells[Kolon, 2].Text);
              Kayit.SubItems.Add(Sheet.Cells[Kolon, 3].Text);
              Kayit.SubItems.Add(Sheet.Cells[Kolon, 4].Text);
              Kayit.SubItems.Add('');
              Kayit.SubItems.Add('');
              Kayit.SubItems.Add(Sheet.Cells[Kolon, 6].Text);
              Kayit.SubItems.Add(Sheet.Cells[Kolon, 8].Text);
            end;
          end;
        finally
          ListView1.Items.EndUpdate;
        end;
      end;
    end;
    Screen.Cursor := crDefault;
    Excel.WorkBooks.Close;
    Excel.quit;
    Excel := Unassigned;
    Sheet := Unassigned;
  except
  end;
end;
ertank
Kıdemli Üye
Mesajlar: 1716
Kayıt: 12 Eyl 2015 12:45

Re: Seçilen tüm kayıtları listeleme...

Mesaj gönderen ertank »

Merhaba,

Okumayı döngü içinde yapmanız gerekli.

Kod: Tümünü seç

procedure TForm1.Button1Click(Sender: TObject);
const
  xlCellTypeLastCell = $0000000B;
var
  Kayit: TListItem;
  Buldum: String;
  Book: Variant;
  Excel, Sheet: Variant;
  Kolon, SSay: Integer;
  i: Integer;
begin
  try
    Excel := CreateOleObject('Excel.Application');
    begin
      OpenDialog.Filter := 'Excel *.xls,*.xlsx|*.xls;*.xlsx';
      OpenDialog.Options := OpenDialog.Options + [ofAllowMultiSelect];
      // OpenDialog.FilterIndex := 1;
      if OpenDialog.Execute then
      begin
        for i := 0 to OpenDialog.Files.Count-1 do
        begin
          Book := Excel.WorkBooks.Open(OpenDialog.Files[i]);
          Sheet := Book.worksheets[12];
          SSay := Excel.ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell,
            EmptyParam).Row;
          ListView1.Items.Clear;
          ListView1.Items.BeginUpdate;
          try
            for Kolon := 1 to SSay do
            begin
              Buldum := Copy(Sheet.Cells[Kolon, 3].Text, 1, 1);
              if Pos(Buldum, 'E') > 0 then
              begin
                Screen.Cursor := crHourGlass;
                Kayit := ListView1.Items.Add;
                Kayit.Caption := IntToStr(ListView1.Items.Count);
                Kayit.SubItems.Add(Sheet.Cells[Kolon, 2].Text);
                Kayit.SubItems.Add(Sheet.Cells[Kolon, 3].Text);
                Kayit.SubItems.Add(Sheet.Cells[Kolon, 4].Text);
                Kayit.SubItems.Add('');
                Kayit.SubItems.Add('');
                Kayit.SubItems.Add(Sheet.Cells[Kolon, 6].Text);
                Kayit.SubItems.Add(Sheet.Cells[Kolon, 8].Text);
              end;
            end;
          finally
            ListView1.Items.EndUpdate;
          end;
        end;
      end;
    end;
    Screen.Cursor := crDefault;
    Excel.WorkBooks.Close;
    Excel.quit;
    Excel := Unassigned;
    Sheet := Unassigned;
  except
  end;
end;
Kullanıcı avatarı
hido
Üye
Mesajlar: 268
Kayıt: 29 Mar 2014 04:32

Re: Seçilen tüm kayıtları listeleme...

Mesaj gönderen hido »

Çok teşekkür ederim...
Kullanıcı avatarı
hido
Üye
Mesajlar: 268
Kayıt: 29 Mar 2014 04:32

Re: Seçilen tüm kayıtları listeleme...

Mesaj gönderen hido »

Selam;

Birden fazla excel seçip listviewe listeleme yapmak istediğimde resimdeki hatayı alıyorum bu hata neden kaynaklı acaba?

Tek, tek listelemede sorun çıkmıyor.

Resim

Kod: Tümünü seç

procedure TForm1.BtnAraClick(Sender: TObject);
const
  xlCellTypeLastCell = $0000000B;
var
  ListV: TListItem;
  TCevir, TAl: String;
  Book: Variant;
  Excel, Sheet: Variant;
  SSay, J, I: Integer;
begin
  try
    Excel := CreateOleObject('Excel.Application');
    OpenDialog.Filter := 'Excel *.xls,*.xlsx|*.xls;*.xlsx';
    OpenDialog.Options := OpenDialog.Options + [ofAllowMultiSelect];
    if OpenDialog.Execute then
    begin
      for I := 0 to OpenDialog.Files.Count - 1 do
      begin
        Book := Excel.WorkBooks.Open(OpenDialog.Files[I]);
        TAl := ChangeFileExt(ExtractFileName(OpenDialog.Files[I]), '');

        Sheet := Book.Worksheets['Master']; // Sayfa adı
        SSay := Excel.ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell,
          EmptyParam).Row;
        ListView1.Items.BeginUpdate;
        try
          Delete(TAl, 11, 30);
          TCevir := FormatDateTime('dd/mm/yyyy dddd', StrtoDate(TAl));
          for J := 1 to SSay do
          begin
            if Pos(Sheet.Cells[J, 8].Text, Aranan.Text) > 0 then
            begin
              Screen.Cursor := crHourGlass;
              ListV := ListView1.Items.Add;
              ListV.Caption := IntToStr(ListView1.Items.Count);
              ListV.SubItems.Add(Sheet.Cells[J, 4].Text);
              ListV.SubItems.Add(TCevir);
              ListV.SubItems.Add(Sheet.Cells[J, 2].Text);
              ListV.SubItems.Add(Sheet.Cells[J, 6].Text);
              end;
          end;
        finally
          ListView1.Items.EndUpdate;
          Screen.Cursor := crDefault;
          Excel.WorkBooks.Close;
          Excel.quit;
          Excel := Unassigned;
          Sheet := Unassigned;
        end;
      end;
    end;
  except
  end;
end;
Kullanıcı avatarı
kimimben
Üye
Mesajlar: 129
Kayıt: 28 Oca 2016 04:41
Konum: İstanbul

Re: Seçilen tüm kayıtları listeleme...

Mesaj gönderen kimimben »

breakpoint koyup, adım adım çalıştırıp hangi satırda exception atıyor tespit edebilirsin.
ertank
Kıdemli Üye
Mesajlar: 1716
Kayıt: 12 Eyl 2015 12:45

Re: Seçilen tüm kayıtları listeleme...

Mesaj gönderen ertank »

hido yazdı:Selam;

Birden fazla excel seçip listviewe listeleme yapmak istediğimde resimdeki hatayı alıyorum bu hata neden kaynaklı acaba?

Tek, tek listelemede sorun çıkmıyor.

Resim

Kod: Tümünü seç

procedure TForm1.BtnAraClick(Sender: TObject);
const
  xlCellTypeLastCell = $0000000B;
var
  ListV: TListItem;
  TCevir, TAl: String;
  Book: Variant;
  Excel, Sheet: Variant;
  SSay, J, I: Integer;
begin
  try
    Excel := CreateOleObject('Excel.Application');
    OpenDialog.Filter := 'Excel *.xls,*.xlsx|*.xls;*.xlsx';
    OpenDialog.Options := OpenDialog.Options + [ofAllowMultiSelect];
    if OpenDialog.Execute then
    begin
      for I := 0 to OpenDialog.Files.Count - 1 do
      begin
        Book := Excel.WorkBooks.Open(OpenDialog.Files[I]);
        TAl := ChangeFileExt(ExtractFileName(OpenDialog.Files[I]), '');

        Sheet := Book.Worksheets['Master']; // Sayfa adı
        SSay := Excel.ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell,
          EmptyParam).Row;
        ListView1.Items.BeginUpdate;
        try
          Delete(TAl, 11, 30);
          TCevir := FormatDateTime('dd/mm/yyyy dddd', StrtoDate(TAl));
          for J := 1 to SSay do
          begin
            if Pos(Sheet.Cells[J, 8].Text, Aranan.Text) > 0 then
            begin
              Screen.Cursor := crHourGlass;
              ListV := ListView1.Items.Add;
              ListV.Caption := IntToStr(ListView1.Items.Count);
              ListV.SubItems.Add(Sheet.Cells[J, 4].Text);
              ListV.SubItems.Add(TCevir);
              ListV.SubItems.Add(Sheet.Cells[J, 2].Text);
              ListV.SubItems.Add(Sheet.Cells[J, 6].Text);
              end;
          end;
        finally
          ListView1.Items.EndUpdate;
          Screen.Cursor := crDefault;
          Excel.WorkBooks.Close;
          Excel.quit;
          Excel := Unassigned;
          Sheet := Unassigned;
        end;
      end;
    end;
  except
  end;
end;

Merhaba,

Excel, Book ve Sheet nesnelerini for döngüsü içinde oluşturup, Close ve Unassigned yapmayı deneryebilir misiniz?

Sanki bir Excel çalışma kitabı kapatılmadan diğeri açılıyor gibi. Ancak sorunun çözümü olduğundan emin değilim.
Cevapla