Witam napisałem mały kodzik wrzucający zawartość SQLDataSet-a do exela wszystko było by fajnie tylko nie mogę sobie poradzić jak wstawić nazwy kolum w pierwszym rekordzie.
procedure TForm1.sButton1Click(Sender: TObject);
var
xl, sht, wb : Variant;
row,i,j : integer;
s : string;
begin
if not SQLDataSet1.Active then begin
MessageDlg('Dataset not opened.', mtWarning, [mbOK], 0);
Exit;
end;
try
xl := CreateOLEObject('Excel.Application');
wb := xl.WorkBooks.Add;
sht := wb.Sheets.Add;
xl.Visible := true;
xl.DisplayAlerts := true;
// Kolumna A: Idx=1 ,B: Idx=2 itd...
// Wiersz 1: Idx=1, 2: Idx=2 itd...
SQLDataSet1.First; // sDataset - sqldataset
for i := 0 to SQLDataSet1.FieldCount-1 do sht.Cells[1,i+1].Value := SQLDataset1.Fields[i].Name;
for i := 1 to SQLDataSet1.RecordCount do begin
for j := 1 to SQLDataSet1.FieldCount do begin
//sht.Cells[row,col].Value := i; // kolumna A
sht.Cells[i,j].Value := SQLDataSet1.Fields[j-1].AsString;
sht.Cells[i+1,j].Value := SQLDataset1.Fields[j-1].AsString; // offset o +jeden wiersz
end;
SQLDataSet1.Next;
end;
sht.Columns['A:CV'].EntireColumn.AutoFit; // autofit 100 kolumn
sht.Range['A1:CV1'].Font.Bold:=True;
sht.Range['A1:CV1'].Borders.LineStyle :=12; //w zakresie komórek A1:CV1 zmienia styl ramki
sht.Range['A2:CV2'+inttostr(SQLDataSet1.RecordCount-1)].Borders.LineStyle :=1; // obramowanie
xl := Unassigned;
except
ShowMessage('W Twoim komputerze nie jest zainstalowany MS Excel lub aplikacja ta jest uszkodzona albo też została przerwana praca aplikacji !');
end;
end;