修改代码 ( 积分: 100 )

  • 主题发起人 主题发起人 wqhatnet
  • 开始时间 开始时间
W

wqhatnet

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure taform2.CopyDbDataToExcel(Args: array of const);
var
iCount, jCount: Integer;
XLApp: Variant;
Sheet: Variant;
I: Integer;
begin
Screen.Cursor := crHourGlass;
if not VarIsEmpty(XLApp) then
begin
XLApp.DisplayAlerts := False;
XLApp.Quit;
VarClear(XLApp);
end;

try
XLApp := CreateOleObject('Excel.Application');
except
Screen.Cursor := crDefault;
Exit;
end;

XLApp.WorkBooks.Add;
XLApp.SheetsInNewWorkbook := High(Args) + 1;

for I := Low(Args) to High(Args) do
begin
XLApp.WorkBooks[1].WorkSheets[I+1].Name := TDBGrid(Args.VObject).Name;
Sheet := XLApp.Workbooks[1].WorkSheets[TDBGrid(Args.VObject).Name];

if not TDBGrid(Args.VObject).DataSource.DataSet.Active then
begin
Screen.Cursor := crDefault;
Exit;
end;

TDBGrid(Args.VObject).DataSource.DataSet.first;
for iCount := 0 to TDBGrid(Args.VObject).Columns.Count - 1 do
Sheet.Cells[1, iCount + 1] :=
TDBGrid(Args.VObject).Columns.Items[iCount].Title.Caption;

jCount := 1;
while not TDBGrid(Args.VObject).DataSource.DataSet.Eof do
begin
for iCount := 0 to TDBGrid(Args.VObject).Columns.Count - 1 do
Sheet.Cells[jCount + 1, iCount + 1] :=
TDBGrid(Args.VObject).Columns.Items[iCount].Field.AsString;

Inc(jCount);
TDBGrid(Args.VObject).DataSource.DataSet.Next;
end;
XlApp.Visible := True;
end;
Screen.Cursor := crDefault;
end;
我想把它修改成打开指定的的EXCEL文档,添加指定的WOOKBOOK中
 
不会吧,只要打出来啊?那直接连接数据库打印不就行了.
像fastreport,reportmachine有显存的打印控件可以打印图的啊?
而且可以导出.
[:D]
 
直接打印出来,干嘛还要变成.GIF...
 
有点难度
 
怎么打印啊
A4纸上
 
如果非要转成图片,可以截屏啊,很多截屏代码的 搜搜
 
楼主的DBGrid是不是并不跟数据库连接?
你要生成jpeg文件的话,可以把DBGrid上的内容TextOut到Image控件上面再生成图形文件.
 
var BMP: TBitMap;
tpJpg:TJpegImage;
Rect,SRect:TRect;
begin
BMP := TBitMap.Create;
tpJpg:=TJpegImage.create;
try
BMP.Width := self.ClientWidth + 5;
BMP.Height := self.ClientHeight - PanelCtrl.Height + 5;
SRect.Left := 0;
SRect.Top := 0;
SRect.Right := Bmp.Width - 5;
SRect.Bottom:= Bmp.Height - 5;
Rect := SRect;
Rect.Left := Rect.Left + 5;
Rect.Top := Rect.Top +5;
bmp.PixelFormat := pf24bit;
BMP.Canvas.CopyRect(Rect, self.Canvas, SRect);
tpJpg.Assign(BMP);
//可用JPG图像了,想放哪儿放哪儿
Finally
Bmp.Free;
tpJpg.Free;
end;

如果仅截取表格的话,方法类似,自己稍微改一下就得了
 
后退
顶部