登 录
if OpenDialog.Execute then begin m_boConnect := False; ADOConnection.Connected := False; ADOConnection.ConnectionString := format('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Extended Properties=Excel 8.0;Persist Security Info=False', [OpenDialog.FileName]); try ADOConnection.Connected := True; except on E: Exception do begin MainOutMessage(format('[Exception] ADOConnection.Connected Failed %s', [E.Message])); exit; end; end; m_boConnect := True; MainOutMessage('[Hint] ADOConnection.Connected Successful'); end;
function TMainForm.ExportData(sExcelTable: string): Boolean; var i, nRecordCount: Integer; begin if m_boConnect then begin ADOQuery.Close; ADOQuery.SQL.Clear; ADOQuery.SQL.Add('Select * from [' + sExcelTable + '$]'); try ADOQuery.Open; except on E: Exception do begin MainOutMessage(format('[Exception] TMainForm.ExportData(%s) OpenADOQuery %s', [sExcelTable, E.Message])); exit; end; end; nRecordCount := 0; for i := 0 to ADOQuery.RecordCount - 1 do begin //ADOQuery.Fields[nIndex].FullName //按序号获取指定字段名.nIndex是第几个字段即Excel的第几列.老规矩.0开始 //用ADO连接的时候.Excel表的第1行总是会被当作字段 //处理内容 ADOQuery.Next; end; MainOutMessage(format('[Hint] TMainForm.ExportData Successful(%s/%d)', [sExcelTable, nRecordCount])); end else MainOutMessage('[Exception] ADOConnection not connected'); end;
抱歉!评论已关闭.