Imam jednu adotable. Kako da (npr klikom na dugme) exportujem podatke iz te tabele u npr fajl *.xls?
export podataka iz tabele
Zaključano
Zakačeno
Ja to radim pomoću TADOQuery, tako što napravim HTML tabelu, koju zatim sačuvam kao *.xls fajl, koji funkcioniše kao normalna tabela kada se otvori u Excel-u.
Code:
procedure TForm1.Button4Click(Sender: TObject);
var
html: widestring;
excelfile: TextFile;
i: integer;
begin
SaveDialog1.InitialDir := ExtractFilePath(Application.ExeName);
SaveDialog1.Filter := 'Microsoft Excel (*.xls)|*.xls';
SaveDialog1.FileName := 'katalog.xls';
SaveDialog1.DefaultExt := '*.xls';
SaveDialog1.Execute;
.
.
.
ADOQuery3.SQL.Text := 'SELECT * FROM filmovi';
ADOQuery3.Open;
html:= html + '<table border="1" width="100%">' + #13#10;
html:= html + '<tr><td colspan="5" align="center"><i>Katalog Filmova</i></td></tr>' + #13#10;
html:= html + '<tr>' + #13#10;
html:= html + '<td>' + '<b>' + 'ID broj filma' + '</b>' + '</td>';
html:= html + '<td>' + '<b>' + 'Naslov filma' + '</b>' + '</td>';
html:= html + '<td>' + '<b>' + 'Zanr' + '</b>' + '</td>';
html:= html + '<td>' + '<b>' + 'Distributer' + '</b>' + '</td>';
html:= html + '<td>' + '<b>' + 'Tip medijuma' + '</b>' + '</td>' + #13#10;
html:= html + '</tr>' + #13#10;
while not AdoQuery3.Eof do
begin
html:= html + '<tr>' + #13#10;
for i:=0 to AdoQuery3.FieldCount-1 do
begin
html:= html + '<td>';
html:= html + ADOQuery3.Fields[i].AsString;
html:= html + '</td>' + #13#10;
end;
html:= html + '</tr>' + #13#10;
ADOQuery3.Next;
end;
html:= html + '</table>';
Memo1.Text := html;
AssignFile(excelfile, SaveDialog1.FileName);
Rewrite(excelfile);
WriteLn(excelfile, html);
CloseFile(excelfile);
ShowMessage('Fajl je uspesno exportovan!');
end;
end;
procedure TForm1.Button4Click(Sender: TObject);
var
html: widestring;
excelfile: TextFile;
i: integer;
begin
SaveDialog1.InitialDir := ExtractFilePath(Application.ExeName);
SaveDialog1.Filter := 'Microsoft Excel (*.xls)|*.xls';
SaveDialog1.FileName := 'katalog.xls';
SaveDialog1.DefaultExt := '*.xls';
SaveDialog1.Execute;
.
.
.
ADOQuery3.SQL.Text := 'SELECT * FROM filmovi';
ADOQuery3.Open;
html:= html + '<table border="1" width="100%">' + #13#10;
html:= html + '<tr><td colspan="5" align="center"><i>Katalog Filmova</i></td></tr>' + #13#10;
html:= html + '<tr>' + #13#10;
html:= html + '<td>' + '<b>' + 'ID broj filma' + '</b>' + '</td>';
html:= html + '<td>' + '<b>' + 'Naslov filma' + '</b>' + '</td>';
html:= html + '<td>' + '<b>' + 'Zanr' + '</b>' + '</td>';
html:= html + '<td>' + '<b>' + 'Distributer' + '</b>' + '</td>';
html:= html + '<td>' + '<b>' + 'Tip medijuma' + '</b>' + '</td>' + #13#10;
html:= html + '</tr>' + #13#10;
while not AdoQuery3.Eof do
begin
html:= html + '<tr>' + #13#10;
for i:=0 to AdoQuery3.FieldCount-1 do
begin
html:= html + '<td>';
html:= html + ADOQuery3.Fields[i].AsString;
html:= html + '</td>' + #13#10;
end;
html:= html + '</tr>' + #13#10;
ADOQuery3.Next;
end;
html:= html + '</table>';
Memo1.Text := html;
AssignFile(excelfile, SaveDialog1.FileName);
Rewrite(excelfile);
WriteLn(excelfile, html);
CloseFile(excelfile);
ShowMessage('Fajl je uspesno exportovan!');
end;
end;
Ili downloaduj OfficePartner komponentu sa http://sourceforge.net/projects/tpofficepartner/
Meni se svidja, prilicno je mocna, mozes da uradis sve sto ti treba.
Evo kako bi otprilike trebao da izgleda source :
Meni se svidja, prilicno je mocna, mozes da uradis sve sto ti treba.
Evo kako bi otprilike trebao da izgleda source :
Code:
function TMembersListExportWindow.SaveList(const Path : String) : String;
const
TNam : Array[1..10] of String = ('IDNum',
'FirstName',
'LastName',
'Address',
'BirthPlace',
'BirthDate',
'PhoneNumber',
'PaperNum',
'JoinDate',
'Status');
TNamTr : Array[1..10] of String = ('Broj',
'Ime',
'Prezime',
'Adresa',
'Mesto Rodjenja',
'Datum Rodjenja',
'Broj Telefona',
'Broj Licne Karte',
'Datum Uclanjenja',
'Status');
ColWidths : Array[1..10] of Integer = (8, 22, 25, 30, 22, 21, 25, 22, 15, 15);
var
CTNum : Byte;
SelString : String;
SortErr : Boolean;
C1 : Integer;
begin
result := 'Lista uspesno snimljena !';
CTNum := 65;
SortErr := FALSE;
SelString := 'SELECT ';
For C1 := 1 to 10 Do
If TCheckBox(FindComponent('CheckBox' + IntToStr(C1))).Checked Then
SelString := SelString + TNam[C1] + ',';
If SelString = 'SELECT ' Then
Begin
result := 'Morate selektovati bar jedan podatak koji ce se nalaziti u listi !';
Exit;
End;
SelString := Copy(SelString, 1, Length(SelString) - 1) + ' FROM bases\Members.db ORDER BY ';
For C1 := 1 to 10 Do
If TRadioButton(FindComponent('RadioButton' + IntToStr(C1))).Checked Then
If TCheckBox(FindComponent('CheckBox' + IntToStr(C1))).Checked Then
SelString := SelString + TNam[C1]
else
SortErr := TRUE;
If SortErr Then
Begin
result := 'Lista mora da bude sortirana po polju koje ce se nalaziti u listi !';
Exit;
End;
OpExcel1.Connected := TRUE;
OpExcel1.Workbooks[0].Worksheets[0].Name := 'Lista Clanova';
Query1.Close;
Query1.SQL.Clear;
Query1.SQL.Add(SelString);
Query1.Open;
With OpExcel1.Workbooks[0].Worksheets[0].Ranges[0] Do
Begin
Address := 'A1';
OfficeModel := OpDataSetModel1;
Populate;
End;
For C1 := 1 to 10 Do
If TCheckBox(FindComponent('CheckBox' + IntToStr(C1))).Checked Then
With OpExcel1.Workbooks[0].Worksheets[0].Ranges[C1 - 1] Do
Begin
Address := Chr(CTNum) + '1';
SimpleText := TNamTr[C1];
Color := clSkyBlue;
Borders := [xlbLeft,xlbRight,xlbTop,xlbBottom];
BorderLineWeight := xlbwThin;
ColumnWidth := ColWidths[C1];
FontAttributes := [xlfaBold];
HorizontalAlignment := xlchaCenter;
RowHeight := 23;
VerticalAlignment := xlcvaCenter;
Inc(CTNum);
End;
OpExcel1.Workbooks[0].SaveAs(Path);
OpExcel1.Connected := FALSE;
end;
function TMembersListExportWindow.SaveList(const Path : String) : String;
const
TNam : Array[1..10] of String = ('IDNum',
'FirstName',
'LastName',
'Address',
'BirthPlace',
'BirthDate',
'PhoneNumber',
'PaperNum',
'JoinDate',
'Status');
TNamTr : Array[1..10] of String = ('Broj',
'Ime',
'Prezime',
'Adresa',
'Mesto Rodjenja',
'Datum Rodjenja',
'Broj Telefona',
'Broj Licne Karte',
'Datum Uclanjenja',
'Status');
ColWidths : Array[1..10] of Integer = (8, 22, 25, 30, 22, 21, 25, 22, 15, 15);
var
CTNum : Byte;
SelString : String;
SortErr : Boolean;
C1 : Integer;
begin
result := 'Lista uspesno snimljena !';
CTNum := 65;
SortErr := FALSE;
SelString := 'SELECT ';
For C1 := 1 to 10 Do
If TCheckBox(FindComponent('CheckBox' + IntToStr(C1))).Checked Then
SelString := SelString + TNam[C1] + ',';
If SelString = 'SELECT ' Then
Begin
result := 'Morate selektovati bar jedan podatak koji ce se nalaziti u listi !';
Exit;
End;
SelString := Copy(SelString, 1, Length(SelString) - 1) + ' FROM bases\Members.db ORDER BY ';
For C1 := 1 to 10 Do
If TRadioButton(FindComponent('RadioButton' + IntToStr(C1))).Checked Then
If TCheckBox(FindComponent('CheckBox' + IntToStr(C1))).Checked Then
SelString := SelString + TNam[C1]
else
SortErr := TRUE;
If SortErr Then
Begin
result := 'Lista mora da bude sortirana po polju koje ce se nalaziti u listi !';
Exit;
End;
OpExcel1.Connected := TRUE;
OpExcel1.Workbooks[0].Worksheets[0].Name := 'Lista Clanova';
Query1.Close;
Query1.SQL.Clear;
Query1.SQL.Add(SelString);
Query1.Open;
With OpExcel1.Workbooks[0].Worksheets[0].Ranges[0] Do
Begin
Address := 'A1';
OfficeModel := OpDataSetModel1;
Populate;
End;
For C1 := 1 to 10 Do
If TCheckBox(FindComponent('CheckBox' + IntToStr(C1))).Checked Then
With OpExcel1.Workbooks[0].Worksheets[0].Ranges[C1 - 1] Do
Begin
Address := Chr(CTNum) + '1';
SimpleText := TNamTr[C1];
Color := clSkyBlue;
Borders := [xlbLeft,xlbRight,xlbTop,xlbBottom];
BorderLineWeight := xlbwThin;
ColumnWidth := ColWidths[C1];
FontAttributes := [xlfaBold];
HorizontalAlignment := xlchaCenter;
RowHeight := 23;
VerticalAlignment := xlcvaCenter;
Inc(CTNum);
End;
OpExcel1.Workbooks[0].SaveAs(Path);
OpExcel1.Connected := FALSE;
end;
Bio sam ubedjen da postoji jednostavnija varijanta :)