Satır rengi ataması

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

Satır rengi ataması

Mesaj gönderen hido »

Selam,

Listview de kayıtları excel aktarıyorum buraya kadar sorun yok, içeriğe göre satır rengi atama konusunda sıkıntım var;

örnek:
if Items.Item.SubItems[5] = 'ISTANBUL' then olan satır rengi kırmızı olsun, fakat SubItems ISTANBUL hangi satıra denk geleceğini bilemediğim için satır rengi atamasını nasıl yapabilirim acaba?

Kod: Tümünü seç

procedure TForm3.Button1Click(Sender: TObject);
var
  Row, I, Left, Top: Integer;
  Range, Sheet, Excel: VAriant;
begin
  if ListView1.Items.Count > 0 then
  begin
    try
      Excel := CreateOleObject('Excel.Application');
    except
      raise Exception.Create('Excel Programı Bulunamadı!');
    end;
    Screen.Cursor := crHourGlass;
    Excel.SheetsInNewWorkBook := 1;
    Excel.WorkBooks.Add;
    Sheet := Excel.WorkBooks[1].Sheets[1];
    Range := Sheet.Columns;
    Range.Columns[1].ColumnWidth := 6;
    Range.Columns[2].ColumnWidth := 10;
    Range.Columns[3].ColumnWidth := 41;
    Range.Columns[4].ColumnWidth := 10;
    Range.Columns[5].ColumnWidth := 10;
    Range.Columns[6].ColumnWidth := 12;
    Range.Columns[7].ColumnWidth := 14;
    Range.Columns[8].ColumnWidth := 20;

    // KAYITLARI AKTARMAYA BAŞLA
    Sheet.Cells[4, 1] := 'SN';
    Sheet.Cells[4, 2] := Form1.ListView1.Columns[1].Caption;
    Sheet.Cells[4, 3] := Form1.ListView1.Columns[2].Caption;
    Sheet.Cells[4, 4] := Form1.ListView1.Columns[3].Caption;
    Sheet.Cells[4, 5] := Form1.ListView1.Columns[4].Caption;
    Sheet.Cells[4, 6] := Form1.ListView1.Columns[5].Caption;
    Sheet.Cells[4, 7] := Form1.ListView1.Columns[6].Caption;
    Sheet.Cells[4, 8] := Form1.ListView1.Columns[7].Caption;
    Row := 5;


    with Form1.ListView1 do
    begin
      for i := 0 to Items.Count - 1 do
      begin
        if Items.Item[i].SubItems[5] = 'ISTANBUL' then
        begin
          Sheet.Range[xxxxxxx].Font.Color := Clred;
        end;


        Sheet.Cells[Row, 2] := Items.Item[i].SubItems[0];
        Sheet.Cells[Row, 3] := Items.Item[i].SubItems[1];
        Sheet.Cells[Row, 4] := Items.Item[i].SubItems[2];
        Sheet.Cells[Row, 5] := Items.Item[i].SubItems[3];
        Sheet.Cells[Row, 6] := Items.Item[i].SubItems[4];
        Sheet.Cells[Row, 7] := Items.Item[i].SubItems[5];
        Sheet.Cells[Row, 8] := Items.Item[i].SubItems[6];
        inc(Row);
      end;
      Screen.Cursor := crDefault;
      Excel.Activeworkbook.SaveAs('C:\Dosya\Model.xlsx');
      Excel.Visible := True;
    end;
  end;
end;
xxxjedixxx
Üye
Mesajlar: 216
Kayıt: 10 Ara 2013 03:50

Re: Satır rengi ataması

Mesaj gönderen xxxjedixxx »

Merhaba,

Eğer Istanbul daima Subitems[5]'te ise;
ActiveSheet.Cells[Row, 7].Font.Color := clRed; // Subitems[5] için 7 yazılmalı

Herhangi bir Subitems[] içinde ise;

