View Full Version : آموزش: خواندن رکوردهای خاص از اکسل
Quantum3629
شنبه 16 اسفند 1393, 06:57 صبح
با سلام
من یک فایل اکسل دارم که باید با دلفی فراخوانی اش کنم و اطلاعاتش رو در یک گرید نشون بدم . البته اینکار رو با ADO انجام میدم و ارتباط دارم . اما مشکلی که هست اینه که من از رکورد یا همون ردیف n ام به بعد اطلاعات در یکی از شیت های اکسل رو میخوام ولی موقع فراخوانی یک شیت ، کل شیت رو میاره. حالا میخواستم بپرستم چطور میتونم این کار رو انجامش بدم
با تشکر
دلفــي
شنبه 16 اسفند 1393, 12:12 عصر
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
end;
Quantum3629
یک شنبه 17 اسفند 1393, 08:57 صبح
سلام دوست عزیز
ممنون از راهنمایی تون .
یه سئوال دیگه ای دارم و اینه که میشه کاری کنیم که اون چند تا سطر توی اکسل رو که میخواییم حذف کنیم بعد ذخیره اش کنیم.چون باید دوباره اون اطلاعات رو فراخوانیشون کنم و تو بانک ذخیره کنم؟؟؟
hadisalahi2
دوشنبه 18 اسفند 1393, 17:02 عصر
کامپوننت های Ems Advance Import بهترین گزینه برای این کار هستن
vBulletin® v4.2.5, Copyright ©2000-1404, Jelsoft Enterprises Ltd.