问题
I have this code for buttonclicked event to load excel and text file to powerbuilder and the excel data is not copied i.e ll_return_xls <= 0 and thus when i want to load it to db i will get error since it has no value or data in it...The problem here is i get the data from text file but not from the excel file...Any help will be appreciated
long ll_return
long ll_return_xls
OLEObject excel
IF rb_file.checked then
IF dwo.Name = "b_browse_meters" THEN // Browse Button Clicked for Text
ll_return = GetFileOpenName ( "Select Meter File", is_fullname, is_filename, "TXT", &
"Text Files (*.txt), *.txt" )
if ll_return < 1 then
gnv_msg.f_mensaje("EX28", "", "", OK!)
return
end if
END IF // Browse Button Clicked
END IF
IF dwo.Name ="b_browse_meters_xls" THEN // Browse Button Clicked for Excel
ll_return_xls = GetFileOpenName ( "Select Meter File", is_fullname, is_filename, "XLS", &
"Excel Files (*.xls), *.xls" )
if ll_return_xls < 1 then
gnv_msg.f_mensaje("EX28", "", "", OK!)
return
end if
excel = create oleobject
excel.ConnectToNewObject( "excel.application" )
excel.visible = false
excel.workbooks.open( is_fullname)
excel.Range("A1:A2").Select
excel.ActiveCell.CurrentRegion.Select()
ClipBoard('')
excel.Selection.Copy()
ClipBoard('')
excel.ActiveWorkbook.Close
excel.Disconnectobject()
Destroy excel
END IF // Browse Button Clicked
And Function to load it the text and excel data
long ll_return, ll_arr_len, ll_loop, ll_indx, ll_pass = 0
integer li_rc, li_file_nbr, li_fr_rc //File Read Return Code
string ls_file, ls_line, ls_msg
string lsa_meters[], lsa_meter_pass[]
string ls_meters
long ll_total_valid, ll_total_aparatos
SetPointer(HourGlass!)
if IsValid(w_progbar) then Close(w_progbar)
openwithparm(w_progbar,"Uploading file data ...")
w_progbar.uo_progress_bar.set_position(10)
li_rc = SUCCESS
ll_total_aparatos = 0
ll_total_valid = 0
// Open file
if li_rc = SUCCESS then
ls_file = is_fullname
li_file_nbr = FileOpen(ls_file, LineMode!)
if IsNull(li_file_nbr) or li_file_nbr < 1 then
li_rc = NO_ACTION
end if
end if
// Read file, store serial numbers into array lsa_meters[]
if li_rc = SUCCESS then // File Opened
ll_indx = 0
do while ll_indx > -1 and li_rc = SUCCESS
li_fr_rc = FileRead(li_file_nbr, ls_line)
choose case li_fr_rc
case is > 0
ll_indx ++
lsa_meters[ll_indx] = ls_line
case 0, -100
ll_indx = -1
case -1
li_rc = FAILURE
gnv_msg.f_mensaje("EX21", "", "", OK!)
end choose
loop
if li_rc = SUCCESS then // File Read OK
ll_arr_len = UpperBound(lsa_meters)
if ll_arr_len > 0 then // Data found in file
for ll_loop = 1 to ll_arr_len step 1
if ib_meter then
ll_return = wf_luhn_check(lsa_meters[ll_loop])
ll_total_valid ++
else
if Len(lsa_meters[ll_loop]) >= 9 then
ll_return = 1
ll_total_valid ++
else
ll_return = -1
end if
end if
if ll_return = 1 then
if wf_aparatos_check(lsa_meters[ll_loop]) <= 0 then
ll_return = 1
else
ll_return = -1
ll_total_aparatos ++
end if
end if
if ll_return = 1 then
ll_pass++
lsa_meter_pass[ll_pass] = lsa_meters[ll_loop]
ls_meters = ls_meters + lsa_meters[ll_loop] + isk_TAB_PB
end if
w_progbar.uo_progress_bar.set_position(50 + ((ll_loop / ll_arr_len) * 40))
next
ll_arr_len = UpperBound(lsa_meter_pass)
if ll_arr_len > 0 then
dw_datos_ingreso.Object.num_ini[1] = lsa_meter_pass[1]
dw_datos_ingreso.Object.num_fin[1] = lsa_meter_pass[ll_arr_len]
is_meters_passed = ls_meters // Concatenated string of meter serial no.
il_apa_filemeters_count = ll_arr_len // Number of meters
else
li_rc = FAILURE
if ib_meter then
gnv_msg.f_mensaje("EX27", "", "", OK!)
else
gnv_msg.f_mensaje("EX22", "", "", OK!)
end if
if ll_total_valid = ll_total_aparatos then
gnv_msg.f_mensaje("EX23", "", "", OK!)
end if
end if
else
li_rc = FAILURE
gnv_msg.f_mensaje("EX24", "", "", OK!)
end if // Data found in file
end if // File Read OK
end if // File Opened
w_progbar.uo_progress_bar.set_position(100)
Close(w_progbar)
//Clean up
if li_file_nbr > 0 then
if FileClose(li_file_nbr) = FAILURE then
li_rc = FAILURE
gnv_msg.f_mensaje("EX25", "", "", OK!)
end if
end if
if ll_total_aparatos > 0 then
gnv_msg.f_mensaje("EW140", String(ll_total_aparatos) , "", OK!)
end if
if li_rc = FAILURE then
gnv_msg.f_mensaje("EX26", "", "", OK!)
RETURN FALSE
end if
RETURN TRUE
回答1:
You can use the ODBC driver directly to the Excel file, and then build a Pipeline object that moves the data from the spreadsheet directly into the database. Don't write code you don't need to.
来源:https://stackoverflow.com/questions/24488444/load-excel-file-to-powerbuilder