Excel的导入产生了OLE error 800A03EC!(30分)

  • 主题发起人 主题发起人 lijun175165721
  • 开始时间 开始时间
L

lijun175165721

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;xlApp,xlBook,xlSheet,xlQuery: Variant;<br> &nbsp;adoConnection,adoRecordset: Variant;<br>begin<br> &nbsp;adoConnection :=CreateOleObject('ADODB.Connection');<br> &nbsp;adoRecordset :=CreateOleObject('ADODB.Recordset');<br> &nbsp;adoConnection.Open(self.ADOConnection1.ConnectionString);<br> &nbsp;adoRecordset.CursorLocation :=self.ADOQuery1.CursorLocation;<br> &nbsp;adoRecordset.Open('SELECT * FROM table1',adoConnection,1,3);<br> &nbsp;xlApp := CreateOleObject('Excel.Application');<br> &nbsp;xlBook := xlApp.Workbooks.Add;<br> &nbsp;xlSheet := xlBook.Worksheets['sheet1'];<br> &nbsp;xlApp.Visible := True;<br><br> &nbsp;//把查询结果导入EXCEL数据<br><br> &nbsp;xlQuery := xlSheet.QueryTables.Add(adoRecordset,xlSheet.Range[ '&quot;A1:E2&quot;' ]); &nbsp;//出问题老是这一句<br><br> &nbsp;xlQuery.FieldNames := True;<br> &nbsp;xlQuery.RowNumbers := False;<br> &nbsp;xlQuery.FillAdjacentFormulas := False;<br> &nbsp;xlQuery.PreserveFormatting := True;<br> &nbsp;xlQuery.RefreshOnFileOpen := False;<br> &nbsp;xlQuery.BackgroundQuery := True;<br> &nbsp;//xlQuery.RefreshStyle := xlInsertDeleteCells;<br> &nbsp;xlQuery.SavePassword := True;<br> &nbsp;xlQuery.SaveData := True;<br> &nbsp;xlQuery.AdjustColumnWidth := True;<br> &nbsp;xlQuery.RefreshPeriod := 0;<br> &nbsp;xlQuery.PreserveColumnInfo := True;<br> &nbsp;xlQuery.FieldNames := True;<br> &nbsp;xlQuery.Refresh;<br>end;<br>运行后产生了OLE error 800A03EC!的错误!请大家指点!
 
use comobj;<br>procedure DAOCHU(FILENAME:STRING;SENDER:TOBJECT);<br>VAR<br> &nbsp;Excel:variant;<br> &nbsp;book:variant;<br> &nbsp;Sheet: Variant;<br> &nbsp;Col, Row: Integer;<br>begin<br> Excel:=UnAssigned;<br> Excel:=CreateOleObject('Excel.Application');<br> Excel.Visible:=FALSE;<br> Excel.WorkBooks.Add;<br> book:=Excel.ActiveWorkBook;<br>///以上为建立新工作薄//////////////////////////<br>Sheet :=Book.WorkSheets[1];<br>row:=1;<br> &nbsp; &nbsp;for Col :=0 to (SENDER AS TDBGRID).COLUMNS.COUNT-1 &nbsp;do<br> &nbsp; &nbsp; &nbsp; Sheet.Cells[Row, Col+1] :=(SENDER AS TDBGRID).Columns[COL].Title.Caption;<br><br>row:=2;<br>(SENDER AS TDBGRID).DataSource.DataSet.FIRST;<br> &nbsp;WHILE NOT &nbsp; (SENDER AS TDBGRID).DataSource.DataSet.EOF DO<br> &nbsp; BEGIN<br> &nbsp; &nbsp; &nbsp; &nbsp; for Col :=0 to (SENDER AS TDBGRID).COLUMNS.COUNT-1 &nbsp;do<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Sheet.Cells[Row, Col+1] :=(SENDER AS TDBGRID).Fields[COL].AsString;<br><br> &nbsp; &nbsp; (SENDER AS TDBGRID).DataSource.DataSet.MOVEBY(1);<br> &nbsp; &nbsp; INC(ROW);<br> &nbsp; END;<br><br> Sheet.Cells.Columns.AutoFit;<br> book.SaveAs(FILENAME); &nbsp;//保存EXCEL<br> Excel.Quit; /////关闭EXCEL//////<br>END;
 
使用方法,调用时传入参数即可,文件名和表格DBGRID控件名
 
后退
顶部