Kod: Tümünü seç

      var col: Integer;
      ...
      for i := 0 to Items.Count - 1 do
      begin
        for col := 0 to Columns.Count - 1 do
        begin
          if Items.Item[i].SubItems[col] = 'ISTANBUL' then
          begin
            ActiveSheet.Cells[Row, col + 2].Font.Color := clRed;
          end;
          Sheet.Cells[Row, col + 2] := Items.Item[i].SubItems[col];
        end;
        inc(Row);
     end;
Kullanıcı avatarı
hido
Üye
Mesajlar: 268
Kayıt: 29 Mar 2014 04:32

Re: Satır rengi ataması

Mesaj gönderen hido »

xxxjedixxx yazdı:Merhaba,

Eğer Istanbul daima Subitems[5]'te ise;
ActiveSheet.Cells[Row, 7].Font.Color := clRed; // Subitems[5] için 7 yazılmalı

Herhangi bir Subitems[] içinde ise;

Kod: Tümünü seç

      var col: Integer;
      ...
      for i := 0 to Items.Count - 1 do
      begin
        for col := 0 to Columns.Count - 1 do
        begin
          if Items.Item[i].SubItems[col] = 'ISTANBUL' then
          begin
            ActiveSheet.Cells[Row, col + 2].Font.Color := clRed;
          end;
          Sheet.Cells[Row, col + 2] := Items.Item[i].SubItems[col];
        end;
        inc(Row);
     end;

Hocam elinize sağlık, Row, 1 yaptığımda 1. kolon rengi değişiyor ancak diğer colonlarıda 2, 3, 4 ... 7 yapmak istediğimde bir türlü uyarlayamadım

Denemelerim:
Sheet.Cells[Row, 1].Font.Color := clRed;
Sheet.Cells[Row, 1,2].Font.Color := clRed;
Sheet.Cells[Row, 1+2].Font.Color := clRed;
Lord_Ares
Üye
Mesajlar: 1070
Kayıt: 15 Eki 2006 04:33
Konum: Çorlu

Re: Satır rengi ataması

Mesaj gönderen Lord_Ares »

Sheet.Cells[Row, 2] yaparsanız ikinci kolon olacaktır. Tek tek uğraşmak istemiyorsanız for döngüsü ile halledebilirsiniz.
for i=1 to 20 do Sheet.Cells[Row, i].Color := clRed; gibi
Kullanıcı avatarı
hido
Üye
Mesajlar: 268
Kayıt: 29 Mar 2014 04:32

Re: Satır rengi ataması

Mesaj gönderen hido »

Onlarca metot denedim fakat SubItems[5] = 'İSTANBUL' un orduğu tüm satırı renkli yapamadım...

Kod: Tümünü seç

with Form1.ListView1 do
          begin
            for i := 0 to Items.Count - 1 do
            begin
             if Items.Item[i].SubItems[5] = 'İSTANBUL' then
              begin
                Excel.ActiveSheet.Cells[i, 1].Font.Color := clBlue;

                // Sheet.Cells[row, i].Font.Color := clRed;
                // Excel.ActiveSheet.Cells[Row, i].Font.Color := clBlue;
                // Excel.ActiveSheet.Range['A15','a1:h1'].Font.Color := clBlue;
                // Excel.Range['A8', 'h8'].Font.Color := clFuchsia;
              end;
speed60
Üye
Mesajlar: 53
Kayıt: 07 Eki 2011 08:07

Re: Satır rengi ataması

Mesaj gönderen speed60 »

if Items.Item.SubItems[5] = 'İSTANBUL' then //bu satırda "İ" algılamıyor olmasın
Kullanıcı avatarı
hido
Üye
Mesajlar: 268
Kayıt: 29 Mar 2014 04:32

Re: Satır rengi ataması

Mesaj gönderen hido »

Yardımlarınız için teşekkür ederim, bu şekilde çözdüm... Excel.ActiveSheet.Rows[Row].Font.Color :=
Cevapla