用OLE把dbgrid内容输出到excel简单,方法如下: procedure CopyDbDataToExcel(Target:TDbgid); var iCount,jCount:Integer; XLApp:Variant; Sheet: Variant; Target:TDBGrid; begin Screen.Cursor := crHourGlass; if not VarIsEmpty(XLApp) then begin XLApp.DisplayAlerts := False; XLApp.Quit; VarClear(XLApp); end; //通过ole创建Excel对象 Try XLApp := CreateOleObject('Excel.Application'); Except Screen.Cursor := crDefault; Exit; end;
XLApp.WorkBooks.Add[XLWBatWorksheet]; XLApp.WorkBooks[1].WorkSheets[1].Name := '测试工作薄'; Sheet := XLApp.Workbooks[1].WorkSheets['测试工作薄'] ; if Not Target.DataSource.DataSet.Active then begin Screen.Cursor := crDefault; Exit; end; Target.DataSource.DataSet.first;
for iCount := 0 to Target.Columns.Count -1 do begin Sheet.cells[1 ,iCount+1] := Target.Columns.Items[iCount].Title.Caption; end; jCount:=1; While Not Target.DataSource.DataSet.Eof do begin for iCount := 0 to Target.Columns.Count -1 do begin Sheet.cells[jCount+1 ,iCount+1] := Target.Columns.Items[iCount].Field.AsString; end; Inc(jCount); Target.DataSource.DataSet.Next; end; XlApp.Visible := True; Screen.Cursor := crDefault; end; word:=createoleobject('word.application'); word.visible:=true; word.documents.open('e:\abc.doc',false); word.activedocument.range(0,0); word.activedocument.tables.add(word.activedocument.range,2,3); word.activedocument.sellection.insertrowsbelow(1);
procedure CopyDbDataToExcel(Target:TDbgid);
var
iCount,jCount:Integer;
XLApp:Variant;
Sheet: Variant;
Target:TDBGrid;
begin
Screen.Cursor := crHourGlass;
if not VarIsEmpty(XLApp) then
begin
XLApp.DisplayAlerts := False;
XLApp.Quit;
VarClear(XLApp);
end;
//通过ole创建Excel对象
Try
XLApp := CreateOleObject('Excel.Application');
Except
Screen.Cursor := crDefault;
Exit;
end;
XLApp.WorkBooks.Add[XLWBatWorksheet];
XLApp.WorkBooks[1].WorkSheets[1].Name := '测试工作薄';
Sheet := XLApp.Workbooks[1].WorkSheets['测试工作薄'] ;
if Not Target.DataSource.DataSet.Active then
begin
Screen.Cursor := crDefault;
Exit;
end;
Target.DataSource.DataSet.first;
for iCount := 0 to Target.Columns.Count -1 do
begin
Sheet.cells[1 ,iCount+1] := Target.Columns.Items[iCount].Title.Caption;
end;
jCount:=1;
While Not Target.DataSource.DataSet.Eof do
begin
for iCount := 0 to Target.Columns.Count -1 do
begin
Sheet.cells[jCount+1 ,iCount+1] := Target.Columns.Items[iCount].Field.AsString;
end;
Inc(jCount);
Target.DataSource.DataSet.Next;
end;
XlApp.Visible := True;
Screen.Cursor := crDefault;
end;
word:=createoleobject('word.application');
word.visible:=true;
word.documents.open('e:\abc.doc',false);
word.activedocument.range(0,0);
word.activedocument.tables.add(word.activedocument.range,2,3);
word.activedocument.sellection.insertrowsbelow(1);
将生成好的DBchart图形插入到excel中:filepath :='c:\1.bmp';
DBChart1.SaveToBitmapFile(filepath);sheet.Range['A1'].Select;
Selection := sheet.Pictures.Insert( filepath);
Selection.ShapeRange.PictureFormat.Brightness := 0.65;
Selection.ShapeRange.IncrementLeft(8.25);
Selection.ShapeRange.IncrementTop(-9.75);deletefile(filepath);
sheet是啥类型?
--------------------------var
workbook,sheet,excelObj,excel : OleVariant;
excelObj := CreateOleObject('excel.sheet');
excel := excelObj.Application; workbook := excel.workbooks.open( '报表模板路径' );
sheet := workbook.sheets[1];
Excel.ActiveWindow.WindowState := -4137;filepath :='c:\1.bmp';
DBChart1.SaveToBitmapFile(filepath);sheet.Range['A1'].Select;
Selection := sheet.Pictures.Insert( filepath);
Selection.ShapeRange.PictureFormat.Brightness := 0.65;
Selection.ShapeRange.IncrementLeft(8.25);
Selection.ShapeRange.IncrementTop(-9.75);deletefile(filepath);
Excel.visible := true;
Selection.ShapeRange.IncrementLeft(8.25);
Selection.ShapeRange.IncrementTop(-9.75);第一句只是对颜色的透明度
后两句是对图片的位置进行设置houwei1008(下雨天)兄竟然拿着我的程序来贴,嘿嘿,
看来要搞点什么冷饮之类的了