我用下列程序打开Excel表格,但是运行到open那时确无法打开指定的Excel表格,无法把表格中的数据取出,但是我手动打开指定的Excel表格时,能够把表格中的数据取出,请问高手怎样解决?
eclApp := CreateOleObject('Excel.Application');
WorkBook :=CreateOleObject('Excel.Sheet');eclApp.WorkBooks.add;
eclApp.visible :=false;
workBook:=eclApp.workBooks.open(OpenDialog1.FileName);
eclApp := CreateOleObject('Excel.Application');
WorkBook :=CreateOleObject('Excel.Sheet');eclApp.WorkBooks.add;
eclApp.visible :=false;
workBook:=eclApp.workBooks.open(OpenDialog1.FileName);
strTitle: string): boolean;
var
intRow,intColumn:integer;
intColLoop:integer;
blReturn:boolean;
excelapp: TExcelApplication;
workbook: TExcelWorkbook;
worksheet: TExcelWorksheet;
bkDS:TBook;
vMerge:variant;
tmp1,tmp2:string;
begin
if DsData.State=dsInactive then
begin
Result:=false;
Exit;
end;
DsData.DisableControls;
bkDS:=DsData.GetBook;
excelapp:=TExcelApplication.Create(nil);
workbook:= TExcelWorkbook.Create(nil);
worksheet:= TExcelWorksheet.Create(nil); try
excelapp.Connect;
excelapp.Workbooks.Add(xlWBATWorksheet,0);
workbook.ConnectTo(excelapp.Workbooks.Item[1]);
worksheet.ConnectTo(workbook.Worksheets[1] as _worksheet); with worksheet do
begin
intColumn:=DsData.FieldCount;
cells.Item[1,(intcolumn div 2)+1]:=strTitle;
cells.Item[2,(intcolumn div 2)+1]:='总记录数:'+inttostr(DsData.RecordCount); for intColLoop:=0 to intColumn -1 do
begin tmp1:=DsData.fields[intColLoop].DisplayLabel;
tmp2:=DsData.fields[intColLoop].DisplayLabel;
if tmp1='Inv_daima' then
tmp2:='发票代码';
if tmp1='Inv_start' then
tmp2:='发票起始号';
if tmp1='Inv_finish' then
tmp2:='发票终止号';
if tmp1='Normal_inv_many' then
tmp2:='正常发票数';
if tmp1='Back_inv_many' then
tmp2:='退票数';
if tmp1='Disused_inv_many' then
tmp2:='废票数';
if tmp1='Normal_total' then
tmp2:='正常发票金额';
if tmp1='Back_total' then
tmp2:='退票金额';
if tmp1='Date_start' then
tmp2:='开始日期';
if tmp1='date_finish' then
tmp2:='终止日期';
if tmp1='flag' then
tmp2:=''; cells.Item[3,intColLoop+1]:=tmp2;
end; Range[cells.Item[1,1],cells.Item[1,intColumn]].Font.Size:=18;
Range[cells.Item[1,1],cells.Item[1,intColumn]].Font.Name:='隶书';
Range[cells.Item[1,1],cells.Item[1,intColumn]].Font.Bold:=true;
Range[cells.Item[1,1],cells.Item[1,intColumn]].Merge(vMerge);
Range[cells.Item[2,1],cells.Item[2,intColumn]].Font.Size:=14;
Range[cells.Item[2,1],cells.Item[2,intColumn]].Font.Bold:=true;
Range[cells.Item[2,1],cells.Item[2,intColumn]].Merge(vMerge);
Range[cells.Item[3,1],cells.Item[2,intColumn]].Font.Size:=12;
//
Range[cells.Item[1,1],cells.Item[3,intColumn]].HorizontalAlignment:=xlCenter;
Range[cells.Item[1,1],cells.Item[3,intColumn]].VerticalAlignment:=xlCenter;
//
with dsData do
begin
First;
intRow:= 1;
while not Eof do
begin
for intColLoop:=0 to intColumn-1 do ////////
begin
if (strTitle = '月发票信息') or (strTitle ='日发票信息') or (strTitle ='发票信息') then
begin
if intColLoop = 11 then
begin
cells.Item[intRow+3,intColLoop+1].NumberFormatLocal:='@';
case fields[intColLoop].AsInteger of
1 : cells.Item[intRow+3,intColLoop+1]:='正常票';
2 : cells.Item[intRow+3,intColLoop+1]:='退票';
else
cells.Item[intRow+3,intColLoop+1]:='废票';
end;
end
else
begin
if intColLoop <> 15 then
begin
cells.Item[intRow+3,intColLoop+1].NumberFormatLocal:='@';
cells.Item[intRow+3,intColLoop+1]:=fields[intColLoop].AsString;
end;
end;
end
else ////////
begin
cells.Item[intRow+3,intColLoop+1].NumberFormatLocal:='@';
cells.Item[intRow+3,intColLoop+1]:=fields[intColLoop].AsString;
end;
end;
introw:=intRow+1;
next;
end;
end;
//cells.Item[introw+3,1]:='打印日期:'+datetostr(date);
Range[cells.Item[introw+3,1],cells.Item[introw+3,intColumn]].Merge(vMerge);
Columns.AutoFit; if fileexists(strFileName) then
deletefile(strFileName);
saveas(strFileName);
end;
blReturn:=true;
finally
excelapp.Disconnect;
excelapp.Quit;
excelapp.Free;
workbook.Free;
worksheet.Free;
DsData.GotoBook(bkDS);
DsData.EnableControls;
end;
Result:=blReturn;
end;
也需要先手动打开指定的Excel表格后才能取出表格里面的数据,
请高手指点,在不用手动打开Excel表格,就能取出表格内的数据,请问怎样实现