如何在DELPHI中把数据直接输出到EXCEL表中(50分)

  • 主题发起人 主题发起人 mzwl
  • 开始时间 开始时间
M

mzwl

Unregistered / Unconfirmed
GUEST, unregistred user!
如何在DELPHI中把数据直接输出到EXCEL表中
 
[blue]我有一例,也是修改于别人的:
//实际使用的函数,内部调用了datasettoexcelsheet,在外面加入ui接口和错误处理
function datasettoexcel(dataset: tdataset; // 要转换的数据集
fieldtagmax: integer; // 字段的tag值如果大于这个值,就不导出到excel
visible: boolean; // 是否让做转换工作的excel可见
excelfilename: string = ''; // excel文件名,*.xls
ftitle:string='';cell_first:integer=2): boolean;
//把数据集导入excelsheet的核心函数
// fieldtagmax字段的tag值如果大于这个值,就不导出到excel
function datasettoexcelsheet(
dataset: tdataset;
fieldtagmax: integer;
sheet: olevariant;
ftitle:string=''; cell_first: integer=2): boolean;

function datasettoexcelsheet(dataset: tdataset;fieldtagmax: integer; sheet: olevariant;ftitle:string;cell_first:integer): boolean;
var
row, col, fieldindex: integer;
bk: tbookmark;
begin
result := false;
if not dataset.active then exit;
bk := dataset.getbookmark;
dataset.disablecontrols;

sheet.activate;
try
sheet.cells[1,cell_first]:=ftitle; //总标题
sheet.range[sheet.cells[1,1],sheet.cells[1,10]].font.size:=18;
sheet.range[sheet.cells[1,1],sheet.cells[1,10]].font.bold:=true;
sheet.range[sheet.cells[1,1],sheet.cells[1,10]].font.NAME:='宋体';
// sheet.range[sheet.cells[1,2],sheet.cells[1,12]].Merge;
// 列标题
row := 2;
col := 1;
for fieldindex := 0 to dataset.fieldcount - 1 do
begin
if dataset.fields[fieldindex].tag <= fieldtagmax then
begin
sheet.cells(row, col) := dataset.fields[fieldindex].displaylabel;
inc(col);
end;
end;
// 表内容
dataset.first;
while not dataset.eof do
begin
row := row + 1;
col := 1;
for fieldindex := 0 to dataset.fieldcount - 1 do
begin
if dataset.fields[fieldindex].tag <= fieldtagmax then
begin
sheet.cells(row, col) := dataset.fields[fieldindex].asstring;
inc(col);
end;
end;
dataset.next;
end;

result := true;
finally
dataset.gotobookmark(bk);
dataset.enablecontrols;
end;


end;

function datasettoexcel(
dataset: tdataset; fieldtagmax: integer;
visible: boolean; excelfilename: string = '';ftitle:string='';cell_first:integer=2): boolean;
var
excelobj, excel, workbook, sheet: olevariant;
oldcursor: tcursor;
savedialog: tsavedialog;
begin
result := false;
if not dataset.active then exit;

oldcursor := screen.cursor;
screen.cursor := crhourglass;

try
excelobj := createoleobject('excel.sheet');
excel := excelobj.application;
excel.visible := visible;
workbook := excel.workbooks.add;
sheet := workbook.sheets[1];
except
messagebox(getactivewindow, '无法调用mircorsoft excel! ' + chr(13) + chr(10) +
'请检查是否安装了mircorsoft excel。', '提示', mb_ok + mb_iconinformation);
screen.cursor := oldcursor;
exit;
end;

result := datasettoexcelsheet(dataset, fieldtagmax, sheet,ftitle,cell_first);
if result then
if not visible then
begin
if excelfilename <> ''
then workbook.saveas(filename := excelfilename)
else begin
savedialog := tsavedialog.create(nil);
savedialog.filter := 'microsoft excel 文件|*.xls';
result := savedialog.execute;
updatewindow(getactivewindow);
if result then
workbook.saveas(filename := savedialog.filename);
savedialog.free;
end;
excel.quit;
end;
screen.cursor := oldcursor;
end;[/blue]
 
嗯,比较简单,以前也有很多相关帖子。
 
go www.delphipages.com
 
后退
顶部