Enregistrer un StringGrid au format excel

function SaveAsExcelFile(StringGrid: TStringGrid; FileName: string): boolean;
const
xlWBATWorksheet = -4167;
var
Row, Col: integer;
GridPrevFile: string;
XLApp, Sheet: OLEVariant;
begin
Result := false;
XLApp := CreateOleObject('Excel.Application');
try
XLApp.Visible := False;
XLApp.Workbooks.Add(xlWBatWorkSheet);
Sheet := XLApp.Workbooks[1].WorkSheets[1];
Sheet.Name := 'My Sheet Name';
for col := 0 to StringGrid.ColCount - 1 do
for row := 0 to StringGrid.RowCount - 1 do
Sheet.Cells[row + 1,col + 1] := StringGrid.Cells[col, row];
try
XLApp.Workbooks[1].SaveAs(FileName);
Result := True;
except
// Error ?
end;
finally
if not VarIsEmpty(XLApp) then
begin
XLApp.DisplayAlerts := False;
XLApp.Quit;
XLAPP := Unassigned;
Sheet := Unassigned;
end;
end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
If SaveAsExcelFile(StringGrid1,'c:MyExcelFile.xls') then
ShowMessage('StringGrid saved!');
end;

Ajouter un commentaire
Pseudo
Adresse email
Site internet (optionnel)
Votre commentaire
Fiche de l'article
Mise a jour22/11/2001
VisualisationVu 2036 fois
PublicInternaute zz
CategorieDelphi - Trucs et astuces - Objet
Auteur de l'article
SwissDelphiCenter SwissDelphiCenter
connecté le 02/11/2011
329 articles dans la section
Contacter l'auteur