用OLE调入你保存的文件。
放个TOleContainer控件
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Grids, DBGrids, Db, DBTables, Excel97, OleServer;
type
TForm1 = class(TForm)
DataSource1: TDataSource;
Table1: TTable;
DBGrid1: TDBGrid;
Button1: TButton;
SaveDialog1: TSaveDialog;
ExcelApplication1: TExcelApplication;
ExcelWorkbook1: TExcelWorkbook;
ExcelWorksheet1: TExcelWorksheet;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var row, i: integer;
begin
if SaveDialog1.Execute then
begin
ExcelApplication1.Connect;
ExcelApplication1.Workbooks.Add(Null, 0);
ExcelWorkBook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorkSheet1.ConnectTo(ExcelWorkBook1.Sheets[1] as _WorkSheet);
row := 2;
table1.First;
while not table1.Eof do
begin
for i := 0 to table1.Fields.Count - 1 do
begin
ExcelWOrkSheet1.Cells.Item[row, i + 1] := table1.Fields.AsString;
end;
row := row + 1;
table1.Next;
end;
ExcelWorkBook1.SaveCopyAs(SaveDialog1.FileName);
ExcelWOrkBook1.Close(false);
panel2.Visible := false;
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
Screen.Cursor := crDefault;
Application.MessageBox('保存完毕!', '消息', 0);
OleContainer1.CreateObjectFromFile(SaveDialog1.FileName,false);//新加的代码
end;
end;
end.