1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| var
ExcelApp : OleVariant;
ModWin : OleVariant; // objets window du modèle et des documents
ModBase : OleVariant; // objet range de base pour parcourir modéle et document en parallèle
RowOffset : Integer;
Data : array of string;
MaxRows,MaxCol : Integer;
begin
ExcelApp := TExcelApp.Create;
try
ExcelApp.Workbooks.Open(FileName := aFileName);
ExcelApp.WindowState := xlMaximized;
ExcelApp.Visible := TRUE;
ExcelApp.Interactive := FALSE;
ModWin := ExcelApp.ActiveWindow;
ModBase := ModWin.ActiveSheet.Range['A1','A1'];
MaxRows := ModWin.ActiveCell.SpecialCells(xlLastCell).Row;
setlength(Data,MaxRows,MaxCol);
for RowOffset := 0 To MaxRows do
begin
// on recupere les info du fichier ecxel
For ColOffset := 0 To MaxCol do
Data[RowOffset,ColOffset] := ModBase.Offset[RowOffset,ColOffset].Value;
// ici on insert dans ta table les elements que tu a besoin
stQuery :=' insert into (VAL1,VAL2) VALUES ("%s",%s)';
query.close;
query.sql.clear;
query.sql.add(Format(stQuery,[DATA[0],DATA[2]));
query.ExecSql;
ModBase.Offset[RowOffset,0].Select;
ExcelApp.Selection.HorizontalAlignment := xlCenter;
ExcelApp.Selection.Font.Bold := TRUE;
ExcelApp.Selection.Interior.ColorIndex := 34; // bleu pâle
ExcelApp.Selection.Interior.Pattern := xlSolid;
end;
finally
ExcelApp.Interactive := TRUE;
ExcelApp.Workbooks.Close;
ExcelApp.Quit;
end; |
Partager