Home > database >  How to excel imported into the database
How to excel imported into the database

Time:09-22

Objective: through the PB import excel to the corresponding database table,

Thinking: to choose the excel file, loaded into the DataWindow, then inserted into a table,

The idea is feasible?

CodePudding user response:

According to this train of thought,
I built a application contains an event
Open (w_main)//w_main
A window w_main
Have a DataWindowd control w_product
A cb_1 button clicked event:
Long numcols numrows, c, r
OLEObject xlapp, xlsub
Int ret
//define variables

Numcols=long (dw_product. Object. The DataWindow. Column. Count)
//set the line and column number
Numrows=dw_product. RowCount ()
//get lines
XlApp=Create OLEObject
//create OLE object

Ret=xlApp. ConnectToNewObject (" Excel Sheet ")
If ret & lt; 0 then
MessageBox (" connection Excel program failed! ", a string (ret))
Return
End the if
/Excel/connection and test the return value

XlApp. Application. Workbooks. Open (" E: \ ybb. XLS ")
//open a specific Excel file
XlApp. Application. The Visible=true
Make the Excel file visible//
Xlsub=xlapp. Application. ActiveWorkbook. Worksheets [1]
//determine the first book

For c=1 to numcols
For r=1 to numrows
Xlsub. Cells (r, c)=dw_product. Object. The data (r, c)
Next
Next
//cycle to send data

XlApp. DisConnectObject ()
Destroy xlapp
//disconnected



Run error:
The original concept Application Execution Error (R0002)
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
The Application terminated.

Error: Null object reference at line 6 clicked in the event of object cb_1 of w_main.

CodePudding user response:

Give you a function that can be called directly import the XLS, text or data in a DBF file, of course, the premise is that the data window column sequence to excel file column sequence is the same, you can find a function name:
String path and filename
An integer value, the result, li_startrow

//ab_flag Boolean incoming header row, no==true, false
//idw_dw datawindow data window
//return long

If ab_flag then
Li_startrow=2
The else
Li_startrow=1
End the if

Value=https://bbs.csdn.net/topics/GetFileOpenName (please select a file to import, the path and filename, "like", "the.xls Files (*. XLS), *. XLS, Text Files (*.txt), *. TXT, DBF Files (*. DBF), *. DBF")

If value<> 1 then return 1

If right (path, 3)='like' or right (path, 3)='XLS' then
OLEObject ObjExcel
ObjExcel=CREATE OLEObject
Result=ObjExcel. ConnectToNewObject (" excel. Application ")
If the result & lt;> 0 then
Messagebox (" message ", "the connection of EXCEL failed, please check whether installed in the computer EXCEL!" )
The Return - 1
The else
ObjExcel. Workbooks. Open (path)

//ObjExcel. Activeworkbook. Sheets (" sheet2 "). Select

String ls_copy
Int I
If ObjExcel. ActiveSheet. Cells. Copy=true then
ObjExcel. Application. The Visible=false
Idw_dw. SetTransObject (SQLCA)
I=idw_dw. ImportClipboard (li_startrow)//the system clipboard content on the paste to the data window, among them 2 as the starting line parameter
Choose a case I
Case 1
MessageBox (' warning ', 'there is no record in Excel file line or starting value is greater than the number of rows in a string of')
Case - 3
MessageBox (' warning ', 'invalid argument,')
Case - 4
MessageBox (' warning ', 'invalid input,')
Case - 13
MessageBox (' warning 'and' input data do not support the type of the data window, ')
Case - 14
MessageBox (' warning ', 'nested error parsing data window,')
The end choose
The Clipboard (" ")//clear Clipboard content on the
ObjExcel. Quit ()
ObjExcel. DisconnectObject ()//disconnect with OLE
Destroy ObjExcel
The return of I
The else
Messagebox (" prompt ", "your EXCEL does not support this function, please check the EXCEL version, it is recommended to use Microsoft OFFICE, EXCEL")
End the if
End the if
Elseif right (path, 3)='TXT' or right (path, 3)='TXT' then
I=idw_dw. Importfile (text! That path, li_startrow)
The return of I
Elseif right (path, 3)='DBF' or right (path, 3)='DBF' then
I=idw_dw. Importfile (DBase3! That path, li_startrow)
The return of I
The else
The return - 1
End the if

CodePudding user response:

Serial number function code case
"String ls_FileName, ls_DirFile
String is_dir
The integer li_value
Li_value=https://bbs.csdn.net/topics/GetFileOpenName (please select the excel file name ""," "ls_DirFile, ls_FileName," "XLS" ", "" Excell Files (*. XLS), *. XLS" ") "
"IF li_value=https://bbs.csdn.net/topics/1 THEN
The is_dir=left (ls_DirFile, pos (ls_DirFile ls_FileName) - 1)
Sle_EA. Text=ls_DirFile
//get the Excel file name
If lastpos (ls_DirFile, 'like')=0 Then
End the If
Is_xlsName=midw (ls_DirFile, lastpos (ls_DirFile, '\') + 1, lastpos (ls_DirFile, 'like') - lastpos (ls_DirFile, '\') - 1)
""OLEObject OLE_ExcelA
OLE_ExcelA=Create OLEObject
Long ll_error
Ll_error=OLE_ExcelA. ConnectToNewObject (" "excel Application" ")
OLE_ExcelA. Visible=true "
"OLE_ExcelB. Workbooks. Open (sle_eb. Text)
Mle_2. Text=mle_2. Text + 'successful open' + sle_eb. Text + char (13) + char (10)
Ll_sumSheet=OLE_ExcelB. Worksheets. Count
If ll_sumsheet & lt; 1 Then
OLE_ExcelB. Quit ()
OLE_ExcelB. Workbooks. Close
OLE_ExcelB. DisConnectObject ()
Destroy OLE_ExcelB;"
"Ll_rowA=OLE_ExcelA. ActiveWorkbook. Activesheet. UsedRange. Rows. Count
Ll_columnA=OLE_ExcelA. ActiveWorkbook. Activesheet. UsedRange. Columns. The Count
"nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related