我在delphi做了个程序,现在我先将DBGrid中的数据导出EXECL中,怎么做了,我是新手,最后有注解 15
展开全部
别不多说,直接给你个我做的列子吧 注意uses ComObj;
//dbgird导出excel
procedure Tjhc_pzmdcs_f.ExportExc(DG :TDBGrid; mdzl:string);
var
ExcelApp: Variant;
i,j: integer;
begin
if DG.DataSource.DataSet.IsEmpty then exit;
ExcelApp := CreateOleObject( 'Excel.Application' );
ExcelApp.Caption := '应用程序调用 Microsoft Excel';
ExcelApp.WorkBooks.Add;
ExcelApp.WorkSheets[1].Activate ;
ExcelApp.Cells[1,1].Value := '品种代码';
ExcelApp.Cells[1,2].Value := '品种名称';
ExcelApp.Cells[1,3].Value := '机型';
ExcelApp.Cells[1,4].Value := mdzl;
ExcelApp.ActiveSheet.Columns[1].ColumnWidth := 12;
ExcelApp.ActiveSheet.Columns[2].ColumnWidth := 30;
ExcelApp.ActiveSheet.Columns[3].ColumnWidth := 18;
ExcelApp.ActiveSheet.Columns[4].ColumnWidth := 12;
DG.DataSource.DataSet.First;
for j:=1 to DG.DataSource.DataSet.RecordCount do
begin
ExcelApp.Cells[j+1,1].Value := DG.DataSource.DataSet.FieldByName('x_pzdm').asstring;
ExcelApp.Cells[j+1,2].Value := DG.DataSource.DataSet.FieldByName('l_pzdm').asstring;
ExcelApp.Cells[j+1,3].Value := DG.DataSource.DataSet.FieldByName('x_jx').asstring;
if mdzl = '密度常数' then
ExcelApp.Cells[j+1,4].Value := DG.DataSource.DataSet.FieldByName('x_mdcs').asstring;
if mdzl = '密度常数平均值' then
ExcelApp.Cells[j+1,4].Value := DG.DataSource.DataSet.FieldByName('x_mdcspjz').asstring;
DG.DataSource.DataSet.Next;
end;
ExcelApp.ActiveSheet.Rows[1].Font.Bold := True;
ExcelApp.ActiveSheet.Rows[1].Font.size := 18;
ExcelApp.Cells.VerticalAlignment := 2;
ExcelApp.Cells.HorizontalAlignment := 3;
ExcelApp.Cells.Font.Size:=12;
ExcelApp.ActiveSheet.PageSetup.CenterHorizontally := True;
ExcelApp.ActiveSheet.PageSetup.CenterVertically := True;
ExcelApp.Visible := True;
end;
//dbgird导出excel
procedure Tjhc_pzmdcs_f.ExportExc(DG :TDBGrid; mdzl:string);
var
ExcelApp: Variant;
i,j: integer;
begin
if DG.DataSource.DataSet.IsEmpty then exit;
ExcelApp := CreateOleObject( 'Excel.Application' );
ExcelApp.Caption := '应用程序调用 Microsoft Excel';
ExcelApp.WorkBooks.Add;
ExcelApp.WorkSheets[1].Activate ;
ExcelApp.Cells[1,1].Value := '品种代码';
ExcelApp.Cells[1,2].Value := '品种名称';
ExcelApp.Cells[1,3].Value := '机型';
ExcelApp.Cells[1,4].Value := mdzl;
ExcelApp.ActiveSheet.Columns[1].ColumnWidth := 12;
ExcelApp.ActiveSheet.Columns[2].ColumnWidth := 30;
ExcelApp.ActiveSheet.Columns[3].ColumnWidth := 18;
ExcelApp.ActiveSheet.Columns[4].ColumnWidth := 12;
DG.DataSource.DataSet.First;
for j:=1 to DG.DataSource.DataSet.RecordCount do
begin
ExcelApp.Cells[j+1,1].Value := DG.DataSource.DataSet.FieldByName('x_pzdm').asstring;
ExcelApp.Cells[j+1,2].Value := DG.DataSource.DataSet.FieldByName('l_pzdm').asstring;
ExcelApp.Cells[j+1,3].Value := DG.DataSource.DataSet.FieldByName('x_jx').asstring;
if mdzl = '密度常数' then
ExcelApp.Cells[j+1,4].Value := DG.DataSource.DataSet.FieldByName('x_mdcs').asstring;
if mdzl = '密度常数平均值' then
ExcelApp.Cells[j+1,4].Value := DG.DataSource.DataSet.FieldByName('x_mdcspjz').asstring;
DG.DataSource.DataSet.Next;
end;
ExcelApp.ActiveSheet.Rows[1].Font.Bold := True;
ExcelApp.ActiveSheet.Rows[1].Font.size := 18;
ExcelApp.Cells.VerticalAlignment := 2;
ExcelApp.Cells.HorizontalAlignment := 3;
ExcelApp.Cells.Font.Size:=12;
ExcelApp.ActiveSheet.PageSetup.CenterHorizontally := True;
ExcelApp.ActiveSheet.PageSetup.CenterVertically := True;
ExcelApp.Visible := True;
end;
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询