Home > Back-end > O EXCEL import MSSQL database code, can use, high efficiency!
O EXCEL import MSSQL database code, can use, high efficiency!
Time:10-13
As title, I now is in this way, the efficiency is low
var ASql APath: String; ExcelApp: Variant; AInt: Integer; The begin If messagebox (Self. Handle, 'are you sure you want to import the data sent straight? ', 'prompt', mb_yesno + mb_iconquestion)=idyes then The begin If OpenDialog1. Execute Then APath:=OpenDialog1 FileName The Else The Exit;
If not FileExists (APath) Then The Begin Messagebox (self. Handle, 'the file does not save you choose, please check it', 'message' mb_iconinformation); The Exit; The End; Try Application. ProcessMessages; ExcelApp:=CreateOleObject (' Excel. Application); ExcelApp. Workbooks. Open (APath); ExcelApp. WorkSheets [1]. Activate;
ASql:='select * from hy_zs where 1=2'. ADOQuery1. Close; ADOQuery1. SQL. Text:=ASql; ADOQuery1. Open;
For AInt:=2 To ExcelApp. ActiveSheet. UsedRange. Rows. Count the Do//AInt starting line number, the heavy line which began To import, AInt initial value is a few The Begin ADOQuery1. Append; ADOQuery1. FieldByName (' Invoice '). AsString:=ExcelApp. Cells [AInt, 1]. The Value; What is the row,//Aint behind the Numbers is which columns, ADOQuery1. FieldByName (' Zsdate). AsDateTime:=ExcelApp. Cells [AInt, 2]. The Value; ADOQuery1. Post; The End; The Finally ExcelApp. WorkBooks. Close; ExcelApp. Quit; Messagebox (self. Handle 'straight to send data import is successful, please check it,' message 'MB_ICONINFORMATION); The End; end;
CodePudding user response:
Provide a EXCEL import ACCESS for your reference under
SELECT * INTO a temporary table FROM [Sheet1 $] IN "' + EXCEL the full path of the file name +" "EXCEL 8.0;" Sheet1 is EXCEL table name
Then put the temporary table inserted INTO a formal table INSERT INTO a SELECT statement
Statements in the form of: Insert into Table2 (field1, field2,... ) select value1, value2,... The from Table1
Request target table Table2 must exist, due to the target table Table2 already exists, so we in addition to insert table Table1 field source, can also insert the constants,
Then DROP TABLE TABLE name Drop the temporary table
The ACCESS is such operation
CodePudding user response:
To choose language sentence will be output range, copied to a temporary WorkSheets, then derived the temporary WorkSheets, don't enumeration to output line by line, so it is fast,