CSDN博客

img niuyonggang

pb 如何从excel中导入数据

发表于2005/4/7 10:07:00  2840人阅读

分类: powerbuiler

第一种方法

OLEObject ExcelServer
long excelok
string str_savename
ExcelServer = CREATE OLEObject
ExcelOK = ExcelServer.ConnectToNewObject( "excel.application" )
if excelok < 0 then
messagebox("连接excel失败,检查你的系统是否安装了office",string(excelok))
return false
else
ExcelServer.Workbooks.Open(str_filename)
str_savename="c:/temp.txt"
excelserver.activeworkbook.saveas(str_savename,3)
excelserver.displayalerts=false
ExcelServer.quit()
   ExcelServer.DisconnectObject()
   DESTROY ExcelServer
dw_acton.ImportFile(str_savename)
filedelete(str_savename)
   return true
end if

第二种方法

string ls_title, ls_str

OLEObject xl, xlApp, xlbook, xlsheet
xl = Create OLEObject
st_stat.text = '正在连接...'
int ret = 0
ret = xl.ConnectToObject(is_path, "Excel.Sheet" )  //打开一个已存在的工作表
if ret < 0 then
messagebox("提示","不能打开所选的文件,可能是已被别人打开!")
destroy xl
return
end if

xlApp = xl.Application
xlbook = xlapp.Workbooks[1]
xlsheet = xlbook.Worksheets[1]
////////
//////
//////
////////以下是读数据部分, 首先判断有几行数据,前面几个都为空时则数据结束
long ll_i,ll_row,ll_ll
int li_start //从第几行开始是真正的数据

li_start = 4 //从第n行开始是真正的数据
//
do while ((ll_ll < 5) and (ll_i < 3000))
ll_i += 1
ls_str = xlsheet.cells[ ll_i,4 ].text //货物名称
ll_row = ll_i
if ls_str = "" then
ll_ll = 1
ls_str = xlsheet.cells[ ll_i,5 ].text
if ls_str = "" then
ll_ll += 1
ls_str = xlsheet.cells[ ll_i,6 ].text
if ls_str = "" then
ll_ll += 1
ls_str = xlsheet.cells[ ll_i+1,4 ].text
if ls_str = "" then
   ll_ll += 1
ls_str = xlsheet.cells[ ll_i+2, 4 ].text
if ls_str = "" then
ll_ll += 1
end if
end if
end if
end if
end if
loop
//
if ll_row > 2800 then
messagebox("错误","行数太多, 不能导入!")
goto l_exit
end if
//一共有ll_row - start 行
////
//ls_title = xlsheet.cells[1,1].text   //标题, 这里可能有比较
////
ls_str = xlsheet.cells[1,2].text //合同号
if ls_str <> is_contractcode then
messagebox( "导入出错","合同号不符,请检查[1,2]单元格" )
st_stat.text = "导入失败, 请退出重来!"
goto l_exit
end if
////
//ls_str = xlsheet.cells[2,2].text //合同名
//if ls_str <> is_contractname then
// messagebox( "导入出错","合同名不符,请检查[2,2]单元格" )
// st_stat.text = "导入失败, 请退出重来!"
// goto l_exit
//end if
////
////开始导入真正的数据
string ls_temp,ls_find
long l_count,l_temp

if (ll_row - 1) = li_start then
messagebox( "提示","没有数据可导!请检查EXCEL表格后,重来!" )
return
end if

for ll_i = li_start to ll_row - 1
st_stat.text = "正在导入第 " + string( ll_i - li_start + 1 ) + " 行," + '共有'+ string( ll_row - li_start) + '行'
hpb_1.position = (( ll_i - li_start + 1 ) * 100/(ll_row - li_start))
l_count = dw_2.insertrow( 0 )
dw_2.setrow( l_count )
dw_2.object.f_id[ l_count ] = gf_get_max( 't_goodslist' ) //ID
dw_2.object.f_contractid[ l_count ] = il_contractid //合同ID
//从文件导入部分
dw_2.object.f_period[ l_count ] =      xlsheet.cells[ ll_i, 1].text //期别
dw_2.object.f_mysys[ l_count ]     =   xlsheet.cells[ ll_i, 2].text //系统码
dw_2.object.f_goodscode[ l_count ] = xlsheet.cells[ ll_i, 3].text //货物编码
dw_2.object.f_goodsname[ l_count ] = xlsheet.cells[ ll_i, 4].text //货物名称
dw_2.object.f_specif[ l_count ]    = xlsheet.cells[ ll_i, 5].text //货物规格
dw_2.object.f_parameter[ l_count]  =   xlsheet.cells[ ll_i, 6].text //主要技术参数
dw_2.object.f_factory[ l_count ]   =   xlsheet.cells[ ll_i, 7].text //制造厂

dw_2.object.f_goodstype[ l_count ] = xlsheet.cells[ ll_i, 8].text //货物类别
dw_2.object.f_unit[ l_count ] = xlsheet.cells[ ll_i, 9].text //单位

