倾家荡产,问题简单!白送分差不多!(135分)

  • 主题发起人 主题发起人 燕十三
  • 开始时间 开始时间

燕十三

Unregistered / Unconfirmed
GUEST, unregistred user!
谁可以简要介绍一下将一个数据记里的数据转到Excel里怎么做啊?
 
用DEVELOPEXPRESS DBGRID可以保存成EXCEL格式
 
将数据转入Access数据库中,使用Office中的Access打开Access数据库文件,选择/傅志/粘贴到Excel中即可
 
<black>我说的是:比如在DBGrid中显示的数据,点击按钮之后,打开Excel,并且数据自动保存在
excel里!</black>
 
procedure TForm1.button1Click(Sender: TObject);
var
i,row,column:integer;
begin
Try
ExcelApplication1.Connect;
Except
MessageDlg('Excel may not be installed',
mtError, [mbOk], 0);
Abort;
End;
ExcelApplication1.Visible[0]:=True;
ExcelApplication1.Caption:='Excel Application';
ExcelApplication1.Workbooks.Add(Null,0);
ExcelWorkbook1.ConnectTo
(ExcelApplication1.Workbooks[1]);
ExcelWorksheet1.ConnectTo
(ExcelWorkbook1.Worksheets[1] as _Worksheet);
Table1.Open;
row:=1;
While Not(Table1.Eof) do
begin
column:=1;
for i:=1 to Table1.FieldCount do
begin
ExcelWorksheet1.Cells.Item[row,column]:
=Table1.fields[i-1].AsString;
column:=column+1;
end;
Table1.Next;
row:=row+1;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
ExcelWorksheet1.PrintPreview;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
ExcelWorksheet1.PrintOut;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
end;
 
function DataSetToExcelSheet(ADataSet: TDataSet; AFieldTagMax: Integer; ASheet: OleVariant): Boolean;
var
Row, Col, FieldIndex: Integer;
BookMark: TBookMark;
begin
if ADataSet.Active then
begin
BookMark:= ADataSet.GetBookMark;
ADataSet.DisableControls;
ASheet.Activate;
try
//工作表单的列标题
Row:= 1;
Col:= 1;
for FieldIndex:= 0 to ADataSet.FieldCount - 1 do
begin
if ADataSet.Fields[FieldIndex].Tag <= AFieldTagMax then
begin
ASheet.Cells(Row, Col):= ADataSet.Fields[FieldIndex].DisplayLabel;
Inc(Col);
end;
end;
//工作表单的内容
ADataSet.First;
while Not ADataSet.Eof do
begin
Row:= Row + 1;
Col:= 1;
for FieldIndex:= 0 to ADataSet.FieldCount - 1 do
begin
if ADataSet.Fields[FieldIndex].Tag <= AFieldTagMax then
begin
ASheet.Cells(Row, Col):= ADataSet.Fields[FieldIndex].AsString;
Inc(Col);
end;
end;
ADataSet.Next;
end;
Result:= True;
finally
ADataSet.GotoBookMark(BookMark);
ADataSet.EnableControls;
end;
end
else
Result:= False;
end;

function DataSetToExcel(ADataSet: TDataSet; AFieldTagMax: Integer; AVisible: Boolean; AExcelFileName: String = ''): Boolean;
var
ExcelObj, Excel, WorkBook, Sheet: OleVariant;
SaveDialog: TSaveDialog;
begin
if ADataset.Active then
begin
ExcelObj:= CreateOleObject('Excel.Sheet');
try
Excel:= ExcelObj.Application;
Excel.Visible:= AVisible ;
WorkBook:= Excel.WorkBooks.Add;
Sheet:= WorkBook.Sheets[1];
if DataSetToExcelSheet(ADataSet, AFieldTagMax, Sheet) then
begin
if AExcelFileName <> '' then
begin
WorkBook.SaveAs(FileName:= AExcelFileName);
Result:= True;
end
else
begin
SaveDialog:= TSaveDialog.Create(Nil);
try
SaveDialog.Filter:= 'Microsoft Excel 文件|*.xls';
if SaveDialog.Execute then
begin
WorkBook.SaveAs(FileName:= SaveDialog.FileName);
Result:= True;
end
else
Result:= False;
UpdateWindow(GetActiveWindow);
finally
SaveDialog.Free;
end;
end;
end
else
Result:= False;
finally
Excel.Quit;
end;
end
else
Result:= False;
end;
 
多人接受答案了。
 
后退
顶部