把表的内容导入excel[新手提问](50分)

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

xf9011

Unregistered / Unconfirmed
GUEST, unregistred user!
代码如下:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,oleserver,excel2000,olectnrs,db,dbgrids,dbtables,grids,stdctrls,
extctrls,comctrls, ExcelXP,comobj;

type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
DataSource1: TDataSource;
Table1: TTable;
ExcelApplication1: TExcelApplication;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation



{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
asheet:Variant;
i:integer;
begin
ExcelApplication1.Visible[0]:=True;
//显示MS-Excel的运行过程
ExcelApplication1.Workbooks.Add(xlWBATWorksheet,0);
//创建一个WorkBook
asheet:=ExcelApplication1.Workbooks.Item[1];
//获取WorkBook听一页WorkSheet

//将carmanagement.dbf的数据填写入aSheet的Cells两维数组内
i:=1;
with table1 do begin
open;
first;
while not eof do begin
asheet.cells[i,1].Value:=fieldbyname('Dw').AsString;
asheet.cells[i,2].Value:=fieldbyname('Bm').AsString;
asheet.cells[i,3].Value:=fieldbyname('Cph').AsString;
asheet.cells[i,4].Value:=fieldbyname('Cpxh').AsString;
asheet.cells[i,5].Value:=fieldbyname('Ccny').AsString;
asheet.cells[i,6].Value:=fieldbyname('Fdjh').AsString;
asheet.cells[i,7].Value:=fieldbyname('Dph').AsString;
asheet.Cells[i,8].Value:=fieldbyname('Dxqk').AsString;
asheet.cells[i,9].Value:=fieldbyname('Bxqh').AsString;
asheet.cells[i,10].Value:=fieldbyname('Bz').AsString;
end;
end;
asheet.SaveAs('d:/a.xls');
asheet.Application.quit;

end;

编译能通过,但是只要一运行就提示'Method "cells" not supported by automation object.'

请问该如何解决呀?
 
给你一个示例:
function TfrmCalcOgmKpi.ExportExcel: Boolean;
var
ExcelApp, WorkBook: Variant;
begin
Result := False;
Application.ProcessMessages;

if SaveDialog1.Execute then XlsFileName := SaveDialog1.FileName
else Exit;
if XlsFileName = '' then Exit;
try
ExcelApp := CreateOleObject('Excel.Application');
WorkBook := CreateOleobject('Excel.Sheet');
except
ShowMessage('您的机器里未安装Microsoft Excel。');
Exit;
end;
try
Screen.Cursor := crHourGlass;
WorkBook := ExcelApp.WorkBooks.Add;

//给单元格赋值:
ExcelApp.Cells[1, 2].Value := 'B1'; //第 1 行第 2 列
ExcelApp.Cells[1, 4].Value := 'D1'; //第 1 行第 4 列
ExcelApp.Cells[2, 2].Value := 'FV'; //第 2 行第 2 列
ExcelApp.Cells[2, 3].Value := 'QTY'; //第 2 行第 3 列
ExcelApp.Cells[2, 4].Value := 'FV'; //第 2 行第 4 列
ExcelApp.Cells[2, 5].Value := 'QTY'; //第 2 行第 5 列

WorkBook.SaveAs(XlsFileName);
Result := True;
finally
WorkBook.Close;
ExcelApp.Quit;
ExcelApp := Unassigned;
Screen.Cursor := crDefault;
end;
end;
你参考一下。
 

Similar threads

I
回复
0
查看
855
import
I
I
回复
0
查看
558
import
I
I
回复
0
查看
2K
import
I
后退
顶部