amirtofighi
سه شنبه 17 آبان 1390, 11:09 صبح
با سلام
من برای تبدیل اطلاعات بانک اطلاعاتی به برنامه اکسل از کد زیر استفاده میکنم:
procedure ExporttoExl(TheDataset:TDataSet;SheetExcelName:Str ing);
var
XApp:Variant;
sheet:Variant;
r,c:Integer;
q:Integer;
row,col:Integer;
fildName:Integer;
begin
try
begin
XApp:=CreateOleObject('Excel.Application');
XApp.Visible:=true;
end;
except
showmessage('Unable to link with MS Excel, it seems as it is not installed on this system.');
exit;
end;
XApp.WorkBooks.Add(-4167);
//open a new blank workbook
XApp.WorkBooks[1].WorkSheets[1].Name:='Sheet1';
//give any name required to ExcelSheet
sheet:=XApp.WorkBooks[1].WorkSheets['Sheet1'];
for fildName:=1 to TheDataset.FieldCount-1 do
//TheDataset refer to the any dataset holding data
begin
q:=fildName+1;
sheet.Cells[1,q]:=TheDataset.Fields[fildName].FieldName; // enter the column headings
end;
//now supply the data from table to excel sheet
TheDataset.First;
for r:=0 to TheDataset.RecordCount-1 do
begin
for c:=1 to TheDataset.FieldCount-1 do
begin
row:=r+2;
col:=c;
sheet.Cells[row,col]:=TheDataset.Fields[c].AsString;
end;
TheDataset.Next;
end;
//set font attributes of required range if required
XApp.WorkSheets['Sheet1'].Range['A1:AA1'].Font.Bold:=True;
XApp.WorkSheets['Sheet1'].Range['A1:AA1'].Font.Color := clblue;
XApp.WorkSheets['Sheet1'].Range['A1:AA1'].Font.Color := clblue;
XApp.WorkSheets['Sheet1'].Range['A1:F1'].Borders.LineStyle :=12;
//XApp.WorkSheets['Sheet1'].Columns[1,10].ColumnWidth.AutoFit;
// set other attributes as below
XApp.WorkSheets['Sheet1'].Range['A1:F1000'].HorizontalAlignment :=3;
XApp.WorkSheets['Sheet1'].Range['A1:F1000'].verticalAlignment :=2;
// .Borders.LineStyle :=13;
//XApp.WorkSheets['Sheet1'].Columns[1].ColumnWidth:=10;
xapp.caption := 'Exported from Demo programmed by SK Arora,the digitiger';
XApp.WorkSheets['Sheet1'].name := 'Exported from ' + SheetExcelName;
//assuming dataset is TTable based its tablename can be given as title of worksheet
//close;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ExporttoExl(DBISAMTable1,'WORKERS');
end;
من چند تا مشکل دارم میخواستم بدونم میتونین کمکم کنید:
1)وقتی فایل اکسل رو میبینم اون ستونی که مربوط به فیلد از نوع memo هست، سطرهارو با یه enter اضافه نشون میده مثلا:
سطر1
سطر2
سطر3
رو اینطور نشون میده:
سطر1
سطر2
سطر3
2)چطور میتونم کدی وارد کنم که عرض ستونهای فایل اکسل رو باتوجه به اطلاعات هر ستون خودش resize کنه
3)برای اینکه سطرها و ستونها رو وسط چین کنم از کد
[XApp.WorkSheets['Sheet1'].Range['A1:F1000'].HorizontalAlignment :=3;
XApp.WorkSheets['Sheet1'].Range['A1:F1000'].verticalAlignment :=2;
استفاده کردم. میخواستم بدونم با چه کدی میشه کل صفحه رو این خاصیت داد؟
خیلی ممنونم
من برای تبدیل اطلاعات بانک اطلاعاتی به برنامه اکسل از کد زیر استفاده میکنم:
procedure ExporttoExl(TheDataset:TDataSet;SheetExcelName:Str ing);
var
XApp:Variant;
sheet:Variant;
r,c:Integer;
q:Integer;
row,col:Integer;
fildName:Integer;
begin
try
begin
XApp:=CreateOleObject('Excel.Application');
XApp.Visible:=true;
end;
except
showmessage('Unable to link with MS Excel, it seems as it is not installed on this system.');
exit;
end;
XApp.WorkBooks.Add(-4167);
//open a new blank workbook
XApp.WorkBooks[1].WorkSheets[1].Name:='Sheet1';
//give any name required to ExcelSheet
sheet:=XApp.WorkBooks[1].WorkSheets['Sheet1'];
for fildName:=1 to TheDataset.FieldCount-1 do
//TheDataset refer to the any dataset holding data
begin
q:=fildName+1;
sheet.Cells[1,q]:=TheDataset.Fields[fildName].FieldName; // enter the column headings
end;
//now supply the data from table to excel sheet
TheDataset.First;
for r:=0 to TheDataset.RecordCount-1 do
begin
for c:=1 to TheDataset.FieldCount-1 do
begin
row:=r+2;
col:=c;
sheet.Cells[row,col]:=TheDataset.Fields[c].AsString;
end;
TheDataset.Next;
end;
//set font attributes of required range if required
XApp.WorkSheets['Sheet1'].Range['A1:AA1'].Font.Bold:=True;
XApp.WorkSheets['Sheet1'].Range['A1:AA1'].Font.Color := clblue;
XApp.WorkSheets['Sheet1'].Range['A1:AA1'].Font.Color := clblue;
XApp.WorkSheets['Sheet1'].Range['A1:F1'].Borders.LineStyle :=12;
//XApp.WorkSheets['Sheet1'].Columns[1,10].ColumnWidth.AutoFit;
// set other attributes as below
XApp.WorkSheets['Sheet1'].Range['A1:F1000'].HorizontalAlignment :=3;
XApp.WorkSheets['Sheet1'].Range['A1:F1000'].verticalAlignment :=2;
// .Borders.LineStyle :=13;
//XApp.WorkSheets['Sheet1'].Columns[1].ColumnWidth:=10;
xapp.caption := 'Exported from Demo programmed by SK Arora,the digitiger';
XApp.WorkSheets['Sheet1'].name := 'Exported from ' + SheetExcelName;
//assuming dataset is TTable based its tablename can be given as title of worksheet
//close;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ExporttoExl(DBISAMTable1,'WORKERS');
end;
من چند تا مشکل دارم میخواستم بدونم میتونین کمکم کنید:
1)وقتی فایل اکسل رو میبینم اون ستونی که مربوط به فیلد از نوع memo هست، سطرهارو با یه enter اضافه نشون میده مثلا:
سطر1
سطر2
سطر3
رو اینطور نشون میده:
سطر1
سطر2
سطر3
2)چطور میتونم کدی وارد کنم که عرض ستونهای فایل اکسل رو باتوجه به اطلاعات هر ستون خودش resize کنه
3)برای اینکه سطرها و ستونها رو وسط چین کنم از کد
[XApp.WorkSheets['Sheet1'].Range['A1:F1000'].HorizontalAlignment :=3;
XApp.WorkSheets['Sheet1'].Range['A1:F1000'].verticalAlignment :=2;
استفاده کردم. میخواستم بدونم با چه کدی میشه کل صفحه رو این خاصیت داد؟
خیلی ممنونم