ListViewden excel çıktı

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

ListViewden excel çıktı

Mesaj gönderen hido »

Selam, iki gündür araştırıyorum bir türlü çalışan kod bulamadım, ListViewden excele çıktı alabileceğim kod konusunda yardımcı olabilecek var mı?
Kullanıcı avatarı
unicorn64
Üye
Mesajlar: 919
Kayıt: 04 Nis 2006 08:56
Konum: yine yeniden Ankara ^_^

Re: ListViewden excel çıktı

Mesaj gönderen unicorn64 »

bir zamanlar şöyle bir fonksiyon hazırlamıştım...

Kod: Tümünü seç

type
  TDynStringArray=array of string;
  
procedure TMainForm.ExportToExcel(aView: TListView; aBaslik: string; aAlanlar,
  aBasliklar: array of string);
var
  tablo:Tstringlist;
  x,l2:integer;
  sutunsayisi:integer;
  xDegerler:TDynStringArray;

  function AddRow(aDegerler:TDynStringArray):string; overload;
  var
    i,l:integer;
  begin
    Result:='<tr>';
    l:=length(aDegerler);

    for i := 0 to l - 1 do
      Result:=Result+'<td>'+AnsiReplaceStr(aDegerler[i],#13#10,'<br>')+'</td>';

    Result:=Result+'</tr>';
  end;

  function AddRow(aDeger:string;aColumnCount:string;aAlign:string):string;overload;
  begin
    Result:='<tr>';
    Result:=Result+'<td colspan="'+aColumnCount+'" align="'+aAlign+'">'+aDeger+'</td>';
    Result:=Result+'</tr>';
  end;

  function ParseIt(aItem:TListItem):TDynStringArray;
  var
    m:integer;
    s:string;
    xs:string;
    xi:integer;
  begin
    SetLength(Result,sutunsayisi);

    for m := 0 to sutunsayisi - 1 do
    begin
      s:=aAlanlar[m];
      Result[m] := '';
      if s='%Caption%' then
        Result[m]:=aItem.Caption
      else if pos('%SubItems%',s)>0 then
      begin
        xs:=copy(s,11,length(s)-10);
        xi:=strtointdef(xs,0);

        if xi < aItem.SubItems.Count then
          Result[m]:=aItem.SubItems[xi];
      end
      else
        Result[m]:=s;
    end;
  end;

  function StaticToDynArray(aDegerler:array of string):TDynStringArray;
  var
    i,l:integer;
  begin
    l:=length(aDegerler);
    setlength(Result,l);

    for i := 0 to l - 1 do
        Result[i]:=aDegerler[i];
  end;

begin

  SaveToExcel.FileName := aBaslik + '_' + DateToStr(now);
  if SaveToExcel.Execute then
  begin
    
    tablo:=TStringList.Create;

    sutunsayisi:=length(aAlanlar);
    tablo.Add('<html>');
    tablo.Add('<head>');
    tablo.Add('  <meta http-equiv="content-type" content="text/html; charset=UTF-8">');
    tablo.Add('</head>');
    tablo.Add('<body>');
    tablo.Add('<table border="1">');
    tablo.Add(AddRow(aBaslik,inttostr(sutunsayisi),'center'));
    tablo.Add(AddRow(DateToStr(now),inttostr(sutunsayisi),'right'));
    tablo.Add(AddRow(StaticToDynArray(aBasliklar)));

    l2:=aView.Items.Count;

    for x := 0 to l2 - 1 do
    begin
      xdegerler:=ParseIt(aView.Items[x]);

      tablo.Add(AddRow(xdegerler));
    end;

    tablo.Add('</table>');
    tablo.Add('</body>');
    tablo.Add('</html>');

    tablo.SaveToFile(SaveToExcel.FileName,TEncoding.UTF8);
    tablo.Clear;
    tablo.Free;
    shellexecute(0,'open', Pchar(SaveToExcel.FileName),'','',SW_SHOW);
    
  end;


end;



kullanımı:

Kod: Tümünü seç

MainForm.ExportToExcel(lvKisi, 'Kişi Listesi', 
   ['%Caption%', '%SubItems%0', '%SubItems%1', '%SubItems%2', '%SubItems%3'], 
   ['Sıra No', 'TC Kimlik No', 'Adı', 'Soyadı','Doğum Tarihi']
   );
bazen yükselmek için önce dibi görmek gerekir...

forumda soru sormadan önce bakılmalı bence
daha fazlası için...

yürümeyi öğrenmeden koşmaya çalışanlar için, tökezleyip düşmek kaçınılmazdır...

Resim
Kullanıcı avatarı
hido
Üye
Mesajlar: 268
Kayıt: 29 Mar 2014 04:32

Re: ListViewden excel çıktı

Mesaj gönderen hido »

unicorn64 ilgine teşekkürler...


Yıllar önceleri bu kodarla databaseden verileri alabiliyordum işimide dörüyordu, şimdi ListView den almam gerek kodlamada düzelmeleri yapamadım bu konuda yardımcı olabicek arkadaş varsa çok sevinirim...

Kod: Tümünü seç

procedure TForm6.SpeedButton1Click(Sender: TObject);
var
  Excel, K: VAriant; // Excel sayfa çalışma sayfası
  Sutun, Satir, Kolon: integer;
begin
  if Form1.ListView1.Items.Count > 0 then
  begin
     try
      Excel := CreateOleObject('Excel.application'); // Exceli Oluştur
      Excel.WorkBooks.Add; // Yeni Çalışma kitapı oluştur
      K := Excel.Activeworkbook.Worksheets[1].Cells[1];
      Excel.WorkBooks[1].Worksheets[1].Name := 'Liste'; // Sayfa Adı
      begin
        for Kolon := 0 to 20 do
        begin
          K.Cells[6, Kolon + 1].Font.Color := ClBlack; // Satır Yazı Rengi
          K.Cells[6, Kolon + 1].Font.FontStyle := 'Bold'; // Satır Yazı Kalın
        end;
      end;
      begin
        K.Cells[6, 1] := Form1.ListView1.Columns[0].Caption; // Satır Başlıkları
        K.Cells[6, 2] := Form1.ListView1.Columns[1].Caption;
        K.Cells[6, 3] := Form1.ListView1.Columns[2].Caption;
        K.Cells[6, 4] := Form1.ListView1.Columns[3].Caption;
        K.Cells[6, 5] := Form1.ListView1.Columns[4].Caption;
        K.Cells[6, 6] := Form1.ListView1.Columns[5].Caption;
        K.Cells[6, 7] := Form1.ListView1.Columns[6].Caption;
        K.Cells[6, 8] := Form1.ListView1.Columns[7].Caption;
      end;
      Sutun := 0;
      Satir := 5; // Satırdan Başla
      while Not Form1.list.Eof do // Kayıtları Gönder
      begin
        Sutun := Sutun + 1;
        Satir := Satir + 2;
        begin
          K.Cells[1 + Satir, Sutun + 0] := Form1.list.Fields[0].Text;
          K.Cells[1 + Satir, Sutun + 1] := Form1.list.Fields[1].Text;
          K.Cells[1 + Satir, Sutun + 2] := Form1.list.Fields[2].Text;
          K.Cells[1 + Satir, Sutun + 3] := Form1.list.Fields[3].Text;
          K.Cells[1 + Satir, Sutun + 4] := Form1.list.Fields[4].Text;
          K.Cells[1 + Satir, Sutun + 5] := Form1.list.Fields[5].Text;
          K.Cells[1 + Satir, Sutun + 6] := Form1.list.Fields[6].Text;
          Form1.list.Next;
          Sutun := 0;
        end;
      end;
      Excel.Cells.HorizontalAlignment := -4131; // Sola Yasla
      Excel.ActiveSheet.PageSetup.TopMargin := 10; // Kenar Boşluğu
      Excel.ActiveSheet.PageSetup.LeftMargin := 5;
      Excel.ActiveSheet.PageSetup.RightMargin := -5;
      Excel.Cells.Font.Size := 12; // Yazı Büyüklüğü

      K.Cells[2, 2].Value := FormatDateTime('d mmmm yyyy dddd hh:nn:ss', Now);   // Tarih
      K.Cells[1].Range['A2', 'C2'].MergeCells := True; // Hücreleri Birleştir

      // K.Cells[3, 2].Value := DateToStr(DateTimePicker1.Date) + ' listem';
      K.Cells[1].Range['A3', 'C3'].MergeCells := True;

      // K.Cells[4, 2].Value := User.Caption;
      K.Cells[1].Range['A4', 'C4'].MergeCells := True;

      Excel.Activeworkbook.Worksheets[1].Columns.Autofit;
      Excel.Columns[1].Columns.Autofit;
      Excel.Columns[2].Columns.Autofit;
      Excel.Columns[3].ColumnWidth := 40;
      Excel.Columns[4].Columns.Autofit;
      Excel.Columns[5].Columns.Autofit;
      Excel.Columns[6].Columns.Autofit;
      Excel.Columns[7].Columns.Autofit;
      Excel.Columns[8].Columns.Autofit;

      Excel.Activeworkbook.SaveAs('C:\Users\Hido\Desktop\' + FormatDateTime('dd.mm.yyyy   hh.nn.ss', Now) + ' Liste' + '.xls');
      Excel.Visible := True; // Excel Göster
      Form1.list.Close;
    except
      Abort;
    end;
  end;
end;
Cevapla