exceli stringgride aktarma ve ekstra satır ekleme

Delphi'de kod yazma ile ilgili sorularınızı bu foruma yazabilirsiniz.
Cevapla
kazimates
Üye
Mesajlar: 332
Kayıt: 01 Tem 2005 12:40
Konum: Kıbrıs
İletişim:

exceli stringgride aktarma ve ekstra satır ekleme

Mesaj gönderen kazimates »

merhaba aşağıda ki kodları kullanarak once bir excel dosyasını stringgride aktarıyorum. burada sorun yok. fakat ornek olarak okudugum excelde startdate ve enddate var burada 2 tarıh aralıgı var ornek 02.01.2011 ve enddate ıse 13.01.2011 buraya geldıgımde aynısını sg2 ye aktarmak degılde fromdate ve enddate 2.1.2011 ve sonraki satırda takı 2 den 13.1 e kadar gunlerı bırer bırer artırıp gıtmesını ıstıyorum. ama olmuyor.
ornek excelde startdate enddate
1/1/2011 3/1/2011 ıse
sg2 ye
startdate enddate
1/1/2011 1/1/2011
2/1/2011 2/1/2011
3/1/2011 3/1/2011
diye eklemelı ve sonra exceldekı sg1 ın sonrakı satırına gecıp oradakı tarıhlerı almalı ve onlarıda bu sekılde yapmalıdır. bunu hangı algorıtmada kı yanlısdan dolayı yapamıyorum lutfen kodları bır kontrol edebılırmısınız.

Kod: Tümünü seç

unit MainUnit;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    StringGrid1: TStringGrid;
    sg2: TStringGrid;
   // procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure doldur(currentsatir1,sonsatir1: integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses
  ComObj;

function Xls_To_StringGrid(AGrid: TStringGrid; AXLSFile: string): Boolean;
const
  xlCellTypeLastCell = $0000000B;
var
  XLApp, Sheet: OLEVariant;
  RangeMatrix: Variant;
  x, y, k, r: Integer;
begin
  Result := False;
  // Create Excel-OLE Object
  XLApp := CreateOleObject('Excel.Application');
  try
    // Hide Excel
    XLApp.Visible := False;

    // Open the Workbook
   // XLApp.Workbooks.Open('C:\jewelsofthemed\lionweb.xls');
    XLApp.Workbooks.Open(AXLSFile);
    // Sheet := XLApp.Workbooks[1].WorkSheets[1];
    Sheet := XLApp.Workbooks[ExtractFileName(AXLSFile)].WorkSheets[1];

    // In order to know the dimension of the WorkSheet, i.e the number of rows
    // and the number of columns, we activate the last non-empty cell of it

    Sheet.Cells.SpecialCells(xlCellTypeLastCell, EmptyParam).Activate;
    // Get the value of the last row
    x := XLApp.ActiveCell.Row;
    // Get the value of the last column
    y := XLApp.ActiveCell.Column;

    // Set Stringgrid's row &col dimensions.

    AGrid.RowCount := x;
    AGrid.ColCount := y;

    // Assign the Variant associated with the WorkSheet to the Delphi Variant

    RangeMatrix := XLApp.Range['A1', XLApp.Cells.Item[X, Y]].Value;
    //  Define the loop for filling in the TStringGrid
    k := 1;
    repeat
      for r := 1 to y do
        AGrid.Cells[(r - 1), (k - 1)] := RangeMatrix[K, R];
      Inc(k, 1);
      AGrid.RowCount := k + 1;
    until k > x;
    // Unassign the Delphi Variant Matrix
    RangeMatrix := Unassigned;

  finally
    // Quit Excel
    if not VarIsEmpty(XLApp) then
    begin
      // XLApp.DisplayAlerts := False;
      XLApp.Quit;
      XLAPP := Unassigned;
      Sheet := Unassigned;
      Result := True;
    end;
  end;
end;

procedure TForm1.doldur(currentsatir1: Integer; sonsatir1: Integer);
var
   satirsayac, satirsayac2 : integer;
   startdate, enddate: tdatetime;
   fromprice, nowprice, hoadult, hochd1, hochd2: string;
   boardtype, roomtype: string;
begin
    satirsayac:=currentsatir1;
    showmessage(inttostr(satirsayac));

           boardtype:= (stringgrid1.cells[0,satirsayac]);
           startdate:= strtodate(stringgrid1.Cells[1,satirsayac]);
           enddate:= strtodate(stringgrid1.Cells[2,satirsayac]);
           fromprice:= (stringgrid1.Cells[3,satirsayac]);
           nowprice:= (stringgrid1.Cells[4,satirsayac]);
           roomtype:= stringgrid1.Cells[5,satirsayac];
           hoadult:=(stringgrid1.Cells[6,satirsayac]);
           hochd1:=(stringgrid1.Cells[7,satirsayac]);
           hochd2:=(stringgrid1.Cells[8,satirsayac]);

           while startdate<= enddate do
           begin
             sg2.Cells[0,satirsayac2]:=boardtype;
             sg2.Cells[1,satirsayac2]:=datetostr(startdate);
             sg2.Cells[2,satirsayac2]:=datetostr(startdate);
             sg2.Cells[3,satirsayac2]:=(fromprice);
             sg2.Cells[4,satirsayac2]:=(nowprice);
             sg2.Cells[5,satirsayac2]:=roomtype;
             sg2.Cells[6,satirsayac2]:=(hoadult);
             sg2.Cells[7,satirsayac2]:=(hochd1);
             sg2.Cells[8,satirsayac2]:=(hochd2);
             sg2.RowCount:=sg2.RowCount+1;
             startdate:=startdate+1;
             satirsayac2:=sg2.RowCount;
           end;

end;

