PB 导入和导出Excel

本文介绍使用PowerBuilder进行Excel文件的导入与导出操作,包括读取Excel数据到DataWindow及将DataWindow数据导出至Excel并替换表头。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

导入:

<span style="font-size:18px;">Stringls_path,ls_name
ls_path ="C:\Users\hzt\Desktop\test.xls"
dw_1.Reset()//clean DW
String a[1000,1000]
Integer i,j,li_ret
li_ret =GetFileopenName('选择XLS',ls_path,ls_name,"XLS","EXCEL (*.XLS),*.XLS")
if li_ret <>1 then return
OLEObjectExcelServer
Int li_ConnectErr
ExcelServer =Create OLEObject
li_ConnectErr =ExcelServer.ConnectToNewObject( "excel.application" )
If li_ConnectErr< 0 Then
    Choose Case li_ConnectErr
        Case -1
            MessageBox('错误提示','无效的调用')
        Case -2
            MessageBox('错误提示','类名没发现')
        Case -3
            MessageBox('错误提示','对象不能创建')
        Case -4
            MessageBox('错误提示','文件不能连接')
        Case -5
            MessageBox('错误提示','不能连接现在的对象')
        Case -6
            MessageBox('错误提示','文件无效')
        Case -7
            MessageBox("错误提示","文件不存在或已经打开")
        Case -8
            MessageBox("错误提示","服务器不能装载选择的文件")
        Case -9
            MessageBox("错误提示","其他错误")
    End Choose
    Return
End If
ExcelServer.Workbooks.Open(ls_path,0,False)
//对XLS文件进行了改动以后,在关闭该文件时是否需要向用户提出警告。
ExcelServer.Application.DisplayAlerts= False
//选择sheets表
//ExcelServer.activeworkbook.worksheets("sheet2").Select()

ExcelServer.ActiveSheet.cells.Copy
dw_1.ImportClipboard(2)//导入数据需要标题  
ExcelServer.Application.activeworkbook.Close(False)
ExcelServer.Application.quit()
ExcelServer.DisconnectObject()</span>

导出

功能 :将数据窗口数据导出EXCEL文件,并将EXCEL文件默认英文标题替换成中文。

 参数:datawindow datawin,为用户要导出数据窗口的数据窗口控件名

 返回值:integer 1,success;-1,error

 流程描述:先用saveasAscii()倒出为excel文件,再替换表头为中文名

<span style="font-size:18px;">integerli_rtn,ii,li_asc
stringls_name,ls_pathname
boolean lb_exist
ifdatawin.RowCount()<1 then
 MessageBox("提示信息","请先检索数据再导出至Excel!")
 return -1 //error
end if
li_rtn=GetFileSaveName("保存文件",ls_pathname,ls_name,"xls","Excel文件(*.xls),*.xls")
 
if li_rtn=1 then
 lb_exist = FileExists(ls_pathname)
 IF lb_exist THEN
  li_rtn = MessageBox("保存", ls_pathname+"已经存在,是否覆盖?",Exclamation!, YesNo!)
 end if
 if li_rtn=1 then
 //当文件存在用户选择覆盖,或是文件本就不存在时。注意变量li_rtn
  li_rtn=datawin.SaveAsAscii(ls_pathname)
  if li_rtn=1 then
//   MessageBox("提示信息","导出数据成功!")  
  else
   MessageBox("错误信息","导出数据失败!")
   return -1 //error
  end if
 else
  return -1 //error
 end if
else
 return -1
end if
 
/**********以下程序将导出的EXCEL英文标题替换为汉字*********/
long numcols ,numrows , c, r
OLEObject xlapp ,xlsub
int ret
numcols =long(datawin.Object.DataWindow.Column.Count)
numrows =datawin.RowCount()
 
// 产生oleobject的实例
xlApp = CreateOLEObject
 
//连接ole对象
ret =xlApp.ConnectToNewObject( "Excel.Sheet" )
if ret < 0  then
 MessageBox("连接失败!","连接到EXCEL失败,请确认您的系统是否已经安装EXCEL!~r~n"&
 +"错误代码:"+string(ret))
    return -1
end if
// 打开EXCEL文件
xlApp.Application.Workbooks.Open(ls_pathname)
使文件可见
//xlApp.Application.Visible= true
 
// 得到活动工作表的引用,改善程序性能
xlsub =xlapp.Application.ActiveWorkbook.Worksheets[1]
stringls_colname,ls_text,ls_modistr,ls_col
//取字段名更改为对应的文本text值
FOR c=1 to numcols
 ls_col="#"+string(c)+".name"
 ls_colname=datawin.describe(ls_col)
 ls_modistr=ls_colname+"_t.text"
 ls_text=datawin.describe(ls_modistr)
 xlsub.cells[1,c]=ls_text
NEXT
 
xlApp.DisConnectObject()
Destroy xlapp
MessageBox("提示信息","导出数据成功!")
return 1 //success</span>

参考地址:http://blog.youkuaiyun.com/error/444.html?from=http%3a%2f%2fblog.youkuaiyun.com%2fhappyflystone%2farticle%2fdetails%2f2923951

                     http://liuwenzhen007.blog.163.com/blog/static/3690120920108204917677/

评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值