PB与EXCEL.docx
《PB与EXCEL.docx》由会员分享,可在线阅读,更多相关《PB与EXCEL.docx(19页珍藏版)》请在冰豆网上搜索。
PB与EXCEL
PB与EXCEL
方法一:
OLEObjectExcelServer
integerExcelOK
ExcelServer=CREATEOLEObject
ExcelOK=ExcelServer.ConnectToNewObject("excel.application")
ExcelServer.Workbooks.Open("c:
\book.xls")
ExcelServer.ActiveWorkBook.Sheets("Sheet2").Select
ExcelServer.ActiveSheet.Cells(1,1).value="书名"
ExcelServer.Application.Visible=TRUE
方法二:
先启动excel文件
longhandle_1
handle_1=OpenChannel("Excel","c:
\book.xls")
//建立与EXCEL的通道
setremote("r1c1","书号","excel","c:
\book.xls")
setremote("r1c2","书名","excel","c:
\book.xls")
//更改某一行或某一列的值,同时关闭通道
closechannel(handle_1,handle(parent))
--------------------------------------------------------------
问题内容:
将EXCEL文件导入DW数据窗口的方法
原讨论链接:
所属论坛:
DataWindow审核组:
PowerBuilder
提问者:
yjd2001解决者:
Capricor
感谢:
fibbery、lzheng2001、sun1976、mittee、guoleilele、amekoxiao、workhand
关键字:
答案:
---------------------------------------------------------------
导入的电子表格请符合下面的三种情况
1.是从本系统导出的模板
2.在excel另存为csv(逗号分隔)类型的文件;
3.在excel另存为文本文件(制表符分隔)类型的文件
//最后都用到数据窗口的importfile()
//还有一种方法就是对excel文件一个一个单元格进行读取,
//问题是无法知道excel文件行数和列数
---------------------------------------------------------------
把DW导出的代码为:
constantstringls_filename="d:
\file.xls"
longnumcols,numrows,c,r
OLEObjectxlapp,xlsub
intret
numcols=long(dw_1.Object.DataWindow.Column.Count)
numrows=dw_1.RowCount()
xlApp=CreateOLEObject
ret=xlApp.ConnectToNewObject("Excel.Sheet")
ifret<0then
MessageBox("ConnecttoExcelFailed!
",string(ret))
return
endif
try
xlsub=xlApp.Application.Workbooks.Add()
xlsub=xlapp.Application.ActiveWorkbook.Worksheets[1]
xlApp.Application.Visible=true
stringls_name
forc=1tonumcols//写标题
ls_name=dw_1.describe("#"+string(c)+".name")
xlsub.cells[1,c]=ls_name
next
forr=1tonumrows//写行
forc=1tonumcols
xlsub.cells[r+1,c]=dw_1.object.data[r,c]
next
next
xlsub.saveas(ls_filename)
xlApp.Application.Workbooks.close()
catch(runtimeerrorre)
MessageBox(string(re.number),re.getmessage())
endtry
xlApp.DisConnectObject()
Destroyxlapp
***************************************************************
用ole把excel读进来,然后一个cell一个cell的取数,放到dw里,然后可以操作或者update都可以,一下代码:
//将EXCEL的数据导入到临时表中
uo_file_functionuo_1
uo_1=createuo_file_function
ifuo_1.closeexcel()<>0then
DESTROYuo_1
Return
endif
Destroyuo_1
luo_excel=createoleobject
IFwf_inputbom()=FalseThen
SetCurrentDirectoryA(is_syspath)///将路径重新设置
DestroyLuo_excel
Return
EndIF
SetCurrentDirectoryA(is_syspath)///将路径重新设置
DestroyLuo_excel
其中wf_inputbom():
Longvalue,Ll_col,Ll_01,Ll_row
StringLs_path,Ls_filename,Ls_cx
Is_syspath=space(255)
GetCurrentDirectoryA(255,Is_syspath)
value=GetFileopenname("请选择要导入BOM的EXECL类型文件!
",ls_path,ls_filename,'xls','EXECL文件(*.xls),*.xls')
is_filename=mid(ls_filename,1,len(ls_filename)-4)
ifvalue<>1then////没取到、或者取消返回
Returnfalse
endif
ifluo_excel.connecttoobject("","Excel.Application")<>0then
ifluo_excel.ConnectToNewObject("Excel.Application")<>0then
messagebox('error','连接excel失败请稍后再试!
')
returnfalse
endif
endif
SetPointer(HourGlass!
)
luo_excel.workbooks.open(ls_path)
luo_sub=luo_excel.application.workbooks(ls_filename).worksheets
(1)
///////////取出数据区域
ll_col=2
dowhile1=1
luo_sub.cells(ll_col,1).copy()
tab_1.tabpage_1.sle_2.paste()
ls_cx=trim(tab_1.tabpage_1.sle_2.text)
iftrim(tab_1.tabpage_1.sle_2.text)=""orisnull(tab_1.tabpage_1.sle_2.text)then
exit
endif
tab_1.tabpage_1.sle_2.text=''
ll_col++
loop
//////////Ll_col记录总的行数
ifll_col<2thengotoout//空的Excel,
//删除PIM_BOM表中原有数据
deletefrominv_usedquota2;
tab_1.tabpage_1.dw_8.reset()
tab_1.tabpage_1.dw_8.retrieve()
////////////取数
ll_01=2
//从第三行开始取数
dowhilell_01<=ll_col-1
ll_row=tab_1.tabpage_1.dw_8.insertrow(0)
tab_1.tabpage_1.sle_2.text=''
luo_sub.cells(ll_01,1).copy()
tab_1.tabpage_1.sle_2.paste()
ls_cx=trim(tab_1.tabpage_1.sle_2.text)
ifleft(ls_cx,1)='"'then
ls_cx=right(ls_cx,len(ls_cx)-1)
endif
tab_1.tabpage_1.sle_2.text=''
tab_1.tabpage_1.dw_8.setitem(ll_row,'wlh',ls_cx)
luo_sub.cells(ll_01,2).copy()
tab_1.tabpage_1.sle_2.paste()
ls_cx=trim(tab_1.tabpage_1.sle_2.text)
tab_1.tabpage_1.sle_2.text=''
tab_1.tabpage_1.dw_8.setitem(ll_row,'zwlh',ls_cx)
luo_sub.cells(ll_01,5).copy()
tab_1.tabpage_1.sle_2.paste()
ls_cx=trim(tab_1.tabpage_1.sle_2.text)
ifleft(ls_cx,1)='"'then
ls_cx=right(ls_cx,len(ls_cx)-1)
endif
tab_1.tabpage_1.sle_2.text=''
tab_1.tabpage_1.dw_8.setitem(ll_row,'de',dec(ls_cx))
luo_sub.cells(ll_01,9).copy()
tab_1.tabpage_1.sle_2.paste()
ls_cx=trim(tab_1.tabpage_1.sle_2.text)
ifleft(ls_cx,1)='T'orleft(ls_cx,1)='t'then
ls_cx='1'
************************************************************
f_excel_to_dw專業2008-11-0708:
41:
52阅读9评论0字号:
大中小
/*--------------------------------------------------------------------------------------------------------------------------------*/
/*函數:
f_excel_to_dw*/
/*參數:
as_dw(datawindow)*/
/*返回值:
integer*/
/*描述:
將數據從Excel導入到Datawindow中*/
/*撰寫:
Hushuiqiu2008/10/30*/
/*---------------------------------------------------------------------------------------------------------------------------------*/
stringstr_savename,named,s_grxh
intexcelok,li_net
longli_count,i
OleobjectExcelserver
Excelserver=createOleobject
Excelok=excelserver.Connecttonewobject("excel.application")
//checkreturnvaluemakesuresuccesstoconnecttoExcel
IFExcelok<>0THEN
Messagebox("","FaildconnecttoExcel,pleasemakesureyouhavesetuptheexcel")
RETURN-1
ENDIF
li_net=GetFileOpenName("Choosefiles",str_savename,named,"xls","Excelfile(*.xls),*.xls")
IFli_net>0THEN
IFstr_savename=""THENRETURN-1
as_dw.settransobject(sqlca)
as_dw.reset()
Excelserver.workbooks.open(str_savename)
Excelserver.activesheet.cells.copy
li_count=as_dw.importclipboard
(2)//importdata
Clipboard("")
Excelserver.quit()
Excelserver.disconnectobject()
Destroyexcelserver
Return1
Else
Return-1
ENDIF
************************************************************
回复于:
2003-07-2410:
42:
36//先启动excel文件
longhandle_1
handle_1=OpenChannel("Excel","c:
\myexlel.xls")
//开始读取数据!
stringls_name
integerli_rtn
li_rtn=getremote("r1c1",ls_name,handle_1)//r1表示行1,c1表示列1
//将行1列1的值赋给变量ls_name
ifli_rtn=1then//判断操作是否成功
messagebox("",ls_name)//成功
else
messagebox(ls_name,string(li_rtn))
endif
dw_1.SetItem(行,列,ls_name)//把内容写到数据窗口
//---数据读取结束(这段内容可连续重复也可用循环语句读出数据到数组!
)
CloseChannel(handle_1)//关闭excel
这里给出从DATAWINDOW到EXECL导出数据的方法,如果是从EXECL到DATAWINDOW则反过来,加以修改就可以啦。
stringls_initfile,ls_name,is_server,ls_string
stringls_path
integerli_file,li_ret,li_Block
stringls_1name,ls_2name
stringls_mc,ls_zch
longll_bl,i,ll_count
dec{2}dec_bl
OLEObjectlo_xlapp,lo_xlsub
uo_statusbar.of_settext(0,0,"正在导出报表文件,请稍候...")
ls_path=g_app.Localpath+"\xls\aa.xls"
uo_statusbar.of_settext(0,0,"正在打开Excel文件...")
lo_xlapp=CreateOLEObject
li_ret=lo_xlapp.ConnectToNewObject("Excel.Sheet")
lo_xlapp.Application.Workbooks.Open("c:
\power\xls\aa.xls")
stringls_MonthNow,ls_DateNow,ls_cbsj
ls_MonthNow=String(today(),'yyyymm')
ls_DateNow=String(today(),'yyyymmdd')
lo_xlapp.Application.Visible=false
lo_xlsub=lo_xlapp.Application.ActiveWorkbook.Worksheets[1]
li_block=0
dec{2}dec_da1,dec_da2,dec_cbl,dec_shdl,dec_shl
dec{2}dec_total1,dec_total2,dec_total3,dec_max1,dec_min1,dec_num1
stringstr_cbsj,str_ly
lo_xlsub.cells[3,7]=Left(ls_DateNow,4)+'.'+mid(ls_DateNow,5,2)+'.'+Right(ls_DateNow,2)
ll_count=dw_view.rowcount()
fori=1toll_count
str_cbsj=dw_view.object.cbsj[i]
lo_xlsub.cells[i+4,1]=str_cbsj
dec_da1=dw_view.object.data1[i]
lo_xlsub.cells[i+4,2]=dec_da1
dec_da2=dw_view.object.data2[i]
lo_xlsub.cells[i+4,3]=dec_da2
dec_shdl=dw_view.object.shdl[i]
lo_xlsub.cells[i+4,4]=dec_shdl
dec_shl=dw_view.object.shl[i]
lo_xlsub.cells[i+4,5]=dec_shl
str_ly=dw_view.object.ly[i]
lo_xlsub.cells[i+4,6]=str_ly
dec_cbl=dw_view.object.cbl[i]
lo_xlsub.cells[i+4,7]=dec_cbl
endfor
//总表电量,最大值,最小值,次数最多值,合计
dec_total1=dw_view.object.Primary.Current.total1
lo_xlsub.cells[ll_count+2,2]=dec_total1
dec_total2=dw_view.object.Primary.Current.total2
lo_xlsub.cells[ll_count+2,3]=dec_total2
dec_total3=dw_view.object.Primary.Current.total3
lo_xlsub.cells[ll_count+2,6]=dec_total3
lo_xlapp.Application.ActiveWorkbook.Save()
lo_xlapp.Application.quit()
lo_xlApp.DisConnectObject()
Destroylo_xlapp
uo_statusbar.of_settext(0,0,"导出完成。
")
***************************************************************
EXCEL文件导入DATAWINDOWPowerBuilder2008-01-2121:
12:
29阅读1评论0字号:
大中小
////////////////////////////////////////////////////////////
//函数名称:
GF_EXCEL_TO_DATAWINDOW
//函数功能:
EXCEL文件导入DATAWINDOW
//参数类型:
DATAWINDOW
//参数名称:
dw
//RETURN:
1SUCCEED-1ERROR
//DATE:
20043.1
/////////////////////////////////////////////////////////////
stringstr_savename,named,s_grxh;
intexcelok,li_net;
longli_count;
oleobjectexcelserver;
excelserver=createoleobject;
excelok=excelserver.connecttonewobject("excel.application");
ifexcelok<>0then
messagebox("信息提示","连接EXCEL失败,请检查计算机中EXCEL是否工作正常");
return-1;
endif
li_net=GetFileOpenName("选择文件",str_savename,named,"xls","Excel文件(*.xls),*.xls");
ifli_net>0then
ifstr_savename=""then
return-1
endif
dw.reset()
excelserver.workbooks.open(str_savename);
excelserver.activesheet.cells.copy
li_count=dw.importclipboard
(1);
clipboard("");
excelserver.quit();
excelserver.disconnectobject();
destroyexcelserver;
return1;
else
messagebox("信息提示","没有指定导入文件!
");
excelserver.disconnectobject