procedure TForm1.Button1Click(Sender: TObject);
var
   currentsatir1, toplamsatir1, sonsatir1: integer;
begin
   currentsatir1:=1;
   sg2.Rowcount:=1;
   sg2.ColCount:=stringgrid1.ColCount;
  if Xls_To_StringGrid(StringGrid1, 'C:\jewelsofthemed\lionweb.xls') then
  begin
    sonsatir1:=StringGrid1.RowCount;

    while currentsatir1<=sonsatir1 do
    begin
       doldur(currentsatir1,sonsatir1);
       currentsatir1:=currentsatir1+1;
    end;
  end;

end;

end.
kazimates
Üye
Mesajlar: 332
Kayıt: 01 Tem 2005 12:40
Konum: Kıbrıs
İletişim:

Re: exceli stringgride aktarma ve ekstra satır ekleme

Mesaj gönderen kazimates »

sorunun cevabi asagidaki gibi kodlar duzenlenecek olursa cozum oluyor. bilginize...

Kod: Tümünü seç

type
  TForm1 = class(TForm)
    Button1: TButton;
    StringGrid1: TStringGrid;
    sg2: TStringGrid;
   // procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure doldur(currentsatir1: integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses
  ComObj;

function Xls_To_StringGrid(AGrid: TStringGrid; AXLSFile: string): Boolean;
const
  xlCellTypeLastCell = $0000000B;
var
  XLApp, Sheet: OLEVariant;
  RangeMatrix: Variant;
  x, y, k, r: Integer;
begin
  Result := False;
  // Create Excel-OLE Object
  XLApp := CreateOleObject('Excel.Application');
  try
    // Hide Excel
    XLApp.Visible := False;

    // Open the Workbook
   // XLApp.Workbooks.Open('C:\jewelsofthemed\lionweb.xls');
    XLApp.Workbooks.Open(AXLSFile);
    // Sheet := XLApp.Workbooks[1].WorkSheets[1];
    Sheet := XLApp.Workbooks[ExtractFileName(AXLSFile)].WorkSheets[1];

    // In order to know the dimension of the WorkSheet, i.e the number of rows
    // and the number of columns, we activate the last non-empty cell of it

    Sheet.Cells.SpecialCells(xlCellTypeLastCell, EmptyParam).Activate;
    // Get the value of the last row
    x := XLApp.ActiveCell.Row;
    // Get the value of the last column
    y := XLApp.ActiveCell.Column;

    // Set Stringgrid's row &col dimensions.

    AGrid.RowCount := x;
    AGrid.ColCount := y;

    // Assign the Variant associated with the WorkSheet to the Delphi Variant

    RangeMatrix := XLApp.Range['A1', XLApp.Cells.Item[X, Y]].Value;
    //  Define the loop for filling in the TStringGrid
    k := 1;
    repeat
      for r := 1 to y do
        AGrid.Cells[(r - 1), (k - 1)] := RangeMatrix[K, R];
      Inc(k, 1);
      AGrid.RowCount := k + 1;
    until k > x;
    // Unassign the Delphi Variant Matrix
    RangeMatrix := Unassigned;

  finally
    // Quit Excel
    if not VarIsEmpty(XLApp) then
    begin
      // XLApp.DisplayAlerts := False;
      XLApp.Quit;
      XLAPP := Unassigned;
      Sheet := Unassigned;
      Result := True;
    end;
  end;
end;

procedure TForm1.doldur(currentsatir1: Integer);
var
   satirsayac, satirsayac2 : integer;
   startdate, enddate: tdatetime;
   fromprice, nowprice, hoadult, hochd1, hochd2: string;
   boardtype, roomtype: string;
begin
    satirsayac:=currentsatir1;
    startdate:= strtodate(stringgrid1.Cells[1,satirsayac]);
    enddate:= strtodate(stringgrid1.Cells[2,satirsayac]);

    while startdate<= enddate do
    begin
           satirsayac2:=sg2.RowCount;
           boardtype:= (stringgrid1.cells[0,satirsayac]);
           fromprice:= (stringgrid1.Cells[3,satirsayac]);
           nowprice:= (stringgrid1.Cells[4,satirsayac]);
           roomtype:= (stringgrid1.Cells[5,satirsayac]);
           hoadult:=(stringgrid1.Cells[6,satirsayac]);
           hochd1:=(stringgrid1.Cells[7,satirsayac]);
           hochd2:=(stringgrid1.Cells[8,satirsayac]);

           sg2.Cells[0,satirsayac2]:=boardtype;
           sg2.Cells[1,satirsayac2]:=datetostr(startdate);
           sg2.Cells[2,satirsayac2]:=datetostr(startdate);
           sg2.Cells[3,satirsayac2]:=(fromprice);
           sg2.Cells[4,satirsayac2]:=(nowprice);
           sg2.Cells[5,satirsayac2]:=roomtype;
           sg2.Cells[6,satirsayac2]:=(hoadult);
           sg2.Cells[7,satirsayac2]:=(hochd1);
           sg2.Cells[8,satirsayac2]:=(hochd2);

           startdate:=startdate+1;
           satirsayac2:=sg2.RowCount;
           sg2.RowCount:=sg2.RowCount+1;

    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
   currentsatir1, toplamsatir1, sonsatir1: integer;
begin
   currentsatir1:=1;

  if Xls_To_StringGrid(StringGrid1, 'C:\jewelsofthemed\lionweb.xls') then
  begin

   sg2.ColCount:=stringgrid1.ColCount;
    sonsatir1:=StringGrid1.RowCount-3;

    while currentsatir1<=sonsatir1 do
    begin
       doldur(currentsatir1);
       currentsatir1:=currentsatir1+1;
    end;
  end;

end;

end.
Cevapla