//数量
ls_str = xlsheet.cells[ ll_i, 10].text
if not isnumber( ls_str ) and ls_str <> '' then
messagebox("错误","第["+ string( ll_i) + ',10]单元格必须是数字!')
goto l_exit
end if
dw_2.object.f_quant[ l_count ] = dec(ls_str ) //数量
//出厂价
ls_str = xlsheet.cells[ ll_i, 11].text
if not isnumber( ls_str ) and ls_str<>'' then
messagebox("错误","第["+ string( ll_i) + ',11]单元格必须是数字!')
goto l_exit
end if
dw_2.object.f_outprice[ l_count ] = dec(ls_str ) //出厂价
//运输价
ls_str = xlsheet.cells[ ll_i, 12].text
if not isnumber( ls_str )  and ls_str<>'' then
messagebox("错误","第["+ string( ll_i) + ',12]单元格必须是数字!')
goto l_exit
end if
dw_2.object.f_transprice[ l_count ] = dec(ls_str ) //运输价
//保险价
ls_str = xlsheet.cells[ ll_i, 13].text
if not isnumber( ls_str ) and ls_str<>'' then
messagebox("错误","第["+ string( ll_i) + ',13]单元格必须是数字!')
goto l_exit
end if
dw_2.object.f_guaranteep[ l_count ] = dec(ls_str ) //保险价
//其它价
ls_str = xlsheet.cells[ ll_i, 14].text
if not isnumber( ls_str )  and ls_str<>'' then
messagebox("错误","第["+ string( ll_i) + ',14]单元格必须是数字!')
goto l_exit
end if
dw_2.object.f_otherprice[ l_count ] = dec(ls_str ) //其它价
//综合单价
ls_str = xlsheet.cells[ ll_i, 15].text
if not isnumber( ls_str ) and ls_str <> '' then
messagebox("错误","第["+ string( ll_i) + ',15]单元格必须是数字!')
goto l_exit
end if
dw_2.object.f_unitprice[ l_count ] = dec(ls_str ) //综合单价
//外币总价
ls_str = xlsheet.cells[ ll_i, 16].text
if not isnumber( ls_str ) and ls_str <> '' then
messagebox("错误","第["+ string( ll_i) + ',16]单元格必须是数字!')
goto l_exit
end if
dw_2.object.f_totalprice[ l_count ] = dec(ls_str ) //外币总价
dw_2.object.f_currency[ l_count ] = xlsheet.cells[ ll_i, 17].text //币种
//汇率
ls_str = xlsheet.cells[ ll_i, 18].text
if not isnumber( ls_str ) and ls_str <> '' then
messagebox("错误","第["+ string( ll_i) + ',18]单元格必须是数字!')
goto l_exit
end if
dw_2.object.f_exchange[ l_count ] = dec(ls_str ) //汇率
//汇率日期
ls_str = xlsheet.cells[ ll_i, 19].text
if not isdate( ls_str ) and ls_str <> '' then
messagebox("错误","第["+ string( ll_i) + ',19]单元格必须是日期!')
goto l_exit
end if
dw_2.object.f_exchangedate[ l_count ] = datetime(date(ls_str )) //汇率日期
//人民币单价
ls_str = xlsheet.cells[ ll_i, 20].text
if not isnumber( ls_str ) and ls_str <> '' then
messagebox("错误","第["+ string( ll_i) + ',20]单元格必须是数字!')
goto l_exit
end if
dw_2.object.f_rmbunitprice[ l_count ] = dec(ls_str ) //人民币单价
//人民币总价
ls_str = xlsheet.cells[ ll_i, 21].text
if not isnumber( ls_str ) and ls_str <> '' then
messagebox("错误","第["+ string( ll_i) + ',21]单元格必须是数字!')
goto l_exit
end if
dw_2.object.f_rmbtotalprice[ l_count ] = dec(ls_str ) //人民币总价
//开始日期
ls_str = xlsheet.cells[ ll_i, 22].text
if not isdate( ls_str ) and ls_str <> '' then
messagebox("错误","第["+ string( ll_i) + ',22]单元格必须是日期!')
goto l_exit
end if
dw_2.object.f_startdate[ l_count ] = datetime(date(ls_str )) //开始日期
//结束日期
ls_str = xlsheet.cells[ ll_i, 23].text
if not isdate( ls_str ) and ls_str <> ''then
messagebox("错误","第["+ string( ll_i) + ',23]单元格必须是日期!')
goto l_exit
end if
dw_2.object.f_enddate[ l_count ] = datetime(date(ls_str )) //结束日期

dw_2.object.f_station[l_count] = xlsheet.cells[ ll_i,24].text //站点
dw_2.object.f_place[l_count] = xlsheet.cells[ ll_i,25].text //安装位置
dw_2.object.f_gaisuan[l_count]= xlsheet.cells[ ll_i,26].text //概算码
dw_2.object.f_memo[l_count]= xlsheet.cells[ ll_i,27].text //备注

//其它信息
dw_2.object.f_stat[l_count] = 0
dw_2.object.f_compquant1[l_count] = 0
dw_2.object.f_compquant2[l_count] = 0
dw_2.object.f_inquant1[l_count] = 0
dw_2.object.f_inquant2[l_count] = 0
dw_2.object.f_payquant[ l_count ] = 0
dw_2.object.f_changestat[ l_count ] = 0
dw_2.object.f_changecount[ l_count ] = 0


next

st_stat.text = '导入成功!请选择[保存]或[取消]。 保存--此次导入数据将提交到数据库中,取消--则不提交!'
cb_ok.of_setenabled( true )

////
//////有错退出, 主要是要释放内存
l_exit:
xl.close()
xl.DisConnectObject()
Destroy xl
//
//return true

//以上的程序是导入某一合同的供货清单其中某一列不能为空,判断文件是否结束的写法有点烦。

0 0

相关博文

我的热门文章

img
取 消
img