Arsip: tanya cara Export Delphi ke XLS
more 19 years ago
yehez_kiel
:) temen2 saya mo nanya ada komponen yang gratisan ato script yang bisa di pakai buat export dari dbgrid ato table ke xls.... tolong donk...beri saya pencerahan terimakasih
more 19 years ago
Starboard
kalo aku pake MxNativeExcel dari mas MaxComponents.net kalo mau/males nyari di google imel ajah ke aku.. wehehehhe
wehehheh... kasih contoh cuga ahhhh :D
mxNativeExcel1.Column := 1;
mxNativeExcel1.WriteLabel('MSISDN');
mxNativeExcel1.Column := 2;
mxNativeExcel1.WriteLabel('Sender');
mxNativeExcel1.Column := 3;
mxNativeExcel1.WriteLabel('Tanggal');
mxNativeExcel1.Column := 4;
mxNativeExcel1.WriteLabel('Pesan');
with mxNativeExcel1 do
begin
Shading := False;
ActiveFont := 0;
Borders := [ebTop, ebBottom, ebLeft, ebRight];
end;
i := 3;
while not ZQuery1.Eof do
begin
inc(i);
mxNativeExcel1.Row := i;
mxNativeExcel1.Column := 1;
mxNativeExcel1.WriteLabel(ZQuery1.Fieldbyname('MSISDN').asstring);
mxNativeExcel1.Column := 2;
mxNativeExcel1.WriteLabel(ZQuery1.Fieldbyname('Ordered').AsString);
mxNativeExcel1.Column := 3;
mxNativeExcel1.WriteLabel(ZQuery1.Fieldbyname('Datetime1').AsString);
mxNativeExcel1.Column := 4;
mxNativeExcel1.WriteLabel(ZQuery1.Fieldbyname('Pesan').AsString);
ZQuery1.Next;
end;
mxNativeExcel1.CloseFile;
mxNativeExcel1.SaveToFile;
more 19 years ago
Sutilkon
Kalau aku pakai spti ini :
var Wb : OleVariant;
Var i, baris : integer ;
begin
// Data yg akan diproses,
sp1.Close;
sp1.Parameters.ParamValues['@date1'] := Tgl1.date;
sp1.Open;
wb:=CreateOleObject('Excel.Application');
wb.workbooks.add();
For i:=0 to sp1.Fields.count-1 do
begin
wb.Cells[1,i+1] := sp1.Fields[i].DisplayLabel;
end;
wb.visible:=true;
baris:=2;
With sp1 do
begin
First;
While not eof do
begin
For i:=0 to sp1.Fields.count-1 do
begin
wb.Cells[baris,i+1]:=sp1.Fields[i].AsString;
end;
inc(baris);
next;
Application.ProcessMessages;
end;
end;
thx
more 19 years ago
brodien
cobak yang ini ...tapi aku lupa jalan apa ngak ya..
{Exporting ADO tables into various formats
In this article I want to present a component I built in order to
supply exporting features to the ADOTable component. ADO supplies
an extended SQL syntax that allows exporting of data into various
formats. I took into consideration the following formats:
1)Excel
2)Html
3)Paradox
4)Dbase
5)Text
You can see all supported output formats in the registry:
"HKEY_LOCAL_MACHINE\Software\Microsoft\Jet\4.0\ISAM formats"
This is the complete source of my component }
unit ExportADOTable;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, ADODB;
type
TExportADOTable = class(TADOTable)
private
{ Private declarations }
//TADOCommand component used to execute the SQL exporting commands
FADOCommand: TADOCommand;
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
//Export procedures
//"FiledNames" is a comma separated list of the names of the fields you want to export
//"FileName" is the name of the output file (including the complete path)
//if the dataset is filtered (Filtered = true and Filter <> ''), then I append
//the filter string to the sql command in the "where" directive
//if the dataset is sorted (Sort <> '') then I append the sort string to the sql command in the
//"order by" directive
procedure ExportToExcel(FieldNames: string; FileName: string;
SheetName: string; IsamFormat: string);
procedure ExportToHtml(FieldNames: string; FileName: string);
procedure ExportToParadox(FieldNames: string; FileName: string; IsamFormat: string);
procedure ExportToDbase(FieldNames: string; FileName: string; IsamFormat: string);
procedure ExportToTxt(FieldNames: string; FileName: string);
published
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Carlo Pasolini', [TExportADOTable]);
end;
constructor TExportADOTable.Create(AOwner: TComponent);
begin
inherited;
FADOCommand := TADOCommand.Create(Self);
end;
procedure TExportADOTable.ExportToExcel(FieldNames: string; FileName: string;
SheetName: string; IsamFormat: string);
begin
{IsamFormat values
Excel 3.0
Excel 4.0
Excel 5.0
Excel 8.0
}
if not Active then
Exit;
FADOCommand.Connection := Connection;
FADOCommand.CommandText := 'Select ' + FieldNames + ' INTO ' + '[' +
SheetName + ']' + ' IN ' + '"' + FileName + '"' + '[' + IsamFormat +
';]' + ' From ' + TableName;
if Filtered and (Filter <> '') then
FADOCommand.CommandText := FADOCommand.CommandText + ' where ' + Filter;
if (Sort <> '') then
FADOCommand.CommandText := FADOCommand.CommandText + ' order by ' + Sort;
FADOCommand.Execute;
end;
procedure TExportADOTable.ExportToHtml(FieldNames: string; FileName: string);
var
IsamFormat: string;
begin
if not Active then
Exit;
IsamFormat := 'HTML Export';
FADOCommand.Connection := Connection;
FADOCommand.CommandText := 'Select ' + FieldNames + ' INTO ' + '[' +
ExtractFileName(FileName) + ']' +
' IN ' + '"' + ExtractFilePath(FileName) + '"' + '[' + IsamFormat +
';]' + ' From ' + TableName;
if Filtered and (Filter <> '') then
FADOCommand.CommandText := FADOCommand.CommandText + ' where ' + Filter;
if (Sort <> '') then
FADOCommand.CommandText := FADOCommand.CommandText + ' order by ' + Sort;
FADOCommand.Execute;
end;
procedure TExportADOTable.ExportToParadox(FieldNames: string;
FileName: string; IsamFormat: string);
begin
{IsamFormat values
Paradox 3.X
Paradox 4.X
Paradox 5.X
Paradox 7.X
}
if not Active then
Exit;
FADOCommand.Connection := Connection;
FADOCommand.CommandText := 'Select ' + FieldNames + ' INTO ' + '[' +
ExtractFileName(FileName) + ']' +
' IN ' + '"' + ExtractFilePath(FileName) + '"' + '[' + IsamFormat +
';]' + ' From ' + TableName;
if Filtered and (Filter <> '') then
FADOCommand.CommandText := FADOCommand.CommandText + ' where ' + Filter;
if (Sort <> '') then
FADOCommand.CommandText := FADOCommand.CommandText + ' order by ' + Sort;
FADOCommand.Execute;
end;
procedure TExportADOTable.ExportToDbase(FieldNames: string; FileName: string;
IsamFormat: string);
begin
{IsamFormat values
dBase III
dBase IV
dBase 5.0
}
if not Active then
Exit;
FADOCommand.Connection := Connection;
FADOCommand.CommandText := 'Select ' + FieldNames + ' INTO ' + '[' +
ExtractFileName(FileName) + ']' +
' IN ' + '"' + ExtractFilePath(FileName) + '"' + '[' + IsamFormat +
';]' + ' From ' + TableName;
if Filtered and (Filter <> '') then
FADOCommand.CommandText := FADOCommand.CommandText + ' where ' + Filter;
if (Sort <> '') then
FADOCommand.CommandText := FADOCommand.CommandText + ' order by ' + Sort;
FADOCommand.Execute;
end;
procedure TExportADOTable.ExportToTxt(FieldNames: string; FileName: string);
var
IsamFormat: string;
begin
if not Active then
Exit;
IsamFormat := 'Text';
FADOCommand.Connection := Connection;
FADOCommand.CommandText := 'Select ' + FieldNames + ' INTO ' + '[' +
ExtractFileName(FileName) + ']' +
' IN ' + '"' + ExtractFilePath(FileName) + '"' + '[' + IsamFormat +
';]' + ' From ' + TableName;
if Filtered and (Filter <> '') then
FADOCommand.CommandText := FADOCommand.CommandText + ' where ' + Filter;
if (Sort <> '') then
FADOCommand.CommandText := FADOCommand.CommandText + ' order by ' + Sort;
FADOCommand.Execute;
end;
end.
more 19 years ago
abad21
Klo ngga salah pernah dibahas kok masalah ini, baik yg pake komponen atau masukin script, cuman gw lupa thread-nya yg mana.
Mas bisa coba cari2 deh, tapi gw ada sedikit script mungkin bisa ngebantu:
procedure TfrmAuto.DataSetToExcelFile(const Dataset: TDataset; const FileNameXLS: string);
var WorkBk : _WorkBook; // Define a WorkBook
WorkSheet : _WorkSheet; // Define a WorkSheet
I, J, R, C : Integer;
IIndex : OleVariant;
TabGrid : Variant;
x: integer;
begin
ButtonEnabled(FALSE);
if not Dataset.IsEmpty then begin
IIndex := 1;
R:= Dataset.RecordCount;
C:= Dataset.FieldCount;
// Create the Variant Array
TabGrid := VarArrayCreate([0,(R - 1),0,(C - 1)],VarOleStr);
ProgressBar1.Position:= 0;
Label7.Caption:= '0/'+inttostr(Dataset.RecordCount);
I := 0;
Dataset.First;
// Define the loop for filling in the Variant
while not Dataset.EOF do begin
for J := 0 to (C - 1) do begin
TabGrid[I,J] := Dataset.Fields[J].Text;
Application.ProcessMessages;
end;
Label7.Caption:= inttostr(I+1)+'/'+inttostr(Dataset.RecordCount);
Label8.Caption:= inttostr(round(((I+1)/Dataset.RecordCount)100))+'%';
ProgressBar1.Position:= round(((I+1)/Dataset.RecordCount) 100);
Inc(I,1);
Dataset.Next;
end;
// Connect to the server TExcelApplication
ExcelApplication1.Connect;
// Add WorkBooks to the ExcelApplication
ExcelApplication1.WorkBooks.Add(xlWBatWorkSheet,0);
// Select the first WorkBook
WorkBk := ExcelApplication1.WorkBooks.Item[IIndex];
// Define the first WorkSheet
WorkSheet := WorkBk.WorkSheets.Get_Item(1) as _WorkSheet;
//Header
Worksheet.Range['A1', 'A1'].EntireRow.Font.Bold := True;
Worksheet.Range['A1', 'A1'].EntireRow.Font.Color := clBlack;
for x := 0 to Dataset.FieldCount - 1 do begin
if Dataset.Fields[x].Visible then begin
Worksheet.Cells.Item[1, x + 1].Value := Dataset.Fields[x].DisplayLabel;
Worksheet.Cells.Item[1, x + 1].ColumnWidth := Dataset.Fields[x].DisplayWidth;
end;
end;
// Assign the Delphi Variant Matrix to the Variant associated with the WorkSheet
Worksheet.Range].Value := TabGrid;
WorkSheet.Name := Edit1.Text;
try
WorkBk.SaveAs(
FileNameXLS, // Filename
XlWindowState(xlNormal), // FileFormat
EmptyParam, // Password,
EmptyParam, // WriteResPass
False, // ReadOnlyRecommended
False, // CreateBackup
xlNoChange, // AccessMode
xlUserResolution, // ConflictResolution
False, // AddToMru
EmptyParam, // TextCodepage
EmptyParam, // TextVisualLayout
0);
except on E:EOleException do begin
WorkBk.SaveAs(
FileNameXLS+'_TMP', // Filename
XlWindowState(xlNormal), // FileFormat
EmptyParam, // Password,
EmptyParam, // WriteResPass
False, // ReadOnlyRecommended
False, // CreateBackup
xlNoChange, // AccessMode
xlUserResolution, // ConflictResolution
False, // AddToMru
EmptyParam, // TextCodepage
EmptyParam, // TextVisualLayout
0);
ShowMessage(FileNameXLS+' in use by user, '+#13#10+'file name has change '+FileNameXLS+'_TMP');
end;end;
// Show Excel
ExcelApplication1.Visible[0] := True;
// Disconnect the Server
//ExcelApplication1.Quit;
ExcelApplication1.Disconnect;
// Unassign the Delphi Variant Matrix
TabGrid := Unassigned;
end else begin
Label7.Caption:= '0/0';
Label8.Caption:= '0%';
ProgressBar1.Position:= 0;
ShowMessage('Table is empty');
end;
ButtonEnabled(TRUE);
end;
more 18 years ago
wiseguy1997
Halo, para senior dan teman2 di komunitas Delphi Indonesia...
Perkenalkan aq Newbie mencoba share coding-ku yg kudapat di Internet dg sedikit modifikasi.
Tlah aq coba dg database ADO-MS Access2000/XP dan pake Delphi 6 berjalan sukses. Ni murni dg Delphi
n' tanpa install component apapun. Jg lupa tuliskan Excel2000 atu XP di uses-nya. Koment ato tanggapan di
situs ini aja biar tambah rame n pada pinter2 semua. Silahkan dicoba, smoga bermanfaat.
Uses ....., Excel2000;
Function TForm1.ExportToExcel(oDataSet : TDataSet; const sFile : String): Boolean;
var
iCol,iRow : Integer;
oExcel : TExcelApplication;
oWorkbook : TExcelWorkbook;
oSheet : TExcelWorksheet;
begin
begin
oDataSet.Close;
iCol := 0;
iRow := 1; //untuk memberi tempat bagi nama field
result := True;
oExcel := TExcelApplication.Create(Application);
oWorkbook := TExcelWorkbook.Create(Application);
oSheet := TExcelWorksheet.Create(Application);
try
oExcel.Visible[0] := False;
oExcel.Connect;
except
result := False;
MessageDlg('Microsoft Excel belum terinstal pada komputer ini', mtError, [mbOk], 0);
exit;
end;
oExcel.Visible[0] := True;
oExcel.Caption := 'Aplikasi Toko'; //Nama aplikasi Anda
oExcel.Workbooks.Add(Null,0);
oWorkbook.ConnectTo(oExcel.Workbooks[1]);
oSheet.ConnectTo(oWorkbook.Worksheets[1] as _Worksheet);//[1] menunjukkan sheet ke-1
oDataSet.Active:=True;
oDataSet.Open;
while NOT oDataSet.Eof do begin
Inc(iRow);
//Meletakkan tempat bagi masing-masing Nama Field
oSheet.Range['A1','A1'].Value:='Kode Barang';
oSheet.Range['B1','B1'].Value:='Nama Barang';
oSheet.Range['C1','C1'].Value:='Jumlah Satuan Beli';
oSheet.Range['D1','D1'].Value:='Keterangan Satuan Beli';
oSheet.Range['E1','E1'].Value:='Harga Satuan Beli';
oSheet.Range['F1','F1'].Value:='Margin Satuan Beli';
oSheet.Range['G1','G1'].Value:='Harga Satuan Jual';
oSheet.Range['H1','H1'].Value:='Koefisien A';
oSheet.Range['I1','I1'].Value:='Jumlah Sub Satuan Beli';
oSheet.Range['J1','J1'].Value:='Keterangan Sub Satuan Beli';
oSheet.Range['K1','K1'].Value:='Harga Sub Satuan Beli';
oSheet.Range['L1','L1'].Value:='Margin Sub Satuan Beli';
oSheet.Range['M1','M1'].Value:='Harga Sub Satuan Jual';
oSheet.Range['N1','N1'].Value:='Koefisien B';
oSheet.Range['O1','O1'].Value:='Jumlah Unit Beli';
oSheet.Range['P1','P1'].Value:='Keterangan Unit Beli';
oSheet.Range['Q1','Q1'].Value:='Harga Unit Beli';
oSheet.Range['R1','R1'].Value:='Margin Unit Beli';
oSheet.Range['S1','S1'].Value:='Harga Unit Jual';
oSheet.Range['T1','T1'].Value:='Tanggal';
for iCol:=0 to oDataSet.FieldCount-1 do begin
try
oDataSet.Fields[iCol].AsString;
except
MessageDlg('Tidak ada kolom yang bertipe string', mtError, [mbOk], 0);
exit;
end;
oSheet.Cells.Item[iRow,iCol+1] := oDataSet.Fields[iCol].Value;
if oDataSet.FieldDefs[iCol].DataType IN [ftString] then
oSheet.Cells.Item[iRow,iCol+1] := oDataSet.Fields[iCol].Value;
if oDataSet.FieldDefs[iCol].DataType IN [ftInteger,ftSmallint,ftWord] then
oSheet.Cells.Item[iRow,iCol+1] := IntToStr(oDataSet.Fields[iCol].Value);
if oDataSet.FieldDefs[iCol].DataType IN [ftDateTime] then
oSheet.Cells.Item[iRow,iCol+1] := DateTimeToStr(oDataSet.Fields[iCol].Value);
if oDataSet.FieldDefs[iCol].DataType IN [ftFloat,ftCurrency] then
oSheet.Cells.Item[iRow,iCol+1] := CurrToStr(oDataSet.Fields[iCol].Value);
end;
oDataSet.Next;
end;
//membuat nama di worksheet.
oSheet.Name := 'Ini dari Tabel Daftar Barang';
//mengubah format font di semua kolom.
oSheet.Columns.Font.Color := clBlack;
oSheet.Columns.Font.FontStyle := fsBold;
oSheet.Columns.Font.Size := 10;
//Auto fit all columns.
oSheet.Columns.AutoFit;
DeleteFile(sFile);
Sleep(2000);
oSheet.SaveAs(sFile); //untuk memberi nama file Excel
oSheet.Disconnect;
oSheet.Free;
oWorkbook.Disconnect;
oWorkbook.Free;
oExcel.Quit;
oExcel.Disconnect;
oExcel.Free;
end;
End;
procedure TForm1.ButtExcelDafBarOnClick(Sender: TObject);
begin
//Membuat file Excel dg nama 'Tabel Daftar Barang' di direktori C;\
ExportToExcel(ADOTable1,'C:\Tabel Daftar Barang.XLS');
end;
Note: Thanks to Delphi-seniors for answer my questions ;)
more 18 years ago
ichan29
@yehez_kiel: :) temen2 saya mo nanya ada komponen yang gratisan ato script yang bisa di pakai buat export dari dbgrid ato table ke xls.... tolong donk...beri saya pencerahan terimakasihperasaan udah sering dibahas diforum ini, coba search aja
more ...
- Pages:
- 1
reply |
Report Obsolete
AI Forward
🚀 We're thrilled to partner with Alibaba Cloud for "AI Forward - Alibaba Cloud Global Developer Summit 2025" in Jakarta! Join us and explore the future of AI. Register now:
https://int.alibabacloud.com/m/1000400772/
#AlibabaCloud #DeveloperSummit #Jakarta #AIFORWARD
Last Articles
Last Topic
- PascalTalk #6: (Podcast) Kuliah IT di luar negeri, susah gak sih?
by LuriDarmawan in Tutorial & Community Project more 5 years ago - PascalTalk #5: UX: Research, Design and Engineer
by LuriDarmawan in Tutorial & Community Project more 5 years ago - PascalTalk #4: Obrolan Ringan Seputar IT
by LuriDarmawan in Tutorial & Community Project more 5 years ago - PascalTalk #2: Membuat Sendiri SMART HOME
by LuriDarmawan in Tutorial & Community Project more 5 years ago - PascalTalk #3: RADically Fast and Easy Mobile Apps Development with Delphi
by LuriDarmawan in Tutorial & Community Project more 5 years ago - PascalTalk #1: Pemanfaatan Artificial Intelligence di Masa Covid-19
by LuriDarmawan in Tutorial & Community Project more 5 years ago - Tempat Latihan Posting
by LuriDarmawan in OOT more 5 years ago - Archive
- Looping lagi...
by idhiel in Hal umum tentang Pascal Indonesia more 13 years ago - [ask] koneksi ke ODBC user Dsn saat runtime dengan ado
by halimanh in FireBird more 13 years ago - Validasi menggunakan data tanggal
by mas_kofa in Hal umum tentang Pascal Indonesia more 13 years ago
Random Topic
- mohon masukan...
by arjuna_1982 in Hal umum tentang Pascal Indonesia more 16 years ago - tanya checkbox di delphi
by Rogeb_Shewrite in Hal umum tentang Pascal Indonesia more 18 years ago - Format String
by galih in Tip n Trik Pemrograman more 18 years ago - Emulator untuk Windows Mobile?
by ZeAL in Hal umum tentang Pascal Indonesia more 19 years ago - delphi 6 update 2
by wati in OOT more 13 years ago - Accounting
by dannyong1999 in OOT more 20 years ago - Web Development Using with MySQL & Delphi
by aamumung in Tip n Trik Pemrograman more 17 years ago - Membuat Web Service dengan Delphi?
by johnizzy in Web Programming more 17 years ago - [ask]gimana buat form supaya memiliki style windows XP
by R960XT in Form Enhancement & Graphical Controls more 19 years ago - row dbgrid membesar
by chandra in Tip n Trik Pemrograman more 18 years ago
