delphi4arab منتديات دلفي للعرب

نسخة كاملة : ربط الإكسل مع الديبقرايد
أنت حالياً تتصفح نسخة خفيفة من المنتدى . مشاهدة نسخة كاملة مع جميع الأشكال الجمالية .
السلام عليكم
المشكلة تتمثل فيما يلي حيث لا يتم ظهور جداول الأكسل على الديبقرايد رغم أن الإتصال سليم
أرجو مراجعة المحتوي
وعليكم السلام ورحمة الله وبركاته ،
لكن ملف الإكسل غير مرفق ؟
أرجو ارفاقه حتى يتم التجربة.
السلام عليكم جميعا
شكرا علي الرد اخي ابو معاذ
اما بخصوص ملف الاكسل فلا يوجد فالنموذج يفتح اي ملف اكسل بدون قيود فالملف في الغالبية عادي ككل الملفات
كود :
وجدت هذه الذالة و هي تعمل بشكل رائع و تفي بالغرض

كود :
import an Excel Table to a TStringgrid?

كود :
Autor: Thomas Stutz

كود :
[ Print tip ]

كود :
Tip Rating (196):

كود :
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(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.Button1Click(Sender: TObject);

كود :
begin

كود :
 if Xls_To_StringGrid(StringGrid1, 'C:\Table1.xls') then

كود :
   ShowMessage('Table has been exported!');

كود :
end;


وجدت هذه الذالة و هي تعمل بشكل رائع و تفي بالغرض

import an Excel Table to a TStringgrid?


Autor: Thomas Stutz

[ Print tip ]

Tip Rating (196):

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(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.Button1Click(Sender: TObject);

begin

if Xls_To_StringGrid(StringGrid1, 'C:\Table1.xls') then

  ShowMessage('Table has been exported!');

end;