是什么错误信息?
我这儿有一个小例子,看看有没有用:
procedure TForm1.Button1Click(Sender: TObject);
begin
try
xlApp := TExcelApplication.Create(Self);
xlBook := TExcelWorkBook.Create(Self);
xlSheet := TExcelWorkSheet.Create(Self);
except
ShowMessage('Excel not installed in your computer!');
exit;
end;
xlApp.Visible[0] := True;
xlApp.AlertBeforeOverwriting[0] := False;
xlbook.ConnectTo(xlApp.Workbooks.Open('e:/book1.xls',NULL,false,NULL,NULL,NULL,NULL,NULL,NULL,
NULL,NULL,NULL,NULL,0)); //测试打开文件,usedrange是否正常.
// xlBook.ConnectTo(xlApp.Workbooks.Add(NULL, 0)); // 测试新建book,usedrange是否正常
xlSheet.ConnectTo(xlBook.Sheets[1] as _WorkSheet);
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
edit1.Text := format('%d,%d',[xlSheet.UsedRange[0].Rows.Count,
xlSheet.UsedRange[0].Columns.Count]);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
try
xlBook.Close(False);
xlApp.Disconnect;
xlApp.Quit;
xlApp.Free;
except
end;
end;