pb生成excel的两种方式

以下两种方式可以生成excel文件(xls,或xlsx)
- 使用datawindow
- 使用Excel的OLE控件

1.使用datawindow的saveas

dw_savetoexcel.SaveAs("c:\test1.xls", Excel!, true) //2003
dw_savetoexcel.SaveAs("c:\test2.xls", Excel8!, true) //2003
dw_savetoexcel.SaveAs("c:\test3.xlsx", XLSX!, true)  //2007

如果想保存为2007格式,则需要pb12以上,并且安装.NET 3.0或者更高。
API [ http://infocenter-archive.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc37783.1200/html/dwref/CCJBDCBHdw.htm ]
integer dwcontrol.SaveAs ( { string filename, saveastype saveastype,
boolean colheading { , encoding encoding } } )
saveastype [ http://infocenter-archive.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc37783.1200/html/dwref/BABDIJBH.htm ]

2.使用Excel的OLE控件

使用OLE控件意味着操作Excel的语法来自VBA,可以通过搜索”vba+excel”来搜索相关知识。

//在pb中创建Excel的OLE控件
OLEObject ObjExcel
//检查是否可以载入excel
ObjExcel = CREATE OLEObject
result = ObjExcel.ConnectToNewObject( "excel.application" )
if result < 0 then
    messagebox("连接excel失败,检查你的系统是否安装了office",string(result))
    Destroy ObjExcel
    GarbageCollect()
    return
end if 
ObjExcel.displayalerts=false   //关闭提示框
//退出Excel
ObjExcel.displayalerts=false
ObjExcel.Application.Workbooks.close()
ObjExcel.Application.quit() 
ObjExcel.quit()
ObjExcel.DisconnectObject()
DESTROY ObjExcel
//保存
ObjExcel.activeworkbook.saveas("c:\test1.xls",3) //2003
ObjExcel.activeworkbook.saveas("c:\test2.xlsx",51) //2007

API [ https://msdn.microsoft.com/en-us/vba/excel-vba/articles/workbook-saveas-method-excel ]
SaveAs( FileName , FileFormat , Password , WriteResPassword , ReadOnlyRecommended , CreateBackup , AccessMode , ConflictResolution , AddToMru , TextCodepage , TextVisualLayout , Local )

FileFormat [ https://msdn.microsoft.com/en-us/vba/excel-vba/articles/xlfileformat-enumeration-excel ]

值得注意的是ABAP的”cl_fdt_xl_spreadsheet”类,只能读取采用 ObjExcel.activeworkbook.saveas(“c:\test2.xlsx”,51) 方式生成的excel2007文件。

你可能感兴趣的:(总结,pb,excel,pb,abap,ole,vba)