怎么样把dbgrid里面的数据用excel 表导出 (不使用控件),请GG jj 帮 ( 积分: 50 )

  • 主题发起人 xiangsni
  • 开始时间
X

xiangsni

Unregistered / Unconfirmed
GUEST, unregistred user!
怎么样把dbgrid里面的数据用excel 表导出 (不使用控件),请GG jj 帮
 
怎么样把dbgrid里面的数据用excel 表导出 (不使用控件),请GG jj 帮
 
那就直接把DBGRID.DATASOURCE.DATASET中的记录写道EXCEL中。
给个例程参考一下。。
先准备好数据,然后导入EXCEL。
procedure ExportDataToExcel ;
var
j : integer ;
vExcel, vSheet : variant ;
empNAME, CARDNO : string ;
realSalary : Currency ;
begin
realSalary := 0 ;
try
screen.Cursor := crHourGlass ;
dxStatusBar.Panels[2].Text := '正在载入EXCEL,请稍候..........' ;
dxStatusBar.Refresh ;
//创建EXCEL
try
vExcel := createOLEobject('excel.application') ;
if not varisempty(vExcel) then
begin
vExcel.Workbooks.Add ;
vSheet := vExcel.Workbooks[1].WorkSheets[1] ;
vSheet.Name := 'Payroll' ;
//vExcel.Visible := true ;
end
except
MessageDlg('创建EXCEL失败。',mtError,[mbOK],0) ;
exit ;
end ;
//循环写入数据
try
with DM.tempQuerydo
begin
First ;
vSheet.Columns[2].NumberFormatLocal:= '@';
//第二列卡号显示为文本格式
vSheet.Columns[2].HorizontalAlignment := xlRight ;
vSheet.Columns[1].HorizontalAlignment := xlCenter ;
vSheet.Columns[4].HorizontalAlignment := xlCenter ;
vSheet.Columns[3].HorizontalAlignment := xlRight ;
j := 4 ;
//控制单元格的行
while not Eofdo
begin
CARDNO := FieldByName('CARDNO').AsString ;
//员工卡号
empNAME := FieldByName('NAME').AsString ;
//员工姓名
case strtoint(curSalType) of //实发金额
0: realSalary := FieldByName('NETSUM').AsFloat ;
1: realSalary := FieldByName('SUMBONUS').AsFloat ;
2: realSalary := FieldByName('SUMSUBSIDY').AsFloat ;
end ;
//case
Next ;
vSheet.Cells[j,1].Value := j-3 ;
//序号
vSheet.Cells[j,4].Value := empNAME ;
vSheet.Cells[j,2].Value := CARDNO ;
//卡号,以文本格式显示数字
vSheet.Cells[j,3].Value := FloatToStr(realSalary) ;
//实发工资
Inc(j) ;
end ;
//while
end ;
//with
except
end ;
//try
if j = 4 then
begin
MessageDlg('没有有效数据',mtInformation,[mbOK],0) ;
exit ;
end ;
//设置列标题
vSheet.Cells[3,1].Value := '序号' ;
vSheet.Cells[3,1].HorizontalAlignment := xlCenter ;
vSheet.Cells[3,1].Font.FontStyle := 'bold' ;
vSheet.Cells[3,4].Value := '员工姓名' ;
vSheet.Cells[3,4].HorizontalAlignment := xlCenter ;
vSheet.Cells[3,4].Font.FontStyle := 'bold' ;
vSheet.Cells[3,2].Value := '工资卡号' ;
vSheet.Cells[3,2].HorizontalAlignment := xlCenter ;
vSheet.Cells[3,2].Font.FontStyle := 'bold' ;
vSheet.Cells[3,3].Value := '实发金额' ;
vSheet.Cells[3,3].HorizontalAlignment := xlCenter ;
vSheet.Cells[3,3].Font.FontStyle := 'bold' ;
//设置报表标题
vSheet.Range['A1:D1'].Merge ;
//合并单元格
vSheet.Range['A1:D1'].Font.FontStyle := 'bold' ;
vSheet.Range['A1:D1'].HorizontalAlignment := xlCenter ;
vSheet.Range['A1:D1'].Font.Size := '18' ;
if TreeView.Selected.Level = 1 then
vSheet.Cells[1,1].Value := TreeView.Selected.Text + '工资表'
else
vSheet.Cells[1,1].Value := TreeView.Selected.Parent.Text + '工资表' ;
vSheet.Cells[2,1].Value := CurSalYear + '年' + CurSalMonth + '月' ;
vSheet.Cells[2,4].Value := '单位:元' ;
//设置合计行
vSheet.Cells[j,1].Value := '合计:' ;
vSheet.Cells[j,1].HorizontalAlignment := xlCenter ;
vSheet.Cells[j,1].Font.FontStyle := 'bold' ;
vSheet.Cells[j,1].Font.ColorIndex := 5 ;
vSheet.Cells[j,3].Value := '=SUM(C3:C' + inttostr(j-1) + ')' ;
vSheet.Cells[j,3].Font.FontStyle := 'bold' ;
vSheet.Cells[j,3].Font.ColorIndex := 5 ;
//为数据表格划线
vSheet.Range['A3:D'+inttostr(j)].Borders.LineStyle := xlContinuous ;
//设置行高
vSheet.Range['A1:D1'].RowHeight := 30 ;
vSheet.Range['A2:D'+inttostr(j)].RowHeight := 20 ;
//设置列宽
vSheet.Columns[1].ColumnWidth := 10 ;
vSheet.Columns[4].ColumnWidth := 20 ;
vSheet.Columns[2].ColumnWidth := 25 ;
vSheet.Columns[3].ColumnWidth := 20 ;
//显示EXCEL
vExcel.Visible := true ;
finally
dxStatusBar.Panels[2].Text := '' ;
screen.Cursor := crDefault ;
end ;
end ;
 
晕,不是吧!不会这么复杂吧?
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
967
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
顶部