PB对excel的操作
pb导出excel(直接导出)

基本操作:Ole_object.Workbooks.add//新建一个Excel文件Ole_object.Workbooks.Open("FileNam e")//打开一个已存在Excel文件Ole_object.ActiveWorkBook.Sheets("SheetName").Select//选择文件中一个工作表Ole_object.Application.Run("MacroName")//运行宏Ole_object.Application.Visible=TRUE//Excel文件可见Ole_object.Application.ScreenUpdating=true//设置可见属性②格式设置Ole_object.ActiveSheet.Columns("A:U").AutoFit//列宽自动调整Ole_object.ActiveSheet.Columns("A:Z").ColumnWidth=6.75//列宽Ole_object.ActiveSheet.Rows("1:100").RowHeight=12//行高Ole_object.Application.StandardFont="ArialNarrow";//设置字体Ole_object.Application.StandardFontSize="8"//设置字号Ole_object.ActiveSheet.Font.Size="8"//设置字号Ole_object.ActiveSheet.Font.Bold=True//粗体Ole_object.ActiveSheet.Font.Italic=True//斜体Ole_object.ActiveSheet.Font.Underline=True//下划线Ole_object.ActiveSheet.Font.StrikeThrough=True//删除线Ole_object.ActiveSheet.HorizontalAlignment=3//水平:4靠右;3居中;2靠左Ole_object.ActiveSheet.VerticalAlignment=2//垂直:3靠下;2居中;1靠上Ole_object.ActiveSheet.cells(2,1)=’黑体’//设置字体Ole_object.ActiveSheet.cells(2,1).font.size=25//设置字体大小③工作区域操作:Ole_object.ActiveSheet.Range("A1:Z10").Property=value//设置一个工作区域内的属性值Ole_object.ActiveSheet.Range("A1:Z10").Merge//合并单元格Ole_object.ActiveSheet.Range("A1:Z10").WrapText=False//自动换行禁止Ole_object.ActiveSheet.Range("A1:Z10").Borders(b_type).Weight=i_val//b_type:1左边界;2右边界;3上边界;4下边界;5左上倾斜;6右上倾斜(以上为对单元格的操作)7左边界;8上边界;9下边界;10右边界;11内部垂直边界;12内部水平边界(以上为对区域的操作)//i_val:0无边界线;然后1,2,3。
PB操作Excel文件

PB调用EXCEL的操作不失为一种好的报表输出方式2009-05-15 23:17 928人阅读评论(0) 收藏举报对于较为中国化的报表,虽然后dw这个坚强后盾,但是也难不住繁复疲劳的应对。
不过我们可以知道,数据的输出,都是居于table的栏位或者计算字段。
只是很多统计的模式和展现模式不同。
我在一年前作完一个系统,可是客户几乎隔三差五有新的东东要输出报表。
终于我发现。
让客户先在excel里弄好,然后再写程序来输出也是一个不错的方法。
关键是,客户给的东西不必要再进自己的系统,所以数据不会发生什么问题。
如果本身软件有任何问题,那能从excel里的写的结果很容易看得出来。
虽然写excel表格要花很多时间,但是减少了输入导入和输出的一些麻烦。
转载excel操作大全:powerbuilder操作excel命令大全(例子的有些地方不对!)2006-12-04 16:431.创建Excel对象eole=CREATEOBJEC T(′Excel.application′)2.添加新工作簿eole.Workbooks.add3.设置第3个工作表为激活工作表eole.Worksheets(″sheet3″).Activate4.打开指定工作簿eole.Workbooks.Open(″c:/temp/ll.xls″)5.显示Excel窗口eole.visible=.t.6.更改Excel标题栏eole.Caption=″VFP应用程序调用Microsoft Excel″7.给单元格赋值eole.cells(1,4).value=XM(XM为数据库字段名)8.设置指定列的宽度(单位:字符个数)eole.ActiveSheet.Columns(1).ColumnWidth=59.设置指定行的高度(单位:磅)eole.ActiveSheet.Rows(1).RowHeight=1/0.035(设定行高为1厘米,1磅=0.035厘米)10.在第18行之前插入分页符eole.Worksheets(″Sheet1″).Rows(18).P ageBreak=111.在第4列之前删除分页符eole.ActiveSheet.Columns(4).PageBreak=012.指定边框线宽度(Borders参数如下)ole.ActiveSheet.Range(″b3:d3″).Borders(2).Weight=313.设置四个边框线条的类型eole.ActiveSheet.Range(″b3:d3″).Borders(2).LineStyle=1(其中Borders参数:1-左、2-右、3-顶、4-底、5-斜、6-斜/;LineStyle值:1与7-细实、2-细虚、4-点虚、9-双细实线)14.设置页眉eole.ActiveSheet.PageSetup.CenterHeader=″报表1″15.设置页脚eole.ActiveSheet.PageSetup.CenterFooter=″第&P页″16.设置页眉到顶端边距为2厘米eole.ActiveSheet.PageSetup.HeaderMargin=2/0.03517.设置页脚到底边距为3厘米eole.ActiveSheet.PageSetup.FooterMargin=3/0.03518.设置顶边距为2厘米eole.ActiveSheet.PageSetup.TopMargin=2/0.035 19.设置底边距为4厘米eole.ActiveSheet.PageSetup.BottomMargin=4/0.035 20.设置左边距为2厘米veole.ActiveSheet.PageSetup.LeftMargin=2/0.035 21.设置右边距为2厘米eole.ActiveSheet.PageSetup.RightMargin=2/0.035 22.设置页面水平居中eole.ActiveSheet.PageSetup.CenterHorizontally=.t. 23.设置页面垂直居中eole.ActiveSheet.PageSetup.CenterVertically=.t. 24.设置页面纸张大小(1-窄行8 5 11 39-宽行14 11) eole.ActiveSheet.PageSetup.PaperSize=125.打印单元格网线eole.ActiveSheet.PageSetup.PrintGridlines=.t.26.拷贝整个工作表edRange.Copy27.拷贝指定区域eole.ActiveSheet.Range(″A1:E2″).Copy28.粘贴eole.WorkSheet(″Sheet2″).Range(″A1″).PasteSpecial 29.在第2行之前插入一行eole.ActiveSheet.Rows(2).Insert30.在第2列之前插入一列eole.ActiveSheet.Columns(2).Insert31.设置字体eole.ActiveSheet.Cells(2,1)=″黑体″32.设置字体大小eole.ActiveSheet.Cells(1,1).Font.Size=25 33.设置字体为斜体eole.ActiveSheet.Cells(1,1).Font.Italic=.t. 34.设置整列字体为粗体eole.ActiveSheet.Columns(1).Font.Bold=.t. 35.清除单元格公式eole.ActiveSheet.Cells(1,4).ClearContents 36.打印预览工作表eole.ActiveSheet.PrintPreview37.打印输出工作表eole.ActiveSheet.PrintOut38.工作表另为eole.ActiveWorkbook.SaveAs(″c:/temp/22.xls″) 39.放弃存盘eole.ActiveWorkbook.saved=.t.40.关闭工作簿eole.Workbooks.close41.退出Exceleole.quit42 合并string worksheet,beginRowcol,EndRowColMyOLE.ActiveWorkBook.Sheets(worksheet).Range(BeginRowCol ":" EndRowCol).Select MyOLE.ActiveWorkBook.Sheets(worksheet).Range(BeginRowCol ":" EndRowCol).Merge43 sheet改名MyOLE.ActiveWorkBook.Sheets(olename).selectMyOLE.ActiveWorkBook.Sheets(olename).name=newnameMyOLE=Create OLEObjectConnectErr=MyOLE.ConnectToNewObject ("excel.Application")MyOLE.visible=false//打开指定的XLS文件激活workbooksMyOLE.application.workbooks.Open (FilePath)//对XLS文件进行了改动以后,在关闭该文件时是否需要向用户提出警告。
pb与excel之间导入导出处理

pb与excel之间导入导出处理gf_save_excel_to_txt(as_excel_filename string,as_text_filename string)//把文件名为as_excel_filename的Excel文件另存为文件名为as_text_filename 的Text文件//返回:另存是否成功//删除Text文件If DeleteFile(as_text_filename) Then//删除存在的Text文件成功OLEObject l_olel_ole = Create OLEObject;If l_ole.ConnectToNewObject("Excel.Application") = 0 Then//连接到Excell_ole.Application.DisplayAlerts = False //关闭警告消息对话框,防止退出Excel时提示l_ole.Application.Workbooks.Open(as_excel_filename) //打开Excel文件l_ole.Application.ActiveWorkbook.SaveAs(as_text_filename,-4158) //把打开的Excel文件另存为Text文件,第二参数表示数据以制表符TAB分隔l_ole.Application.Quit() //退出ExcelElseMessagebox(\'错误\',\'无法建立与Excel的连接!\',StopSign!)Destroy l_oleReturn FalseEnd IfDestroy l_oleReturn TrueElse//无法删除Text文件Return FalseEnd If保存excel文件:要想选择目录可以这样string ls_docname, ls_named //文件路径,文件名integer li_rtli_rt = GetFileSaveName("选择保存路径并输入保存文件名" + string(jj),ls_docname, ls_named, &"Excel", &"Excel Files (*.XLS),*.XLS," + &" TXT Files (*.TXT),*.TXT")if li_rt = 1 then//转存dw_x.saveasascii(ls_docname,"~t","") end if。
pb从excel读数据

pb从excel读数据实现通过PB来读取excel中某一行某一列的数据。
我在相关论坛上找到了一个例子,但其中的怎么申明,怎么调用还不太明白,例子如下:读excel的代码:ole1.workbooks.open(ls_pathname,0,0)ole1.visible = true//获取EXCEL单元格数据ls_cell = ole1.workbooks[1].worksheets[1].cells(ll_rowno,ll_colno).value各位可否通过一个实例进行说明呢?谢谢了!NO.1 作者:Hanson_bati_zhu试试看这个是用PB自带的例子改的integer resultOLEObject myoleobjectmyoleobject = CREATE OLEObjectresult = myoleobject.ConnectToObject("现有的EXCEL文件名")IF result = 0 THENMessageBox(,String(myoleobject.application.workbooks(1).worksheets(1).cells(行号,列号).value))END IFmyoleobject.DisconnectObject()DESTROY myoleobjectNO.2 作者:xiaozhigood上面的写的太仓促你可以看这个就可以了//从excel文件获得数据//建立从EXCEL表获取数据的通道OLEObject ole1ole1= CREATE OLEObjectresult=ole1.ConnectTonewObject("Excel.application")if result<>0 thenmessagebox("提示!","连接Excel表出错,请重新选择")returnend if//操作员选择要导入数据的EXCEL文件ll_rtn = getfileopenname(打开文件,ls_pathname,ls_filename,XLS,EXCEL文件(*.xls),*.xls) if ll_rtn<>1 thenmessagebox("提示!","连接Excel表出错,请重新选择")returnend ifole1.workbooks.open(ls_pathname,0,0)ole1.visible = truell_rowno=1 ‘列ll_colno=1 ’行do while ole1.workbooks[1].worksheets[1].cells(ll_rowno,1).value‘判断是否有数据do while ole1.workbooks[1].worksheets[1].cells(ll_rowno,ll_colno).value‘取数据ls_cell = string(ole1.workbooks[1].worksheets[1].cells(ll_rowno,ll_colno).value) s_cpxx[ll_rowno,ll_colno]=ls_cellll_colno=ll_colno+1loopll_rowno=ll_rowno+1loopole1.visible = false‘关闭ole1.workbooks.close()‘销毁destroy ole1。
PB中操作Excel的技巧集

PB中操作Excel的技巧集eole=CREATEOBJECT("Excel.application")2.添加新工作簿eole.Workbooks.add3.设置第3个工作表为激活工作表eole.Worksheets("sheet3").Activate4.打开指定工作簿eole.Workbooks.Open("c:\temp\ll.xls")5.显示Excel窗口eole.visible=True6.更改Excel标题栏eole.Caption=″VFP应用程序调用Microsoft Excel″7.给单元格赋值eole.cells(1,4).value=XM(XM为数据库字段名)8.设置指定列的宽度(单位:字符个数)eole.ActiveSheet.Columns(1).ColumnWidth=59.设置指定行的高度(单位:磅)eole.ActiveSheet.Rows(1).RowHeight=1/0.035 (设定行高为1厘米,1磅=0.035厘米)10.在第18行之前插入分页符e ole.Worksheets(″Sheet1″).Rows(18).PageBreak=111.在第4列之前删除分页符eole.ActiveSheet.Columns(4).PageBreak=012.指定边框线宽度(Borders参数如下)ole.ActiveSheet.Range(″b3:d3″).Borders(2).Weight=313.设置四个边框线条的类型eole.ActiveSheet.Range(″b3:d3″).Borders(2).LineStyle=1(其中Borders参数:1-左、2-右、3-顶、4-底、5-斜、6-斜/;LineStyle值:1与7-细实、2-细虚、4-点虚、9-双细实线)14.设置页眉eole.ActiveSheet.PageSetup.CenterHeader=″报表1″15.设置页脚eole.ActiveSheet.PageSetup.CenterFooter=″第&P页″16.设置页眉到顶端边距为2厘米eole.ActiveSheet.PageSetup.HeaderMargin=2/0.03517.设置页脚到底边距为3厘米eole.ActiveSheet.PageSetup.FooterMargin=3/0.03518.设置顶边距为2厘米eole.ActiveSheet.PageSetup.TopMargin=2/0.03519.设置底边距为4厘米eole.ActiveSheet.PageSetup.BottomMargin=4/0.03520.设置左边距为2厘米eole.ActiveSheet.PageSetup.LeftMargin=2/0.03521.设置右边距为2厘米eole.ActiveSheet.PageSetup.RightMargin=2/0.03522.设置页面水平居中eole.ActiveSheet.PageSetup.CenterHorizontally=True23.设置页面垂直居中eole.ActiveSheet.PageSetup.CenterVertically=True24.设置页面纸张大小(1-窄行8 5 11 39-宽行14 11)eole.ActiveSheet.PageSetup.PaperSize=125.打印单元格网线eole.ActiveSheet.PageSetup.PrintGridlines=True26.拷贝整个工作表edRange.Copy27.拷贝指定区域eole.ActiveSheet.Range(″A1:E2″).Copy28.粘贴eole.WorkSheet(″Sheet2″).Range(″A1″).PasteSpecial29.在第2行之前插入一行eole.ActiveSheet.Rows(2).Insert30.在第2列之前插入一列eole.ActiveSheet.Columns(2).Insert31.设置字体eole.ActiveSheet.Cells(2,1)=″黑体″32.设置字体大小eole.ActiveSheet.Cells(1,1).Font.Size=2533.设置字体为斜体eole.ActiveSheet.Cells(1,1).Font.Italic=True34.设置整列字体为粗体eole.ActiveSheet.Columns(1).Font.Bold=True35.清除单元格公式eole.ActiveSheet.Cells(1,4).ClearContents36.打印预览工作表方法一:eole.ActiveSheet.PrintPreview方法二:eole.ExecuteExcel4Macro("PRINT(1,,,1,,TRUE,,,,,,1,,,TRUE,,FALSE)") 37.打印输出工作表方法一:eole.ActiveSheet.PrintOut方法二:eole.ExecuteExcel4Macro("PRINT(1,,,1,,,,,,,,1,,,TRUE,,FALSE)") 38.工作表另为eole.ActiveWorkbook.SaveAs(″c:\temp\22.xls″)39.放弃存盘eole.ActiveWorkbook.saved=True40.关闭工作簿eole.Workbooks.close41.退出Exceleole.quit42. 合并string worksheet, beginRowcol, EndRowColMyOLE.ActiveWorkBook.Sheets(worksheet).Range( BeginRowCol+": "+EndRowCol).SelectMyOLE.ActiveWorkBook.Sheets(worksheet).Range(BeginRowCol+":"+ EndRowCol).Merge43. sheet改名MyOLE.ActiveWorkBook.Sheets(olename).selectMyOLE.ActiveWorkBook.Sheets(olename).name=newname44. 举例MyOLE=Create OLEObjectConnectErr = MyOLE.ConnectToNewObject ("excel.Application") MyOLE.visible=false//打开指定的XLS文件激活workbooksMyOLE.application.workbooks.Open (FilePath)//对XLS文件进行了改动以后,在关闭该文件时是否需要向用户提出警告。
PB中使用OLEObject操作Excel

PB中使用OLEObject操作Excel在PB中使用OLEObject操作Excelglave posted @ 2009年7月14日 06:15 in PB with tags PB OLE excel, 4671 阅读在PB使用OLEObject操作Excel1.申明和定义(其他变量定义省略)OLEObject xlapp // EXCEL application objectOLEObject xlwk // EXCEL workbook objectOLEObject xlsub // EXCEL worksheet objectOLEObject xlcel // EXCEL cell object2.连接Excel文件,ls_path为Excel路径xlapp = create OLEObjectli_ret = xlapp.ConnectToObject(ls_path)if li_ret < 0 thenMessageBox("ERR","")destroy xlappend if3.打开Excel的workbookll_cnt = xlapp.Application.Workbooks.Count //获取当前workbook的个数xlwk = xlapp.Application.Workbooks[ll_cnt] //打开最新的一个对象,也就是上面连接后打开的excel对象(如果改文件已经打开,需特殊考虑)xlsub = xlwk.Worksheets[1]xlapp.Application.Windows(ll_cnt).Visible = true //设置对象不可视xlsub.Activate4.操作Excel的sheet//设置单元格背景颜色xlsub.cells(1,1).Interior.Color = rgb(255,0,0)xlsub.cells(1,1).Interior.Pattern = "1"//设置列的filterxlsub.Rows("1:1").AutoFilter//获取Excel行数ll_rowcnt_xls = /doc/c24540444.html,edRange.Rows.Count //设置Excel列宽自动大小(随内容宽度自动变化)xlsub.Rows("1:" + string(ll_rowcnt_xls)).Columns.AutoFit//sheet锁定, false:解锁; true:加锁xlsub.Cells.Locked = False//做一个下拉列表框,当选择DEL时候背景变成红色,选择NoAction无变化xlsub.Range(ls_range).Validation.Delete xlsub.Range(ls_range).Validation.Add("3", "1", "1", "NoAction,DEL") xlsub.Range(ls_range).Validation.IgnoreBlank = Truexlsub.Range(ls_range).Validation.InCellDropdown = Truexlsub.Range(ls_range).Validation.InputTitle = ""xlsub.Range(ls_range).Validation.ErrorTitle = ""xlsub.Range(ls_range).Validation.InputMessage = ""xlsub.Range(ls_range).Validation.ErrorMessage = ""xlsub.Range(ls_range).Validation.IMEMode(0)xlsub.Range(ls_range).Validation.ShowInput = Truexlsub.Range(ls_range).Validation.ShowError = Truexlsub.Range(ls_range).FormatConditions.Deletexlsub.Range(ls_range).FormatConditions.Add("1", "3", "DEL") xlsub.Range(ls_range).FormatConditions(1).Font.Color = rgb(255,0,0)//设置sheet单元格格式为文本格式xlsub.Cells.NumberFormatLocal = "@"// 设置保护,第一个参数是密码,后面的参数比较复杂,可以自己去一个一个实验xlsub.protect("password",true, true,true, false,false,false,false,true,true, false,true,true,false,true,false)//将sheet1的一列复制到sheet2中xlwk.Sheets("sheet1").Select()xlwk.Sheets("sheet1").Columns(ll_col_xls).Copy() // 要复制的列xlwk.Sheets("sheet2").Select()xlwk.Sheets("sheet2").Columns(ll_col_newsheet).Select() // 粘贴到ll_col_newsheet列前xlwk.Sheets("sheet2").Paste()5.关闭Excelxlapp.Application.DisplayAlerts = False // 不提示保存等提示信息,且为不保存xlwk.save()xlwk.Close()xlapp.ConnectT oObject(ls_path) //从新打开一个Excel对象ll_cnt = xlapp.Application.Workbooks.Countxlwk = xlapp.Application.Workbooks[ll_cnt]xlapp.Application.DisplayAlerts = True// -------------------------------------------// 上面关闭提示了,这里要解开关闭,否则其他woekbook关闭也没提示,//但是直接设置为true是不可以的(见下面两行代码),必须再打开一个对象,再设置为true才可以//xlapp.Application.DisplayAlerts = False//xlapp.Application.DisplayAlerts = True//--------------------------------------------xlwk.close()xlapp.DisConnectObject()Destroy xlsubDestroy xlwkDestroy xlapp其他一些简单的操作,网上有很多很好的资料,这里只是提到一些特殊的操作,例如:下拉列表框,选择条件,设置保护等等而这些也正是网上还不容易找到答案的.因为在Excel录制了宏之后,把宏放到PB里还是会出现很多问题,需要查阅资料以及自己的实验才能得出正确结果,希望对需要的人有帮助.。
PB控制EXCEL的全面认识要点
PB OLE控制EXCEL的全面认识 1.创建Excel对象eole=CREATEOBJECT(′Excel.application′ 2.添加新工作簿eole.Workbooks.add 3.设置第3个工作表为激活工作表eole.Worksheets(″sheet3″.Activate 4.打开指定工作簿eole.Workbooks.Open(″c:\temp\ll.xls″ 5.显示Excel窗口eole.visible=.t. 6.更改Excel标题栏eole.Caption=″VFP应用程序调用Microsoft Excel″ 7.给单元格赋值eole.cells(1,4.value=XM(XM为数据库字段名 8.设置指定列的宽度(单位:字符个数eole.ActiveSheet.Columns(1.ColumnWidth=5 9.设置指定行的高度(单位:磅eole.ActiveSheet.Rows(1.RowHeight=1/0.035 (设定行高为1厘米,1磅=0.035厘米 10.在第18行之前插入分页符eole.Worksheets(″Sheet1″.Rows(18.PageBreak=1 11.在第4列之前删除分页符eole.ActiveSheet.Columns(4.PageBreak=0 12.指定边框线宽度(Borders参数如下ole.ActiveSheet.Range(″b3:d3″.Borders(2.Weight=3 13.设置四个边框线条的类型eole.ActiveSheet.Range(″b3:d3″.Borders(2.LineStyle=1 (其中Borders参数:1-左、2-右、3-顶、4-底、5-斜、6-斜/;LineStyle值:1与7-细实、2-细虚、4-点虚、9-双细实线 14.设置页眉eole.ActiveSheet.PageSetup.CenterHeader=″报表1″ 15.设置页脚eole.ActiveSheet.PageSetup.CenterFooter=″第&P页″ 16.设置页眉到顶端边距为2厘米eole.ActiveSheet.PageSetup.HeaderMargin=2/0.035 17.设置页脚到底边距为3厘米eole.ActiveSheet.PageSetup.FooterMargin=3/0.03518.设置顶边距为2厘米eole.ActiveSheet.PageSetup.TopMargin=2/0.035 19.设置底边距为4厘米eole.ActiveSheet.PageSetup.BottomMargin=4/0.035 20.设置左边距为2厘米veole.ActiveSheet.PageSetup.LeftMargin=2/0.035 21.设置右边距为2厘米eole.ActiveSheet.PageSetup.RightMargin=2/0.035 22.设置页面水平居中eole.ActiveSheet.PageSetup.CenterHorizontally=.t. 23.设置页面垂直居中eole.ActiveSheet.PageSetup.CenterVertically=.t.24.设置页面纸张大小(1-窄行-宽行eole.ActiveSheet.PageSetup.PaperSize=1 25.打印单元格网线eole.ActiveSheet.PageSetup.PrintGridlines=.t. 26.拷贝整个工作表edRange.Copy 27.拷贝指定区域eole.ActiveSheet.Range(″A1:E2″.Copy 28.粘贴eole.WorkSheet(″Sheet2″.Range(″A1″.Pa steSpecial 29.在第2行之前插入一行eole.ActiveSheet.Rows(2.Insert 30.在第2列之前插入一列eole.ActiveSheet.Columns(2.Insert 31.设置字体eole.ActiveSheet.Cells(2,=″黑体″ 32.设置字体大小eole.ActiveSheet.Cells(1,1.Font.Size=25 33.设置字体为斜体eole.ActiveSheet.Cells(1,1.Font.Italic=.t. 34.设置整列字体为粗体eole.ActiveSheet.Columns(1.Font.Bold=.t. 35.清除单元格公式eole.ActiveSheet.Cells(1,4.ClearContents 36.打印预览工作表eole.ActiveSheet.PrintPreview 37.打印输出工作表eole.ActiveSheet.PrintOut 38.工作表另为eole.Acti veWorkbook.SaveAs(″c:\temp\22.xls″ 39.放弃存盘eole.ActiveWorkbook.saved=.t. 40.关闭工作簿eole.Workbooks.close 41.退出Excel eole.quit 42 合并 string worksheet,beginRowcol,EndRowCol MyOLE.ActiveWorkBook.Sheets(worksheet.Range(BeginRowCol+":"+EndRowCol.Sele ctMyOLE.ActiveWorkBook.Sheets(worksheet.Range(BeginRowCol+":"+EndRowCol.Mer ge 43 sheet改名 MyOLE.ActiveWorkBook.Sheets(olename.selectMyOLE.ActiveWorkBook.Sheets(=newname MyOLE=Create OLEObject ConnectErr=MyOLE.ConnectToNewObject ("excel.Application" MyOLE.visible=false //打开指定的XLS文件激活workbooks MyOLE.application.workbooks.Open (FilePath //对XLS文件进行了改动以后,在关闭该文件时是否需要向用户提出警告。
pb操作EXCEL的一个对象
pb操作EXCEL的一个对象 (/yyoinge/article/details/6549193)以下内容保存为nvo_excel.sru文件,再导入即可view plaincopy to clipboardprint?$PBExportHeader$nvo_excel.sruforwardglobal type nvo_excel from nonvisualobjectend typeend forwardglobal type nvo_excel from nonvisualobjectend typeglobal nvo_excel nvo_exceltype variablesoleobject excelend variablesforward prototypespublic function integer of_connect ()public function integer of_disconnect ()public function string of_splitpath (string as, integer ai)public function integer of_check ()public function string of_getvalue (string as_path, string as_sheet, long row, long col) public function string of_get_excel_colid (long al)public function long of_get_num_colid (string as)public function string of_getvalue (string as_path, integer ai_sheet, long row, long col) public function string of_getvalue (string as_path, string as_sheet, string as_row, long col)public function string of_getvalue (string as_path, integer ai_sheet, string as_row, long col)public function long of_insertrow (string as_path, string as_sheet, long row)public function string of_sheetname (string as_path, integer ai_sheet)public function long of_colcount (string as_path, string as_sheet)public function long of_rowcount (string as_path, string as_sheet)public function long of_sheetcount (string as_path)public function string of_find (string as_path, string as_sheet, integer ai_column, string as_find)public function boolean of_is_opened (string as_path)public function integer of_check (string as_path)public function integer of_workbookcount ()public function integer of_check (string as_path, string as_sheetname)public function long of_sheetadd (string as_path, string as_sheet, integer ai_pos) public function boolean of_is_sheet_exists (string as_path, string as_sheet)public function integer of_open (string as_path)public function integer of_openwithsheet (string as_path, string as_sheet)public function integer of_close (string as)public function integer of_closewithsave (string as)public function integer of_save (string as)public function integer of_deletehypelink (string as_path, string as_sheet)public function integer of_openwithsheet (string as_path, integer ai_sheet)public function integer of_set_hypelink (string as_path, string as_sheet, long row, long col, string as_hype)public function integer of_setvalue (string as_path, integer ai_sheet, long row, long col, string as_value)public function integer of_setvalue (string as_path, string as_sheet, long row, long col, string as_value)public function integer of_sheetselect (string as_path, integer ai_sheet)public function integer of_sheetselect (string as_path, string as_sheet)public function integer of_show (boolean ab_show)public function integer of_showalldata (string as_path, string as_sheet)public function integer of_quit ()public function integer of_sheetdelete (string as_path, string as_sheet)public function long of_get_firstemprow (string as_path, string as_sheet, long al_col[]) public function integer of_setvalue (string as_path, integer ai_sheet, long row, string as_col, string as_value)public function integer of_setvalue (string as_path, string as_sheet, long row, string as_col, string as_value)public function string of_relative_path (string a1, string a2)public function integer of_scrollrow (string as_path, string as_sheet, long row)public function integer of_scrollcolumn (string as_path, string as_sheet, long col) public subroutine of_speed_set (string as_path, string as_sheet, any ast)end prototypespublic function integer of_connect ();of_disconnect()int excelokexcel=create oleobject;excelok=excel.connecttonewobject("excel.application");if excelok<>0 thenchoose case excelokcase -1messagebox('错误提示','无效的调用')case -2messagebox('错误提示','类名没发现')case -3messagebox('错误提示','对象不能创建')case -4messagebox('错误提示','文件不能连接')case -5messagebox('错误提示','不能连接现在的对象')case -6messagebox('错误提示','文件无效')case -7messagebox("错误提示","文件不存在或已经打开")case -8messagebox("错误提示","服务器不能装载选择的文件")case -9messagebox("错误提示","其他错误")end choosereturn -1end ifreturn 1end functionpublic function integer of_disconnect ();if isvalid(excel) thenexcel.workbooks.closeexcel.Application.quit();excel.disconnectobject();destroy excel;end ifreturn 1end functionpublic function string of_splitpath (string as, integer ai);//分隔文件//1返回路径,2返回文件名(带后缀名),32返回文件名(不带后缀名)choose case aicase 1if posw(as, '.') = 0 then return asreturn leftw(as,lenw(as) - posw(reverse(as), '/') + 1)case 2if posw(as, '/') = 0 then return asreturn rightw(as,posw(reverse(as), '/') - 1)case 3if posw(as, '/') > 0 then as = rightw(as,posw(reverse(as), '/') - 1)return leftw(as, lastpos(as, '.') - 1)case elsereturn asend chooseend functionpublic function integer of_check ();if not isvalid(excel) then return -1return 1end functionpublic function string of_getvalue (string as_path, string as_sheet, long row, long col);//取得单元格的内容if of_check(as_path,as_sheet) < 0 then return ''string lstryls = trim(string(excel.workbooks(of_splitpath(as_path,2)).sheets(as_sheet).cells(row, col).value))catch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回-1就好了return ''end tryif isnull(ls) then ls = ''return lsend functionpublic function string of_get_excel_colid (long al);//取得列号long llstring lsif al <= 0 then return ''ll = int((al - 1) / 26)if ll > 0 then ls = char(64 + ll)ll = mod(al, 26)if ll = 0 then ll = 26ls += string(char(64 + ll))return lsend functionpublic function long of_get_num_colid (string as);//取得列号if as = '' then return 0as = reverse(upper(as))long ll, lrstring lsfor ll = 1 to lenw(as)ls = midw(as, ll, 1)lr += (asc(ls) - 64) * 26^(ll - 1)nextreturn lrend functionpublic function string of_getvalue (string as_path, integer ai_sheet, long row, long col);//取得单元格的内容if of_check() < 0 then return ''return of_getvalue(as_path, of_sheetname(as_path, ai_sheet), row, col)end functionpublic function string of_getvalue (string as_path, string as_sheet, string as_row, long col);//取得单元格的内容if of_check() < 0 then return ''long rowrow = of_get_num_colid( as_row)return of_getvalue(as_path, as_sheet, row, col)end functionpublic function string of_getvalue (string as_path, integer ai_sheet, string as_row, long col);//取得单元格的内容if of_check() < 0 then return ''long rowrow = of_get_num_colid( as_row)return of_getvalue(as_path, ai_sheet, row, col)end functionpublic function long of_insertrow (string as_path, string as_sheet, long row);//插入行if of_check(as_path,as_sheet) < 0 then return 0tryexcel.workbooks(of_splitpath(as_path, 2)).sheets(as_sheet).Rows(row + 1 ).Insert catch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回-1就好了return -1end tryreturn row + 1end functionpublic function string of_sheetname (string as_path, integer ai_sheet);//关闭excel工作簿if of_check(as_path) < 0 then return ''tryreturn string(excel.workbooks(of_splitpath(as_path, 2)).Sheets(ai_sheet).name) catch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回-1就好了return ''end tryend functionpublic function long of_colcount (string as_path, string as_sheet);//取得列数if of_check(as_path,as_sheet) < 0 then return 0long lltryll = excel.workbooks(of_splitpath(as_path,2)).sheets(as_sheet).usedrange.Columns.countcatch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回-1就好了return -1end tryreturn llend functionpublic function long of_rowcount (string as_path, string as_sheet);//取得行数if of_check(as_path) < 0 then return -1long lltryll = excel.workbooks(of_splitpath(as_path,2)).sheets(as_sheet).usedrange.rows.countcatch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回-1就好了return -1end tryreturn llend functionpublic function long of_sheetcount (string as_path);//取得工作表数目if of_check(as_path) < 0 then return -1long lltryll = long(excel.workbooks(of_splitpath(as_path, 2)).Sheets.count)catch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回-1就好了return -1end tryreturn llend functionpublic function string of_find (string as_path, string as_sheet, integer ai_column, string as_find);//显示所有数据if of_check(as_path,as_sheet) < 0 then return ''string lsoleobject findrangeif ai_column > 0 then//在指定列查找tryfindrange = excel.workbooks(of_splitpath(as_path,2)).sheets(as_sheet).columns(ai_column).find(as_find)catch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回-1就好了return ''end tryif isvalid(findrange) thenreturn '('+string(findrange.row) + ',' + string(findrange.column) + '):' + string(findrange.value)elsereturn ''end ifelse//在整个表查找tryfindrange = excel.workbooks(of_splitpath(as_path,2)).sheets(as_sheet).usedrange.find(as_find)catch( runtimeerror er1) //出错的原因可能是工作簿并没有打开,直接返回-1就好了return ''end tryif isvalid(findrange) thenreturn '('+string(findrange.row) + ',' + string(findrange.column) + '):' + string(findrange.value)elsereturn ''end ifend ifreturn lsend functionpublic function boolean of_is_opened (string as_path);//判断同名工作簿是否已经打开(路径可能不同)oleobject lworkbooktrylworkbook = excel.Workbooks(of_splitpath(as_path, 2))catch( runtimeerror er)return falseend tryreturn trueend functionpublic function integer of_check (string as_path);if of_check() < 0 then return -1if not of_is_opened( as_path ) then//messagebox('提示','工作簿[' + as_path + ']尚未打开,无法执行相应操作!')//重新打开return of_open(as_path)end ifreturn 1end functionpublic function integer of_workbookcount ();//已经打开的工作簿的数目if of_check() < 0 then return -1int litryli = integer(excel.Workbooks.Count)catch(runtimeerror er)return -1end tryreturn liend functionpublic function integer of_check (string as_path, string as_sheetname);if of_check(as_path) < 0 then return -1if not of_is_sheet_exists( as_path, as_sheetname ) then//messagebox('提示','工作簿[' + as_path + ']中不存在工作表“‘ + as_sheetname + ’”,无法执行相应操作!')return -1end ifreturn 1end functionpublic function long of_sheetadd (string as_path, string as_sheet, integer ai_pos);//新增工作表数目if of_check(as_path) < 0 then return -1if of_is_sheet_exists(as_path, as_sheet) then //工作表已经存在of_sheetselect(as_path, as_sheet)return 0end iflong lltryll = of_sheetcount( as_path)//先插为第一个表if ll > 0 then excel.workbooks(of_splitpath(as_path, 2)).Sheets(1).selectexcel.workbooks(of_splitpath(as_path, 2)).Sheets.add()ll ++excel.workbooks(of_splitpath(as_path, 2)).Sheets(1).name = as_sheetif ai_pos <= 0 thenai_pos = ll + 1elseai_pos ++end ifexcel.workbooks(of_splitpath(as_path,2)).Sheets(1).move(excel.workbooks(of_splitpath(as_path, 2)).Sheets(min(ai_pos, ll))) of_sheetselect(as_path, as_sheet)if ai_pos > ll and ll > 1 thenexcel.workbooks(of_splitpath(as_path,2)).Sheets(ll).move(excel.workbooks(of_splitpath(as_path, 2)).Sheets(ll - 1))of_sheetselect(as_path, as_sheet)end ifcatch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回-1就好了return -1end tryreturn min(ai_pos - 1, ll)end functionpublic function boolean of_is_sheet_exists (string as_path, string as_sheet);//判断同名工作簿是否已经打开(路径可能不同)oleobject lworksheettrylworksheet = excel.Workbooks(of_splitpath(as_path, 2)).sheets(as_sheet)catch( runtimeerror er)return falseend tryreturn trueend functionpublic function integer of_open (string as_path);//打开工作簿,绝对路径if of_check() < 0 then return -1if not fileexists(as_path) thenmessagebox('提示','文件“' + as_path + '”不存在,无法打开!')return -1end iftryexcel.Application.workbooks.open(as_path)catch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回-1就好了return -1end tryexcel.Application.DisplayAlerts = Falsereturn 1end functionpublic function integer of_openwithsheet (string as_path, string as_sheet);//打开工作簿,绝对路径,并切换到as_sheet工作表if of_check() < 0 then return -1if of_open(as_path) < 0 then return -1if of_is_sheet_exists(as_path, as_sheet) = false then return -1return of_sheetselect(as_path, as_sheet)end functionpublic function integer of_close (string as);//关闭excel工作簿if of_check() < 0 then return -1tryexcel.workbooks(of_splitpath(as, 2)).closecatch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回就好了return -1end tryreturn 1end functionpublic function integer of_closewithsave (string as);//保存并关闭工作簿if of_check() < 0 then return -1if of_save(as) < 0 then return -1return of_close(as)end functionpublic function integer of_save (string as);//保存工作簿if of_check() < 0 then return -1excel.Application.DisplayAlerts = Falsetryexcel.workbooks(of_splitpath(as, 2)).save()catch( runtimeerror er) //出错的原因可能是工作簿并没有打开,因此也不需要再保存了,直接返回就好了return -1end tryreturn 1end functionpublic function integer of_deletehypelink (string as_path, string as_sheet);//删除所有单元格的超链接if of_check(as_path,as_sheet) < 0 then return -1tryexcel.workbooks(of_splitpath(as_path, 2)).sheets(as_sheet).cells.Hyperlinks.Delete catch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回-1就好了return -1end tryreturn 1end functionpublic function integer of_openwithsheet (string as_path, integer ai_sheet);//打开工作簿,绝对路径,并切换到第ai_sheet个工作表if of_check() < 0 then return -1if of_open(as_path) < 0 then return -1return of_sheetselect(as_path, ai_sheet)end functionpublic function integer of_set_hypelink (string as_path, string as_sheet, long row, long col, string as_hype);//设置超链接if of_check(as_path,as_sheet) < 0 then return -1tryexcel.workbooks(of_splitpath(as_path,2)).sheets(as_sheet).Hyperlinks.Add(excel.workbooks(of_splitpath(as_path,2)).sheets(as_sheet).Range(of_get_excel_colid(col) + string(row)),as_hype)catch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回-1就好了return -1end tryreturn 1end functionpublic function integer of_setvalue (string as_path, integer ai_sheet, long row, long col, string as_value);//设置单元格的内容if of_check() < 0 then return -1return of_setvalue(as_path, of_sheetname(as_path, ai_sheet), row, col, as_value)end functionpublic function integer of_setvalue (string as_path, string as_sheet, long row, long col, string as_value);//设置单元格的内容if of_check(as_path,as_sheet) < 0 then return -1tryexcel.workbooks(of_splitpath(as_path, 2)).sheets(as_sheet).cells(row, col).value = as_valuecatch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回-1就好了return -1return 1end functionpublic function integer of_sheetselect (string as_path, integer ai_sheet);//切换到sheet,前提是as已经打开if of_check(as_path) < 0 then return -1excel.Windows(of_splitpath(as_path, 2)).Activatetryexcel.ActiveWorkBook.Sheets(ai_sheet).Select////选择文件中一个工作表(按序号选择,比较通用的选择)catch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回-1就好了return -1end tryreturn 1end functionpublic function integer of_sheetselect (string as_path, string as_sheet);//切换到sheet,前提是as已经打开if of_check(as_path,as_sheet) < 0 then return -1excel.Windows(of_splitpath(as_path, 2)).Activatetryexcel.ActiveWorkBook.Sheets(as_sheet).Select//选择文件中一个工作表(按sheet名称选择,sheet名称确定的情况下使用)catch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回-1就好了return -1end tryreturn 1end functionpublic function integer of_show (boolean ab_show);//是否显示Excel界面if of_check() < 0 then return -1excel.visible = ab_showreturn 1end functionpublic function integer of_showalldata (string as_path, string as_sheet);//显示所有数据if of_check(as_path,as_sheet) < 0 then return -1boolean lbtrylb = excel.workbooks(of_splitpath(as_path, 2)).sheets(as_sheet).AutoFilterMode catch(runtimeerror er)return -1end tryIF lb THENexcel.workbooks(of_splitpath(as_path, 2)).sheets(as_sheet).ShowAllData //显示所有数据,关闭自动筛选excel.workbooks(of_splitpath(as_path, 2)).sheets(as_sheet).AutoFilterMode = falseexcel.workbooks(of_splitpath(as_path, 2)).sheets(as_sheet).cells.EntireRow.Hidden = False //取消隐藏,所有行列excel.workbooks(of_splitpath(as_path, 2)).sheets(as_sheet).cells.EntireColumn.Hidden = False //取消隐藏,所有行列return 1end functionpublic function integer of_quit ();//退出Excelif of_check() < 0 then return -1excel.workbooks.closeexcel.Application.quit();return 1end functionpublic function integer of_sheetdelete (string as_path, string as_sheet);//删除指定sheet,前提是as已经打开if of_check(as_path,as_sheet) < 0 then return -1if of_sheetselect(as_path, as_sheet) < 0 then return -1tryexcel.workbooks(of_splitpath(as_path, 2)).sheets(as_sheet).Delete//选择文件中一个工作表(按sheet名称选择,sheet名称确定的情况下使用)catch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回-1就好了return -1end tryreturn 1end functionpublic function long of_get_firstemprow (string as_path, string as_sheet, longal_col[]);//显示所有数据if of_check(as_path,as_sheet) < 0 then return -1boolean lblong row,ll_rowcountint istring lsif upperbound(al_col) = 0 then return -1ll_rowcount = of_rowcount(as_path, as_sheet)for row = ll_rowcount to 1 step -1ls = ''for i = 1 to upperbound(al_col)ls += of_getvalue(as_path, as_sheet, row, al_col[i])nextif ls = '' thencontinueend ifexitreturn rowend functionpublic function integer of_setvalue (string as_path, integer ai_sheet, long row, string as_col, string as_value);//设置单元格的内容if of_check() < 0 then return -1long colcol = of_get_num_colid( as_col)return of_setvalue(as_path, ai_sheet, row, col, as_value)end functionpublic function integer of_setvalue (string as_path, string as_sheet, long row, string as_col, string as_value);//设置单元格的内容if of_check() < 0 then return -1long colcol = of_get_num_colid( as_col)return of_setvalue(as_path, as_sheet, row, col, as_value)end functionpublic function string of_relative_path (string a1, string a2);////如:在a1中做超链接到a2:a1 = "c:/abc/a.xls", a2 = "c:/abc/def/b.xls"string lsint ia1 = lower(a1); a2 = lower(a2)if a1 = a2 then return '' //同一个工作簿,返回空值do while truei = posw(a1, '/')if i <= 0 thenelsels = leftw(a1, i)if leftw(a2, lenw(ls)) = ls and leftw(a2, 3) <> '../' then //相同a1 = midw(a1, i + 1); a2 = midw(a2, i+ 1)elsea1 = midw(a1, i + 1);a2 = "../" + a2end ifend ifloopreturn a2end functionpublic function integer of_scrollrow (string as_path, string as_sheet, long row);//滚动到指定的行if row < 1 or row > of_rowcount( as_path, as_sheet) then return -1if of_check(as_path,as_sheet) < 0 then return -1tryexcel.activewindow.scrollrow = row//workbooks(of_splitpath(as_path,2)).sheets(as_sheet).catch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回-1就好了return -1end tryreturn 1end functionpublic function integer of_scrollcolumn (string as_path, string as_sheet, long col);//滚动到指定的列if col < 1 or col > of_colcount( as_path, as_sheet) then return -1if of_check(as_path,as_sheet) < 0 then return -1tryexcel.activewindow.scrollcolumn = colcatch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回-1就好了return -1end tryreturn 1end functionpublic subroutine of_speed_set (string as_path, string as_sheet, any ast);////设置Excel 的速度性//constant long xlCalculationAutomatic = -4105 //自动重算//constant long xlCalculationManual = -4135 //手动重算//constant long xlCalculationSemiautomatic = 2 //除模拟运算表外,自动重算//st_speed lst//if of_check() < 0 then return lst//lst.flags = 1//lst.ScreenUpdating = excel.Application.ScreenUpdating//lst.DisplayStatusBar = excel.Application.DisplayStatusBar//lst.EnableEvents = excel.Application.EnableEvents//lst.Calculation = excel.Application.Calculation//lst.CalculateBeforeSave = excel.Application.CalculateBeforeSave//lst.DisplayPageBreaks = false//if of_check(as_path, as_sheet) < 0 then return lst////lst.DisplayPageBreaks = excel.workbooks(of_splitpath(as_path,2)).sheets(as_sheet).DisplayPageBreaks//if ast.flags <> 1 then //ast中并没有写入设置,默认为高速设置// excel.Application.ScreenUpdating = TRUE// excel.Application.DisplayStatusBar = false// excel.Application.EnableEvents = false// excel.Application.Calculation = xlCalculationManual// excel.Application.CalculateBeforeSave = true//// excel.workbooks(of_splitpath(as_path, 2)).sheets(as_sheet).DisplayPageBreaks = false//else// excel.Application.ScreenUpdating = ast.ScreenUpdating。
如何用PB程序在excel画表格边框线
如何用PB程序在excel画表格边框线,如何改变文字大小1.创建Excel对象eole=CREATEOBJECT(′Excel.application′)2.添加新工作簿eole.Workbooks.add3.设置第3个工作表为激活工作表eole.Worksheets(″sheet3″).Activate4.打开指定工作簿eole.Workbooks.Open(″c:\temp\ll.xls″)5.显示Excel窗口 eole.visible=.t.6.更改Excel标题栏eole.Caption=″VFP应用程序调用Microsoft Excel″7.给单元格赋值eole.cells(1,4).value=XM(XM为数据库字段名)8.设置指定列的宽度(单位:字符个数)eole.ActiveSheet.Columns(1).ColumnWidth=59.设置指定行的高度(单位:磅)eole.ActiveSheet.Rows(1).RowHeight=1/0.035 (设定行高为1厘米,1磅=0.035厘米)10.在第18行之前插入分页符eole.Worksheets(″Sheet1″).Rows(18).PageBreak=111.在第4列之前删除分页符eole.ActiveSheet.Columns(4).PageBreak=012.指定边框线宽度(Borders参数如下)ole.ActiveSheet.Range(″b3:d3″).Borders(2).Weight=313.设置四个边框线条的类型eole.ActiveSheet.Range(″b3:d3″).Borders(2).LineStyle=1(其中Borders参数:1-左、2-右、3-顶、4-底、5-斜、6-斜/;LineStyle值:1与7-细实、2-细虚、4-点虚、9-双细实线) 14.设置页眉eole.ActiveSheet.PageSetup.CenterHeader=″报表1″15.设置页脚eole.ActiveSheet.PageSetup.CenterFooter=″第&P页″16.设置页眉到顶端边距为2厘米eole.ActiveSheet.PageSetup.HeaderMargin=2/0.03517.设置页脚到底边距为3厘米eole.ActiveSheet.PageSetup.FooterMargin=3/0.03518.设置顶边距为2厘米eole.ActiveSheet.PageSetup.TopMargin=2/0.03519.设置底边距为4厘米eole.ActiveSheet.PageSetup.BottomMargin=4/0.03520.设置左边距为2厘米veole.ActiveSheet.PageSetup.LeftMargin=2/0.03521.设置右边距为2厘米eole.ActiveSheet.PageSetup.RightMargin=2/0.03522.设置页面水平居中eole.ActiveSheet.PageSetup.CenterHorizontally=.t.23.设置页面垂直居中eole.ActiveSheet.PageSetup.CenterVertically=.t.24.设置页面纸张大小(1-窄行8511 39-宽行1411) eole.ActiveSheet.PageSetup.PaperSize=125.打印单元格网线eole.ActiveSheet.PageSetup.PrintGridlines=.t.26.拷贝整个工作表edRange.Copy27.拷贝指定区域eole.ActiveSheet.Range(″A1:E2″).Copy28.粘贴eole.WorkSheet(″Sheet2″).Range(″A1″).PasteSpecial29.在第2行之前插入一行eole.ActiveSheet.Rows(2).Insert30.在第2列之前插入一列eole.ActiveSheet.Columns(2).Insert31.设置字体eole.ActiveSheet.Cells(2,1)=″黑体″32.设置字体大小eole.ActiveSheet.Cells(1,1).Font.Size=2533.设置字体为斜体eole.ActiveSheet.Cells(1,1).Font.Italic=.t.34.设置整列字体为粗体eole.ActiveSheet.Columns(1).Font.Bold=.t.35.清除单元格公式eole.ActiveSheet.Cells(1,4).ClearContents36.打印预览工作表eole.ActiveSheet.PrintPreview37.打印输出工作表eole.ActiveSheet.PrintOut38.工作表另为eole.ActiveWorkbook.SaveAs(″c:\temp\22.xls″)39.放弃存盘eole.ActiveWorkbook.saved=.t.40.关闭工作簿eole.Workbooks.close41.退出Exceleole.quit先把标题放到剪贴板上,再PASTE()到EXCEL中,代码如下: ::clipboard(ls_value)ao_object.range(ls_col+string(1)+":"+ls_col+string(1)).select() ao_object.activesheet.Paste()/**********************************************************/ /*函数名称:uf_dwsaveas_excel功能:将数据窗口数据导出EXCEL文件,并将EXCEL文件默认英文标题替换成中文。
POWERBUILDER与EXCEL的结合
POWERBUILDER与EXCEL的结合辽宁省朝阳市地方税务局信息中心(0421-*******)许贵江辽宁省朝阳师专曹英慧摘要:通过实例介绍PB与EXCEL的结合的两种方法,完成报表的灵活打印关键词:PowerBuilder8.0 报表动态数据交换PowerBuilder(以下称PB)是一种功能强大的面向对象的数据库开发工具,利用它可以方便地开发出数据库管理系统的客户端软件。
但利用它开发打印报表却很不方便,特别是针对中国式报表,这在很大程度上降低了软件的灵活性。
而Microsoft公司的表格处理软件Excel 却简单易用。
若把两者结合起来使用,用PB开发的应用程序管理数据,Excel处理软件打印报表,可取长补短、发挥各自特长,得到实用而灵活的报表。
本人通过实践,用两种方式把PB开发的软件和Excel结合起来。
一种是利用动态数据交换DDE(Dynamic Data Exchange)技术。
另一种是通过将数据窗口的内容以Excel格式保存到中间文件中的方式。
DDE能够使两个正在Windows平台上运行的应用程序之间进行动态交换数据,这两个正在运行的程序分别称为客户程序和服务器程序。
PB通过提供事件和函数支持DDE ,允许PB应用程序向另一个支持DDE的应用程序发送消息或响应其它应用程序的请求。
本文中,我们用Microsoft公司的表格处理软件Excel做DDE应用服务器程序,只用来接收数据,用PB应用程序做DDE客户程序,用来发送数据。
1. 用PB中的run函数启动DDE服务器应用程序Excel.exe。
其语法格式为:run(string{,windowstate})* string:字符型,要执行的程序文件名;* windowstate:枚举型,程序启动后的窗口状态。
2. 在客户程序PB中建立DDE连接在客户程序和服务器程序均启动之后,就要建立客户程序和服务器程序之间的连接。
本程序只是在PB中向Excel报表传递数据,并不需要知道Excel中对数据的修改情况,可以使用PB中的OpenChannel函数建立连接。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Pb中对Excel文件的应用Oleobject ole_object//建立OLE对象ole_object=create oleobject//创建对象integer li_retli_ret=ole_object.connecttoobject( " ", "Excel.Application ")//建立连接if li_ret <> 0 then//如果Excel还没有打开,则新建。
li_ret=ole_object.ConnectToNewObject( "Excel.Application ")if li_ret <> 0thenmessagebox( "OLE错误", "OLE无法连接!错误号:"+string(li_ret))returnend ifole_object.Visible=trueend ifpointer oldpointer//设置鼠标oldpointer=setpointer(HourGlass!)ole_object.Workbooks.Add//新建工作薄ole_object.Application.DisplayAlerts=False//关闭警告消息对话框,防止提示ole_object.Application.Workbooks.Open(as_exccccccccccccccccccccccccccccccccccel_filename)/ /打开Excel文件//ole_object.Application.ActiveWorkbook.SaveAs(as_text_filename,-4158)//把打开的Excel文件另存为//Text文件,第二参数表示数据以制表符TAB分隔ole_object.Application.Quit()//退出Excelole_object.activeworkbook.saved=true//放弃存盘ole_object.workbooks.close//关闭工作簿ole_object.Cells(1,1).Value=reptitleole_object.Range( 'A1 ').Selectole_object.Selection.Font.Size=24ole_object.selection.HorizontalAlignment=3//水平对齐方式:ole_object.Range( 'A1: '+f_columname(ll_colnum)+ '1 ').Selectole_object.Range( 'A1: '+f_columname(ll_colnum)+ '1 ').Mergeole_object.Columns(i).ColumnWidth=ld_widthole_object.Columns(i).HorizontalAlignment=3ole_object.Columns(i).Borders.LineStyle=1ole_object.Columns(i).Font.Bold=Trueole_object.cells(i,j).NumberFormat= "@ "ole_object.cells(i,j).Font.Bold=falseole_object.cells(i,j).value=ls_valueOLE_edRange.Rows.CountSheets.Add基本操作:Ole_object.Workbooks.add//新建一个Excel文件Ole_object.Workbooks.Open( "FileName ")//打开一个已存在Excel文件Ole_object.ActiveWorkBook.Sheets( "SheetName ").Select//选择文件中一个工作表Ole_object.Application.Run( "MacroName ")//运行宏Ole_object.Application.Visible=TRUE//Excel文件可见Ole_object.Application.ScreenUpdating=true//设置可见属性②格式设置Ole_object.ActiveSheet.Columns( "A:U ").AutoFit//列宽自动调整Ole_object.ActiveSheet.Columns( "A:Z ").ColumnWidth=6.75//列宽Ole_object.ActiveSheet.Rows( "1:100 ").RowHeight=12//行高Ole_object.Application.StandardFont= "ArialNarrow ";//设置字体Ole_object.Application.StandardFontSize= "8 "//设置字号Ole_object.ActiveSheet.Font.Size= "8 "//设置字号Ole_object.ActiveSheet.Font.Bold=True//粗体Ole_object.ActiveSheet.Font.Italic=True//斜体Ole_object.ActiveSheet.Font.Underline=True//下划线Ole_object.ActiveSheet.Font.StrikeThrough=True//删除线Ole_object.ActiveSheet.HorizontalAlignment=3//水平:4靠右;3居中;2靠左Ole_object.ActiveSheet.VerticalAlignment=2//垂直:3靠下;2居中;1靠上Ole_object.ActiveSheet.cells(2,1).f =‟黑体‟//设置字体Ole_object.ActiveSheet.cells(2,1).font.size=25//设置字体大小③工作区域操作:Ole_object.ActiveSheet.Range( "A1:Z10 ").Property=value//设置一个工作区域内的属性值Ole_object.ActiveSheet.Range( "A1:Z10 ").Merge//合并单元格Ole_object.ActiveSheet.Range( "A1:Z10 ").WrapText=False//自动换行禁止Ole_object.ActiveSheet.Range( "A1:Z10 ").Borders(b_type).Weight=i_val//b_type:1左边界;2右边界;3上边界;4下边界;5左上倾斜;6右上倾斜(以上为对单元格的操作)7左边界;8上边界;9下边界;10右边界;11内部垂直边界;12内部水平边界(以上为对区域的操作)//i_val:0无边界线;然后1,2,3。
依次边界线加粗Ole_object.ActiveSheet.Range( "A1:Z10 ").Borders(b_type).linestyle=1//1与7—细实、2—细虚、4—点虚、9—双细实线④赋值操作Ole_object.ActiveSheet.range( "A1:Z10 ").Cells.value=“姓名”Ole_object.ActiveSheet.range( "A1:Z10 ").Cells.value=1Ole_object.ActiveSheet.range( "A1:Z10 ").Cells.value=“2003-01-01”Ole_object.ActiveSheet.Range( "A1:Z10 ").Cells(1,2).value=“年龄”//区域内一个单元格的赋值Ole_object.ActiveSheet.Range( "A1:Z1 ").Cells(1,1).value=“SUM(RC[-9]:RC[-1])”⑤打印属性设置Ole_object.ActiveSheet.pagesetup.LeftHeader= " "Ole_object.ActiveSheet.pagesetup.CenterHeader= " "//设置页眉Ole_object.ActiveSheet.pagesetup.RightHeader= " "//Ole_object.ActiveSheet.pagesetup.LeftFooter= " "Ole_object.ActiveSheet.pagesetup.CenterFooter= " "//设置页脚Ole_object.ActiveSheet.pagesetup.RightFooter= " "Ole_object.ActiveSheet.pagesetup.LeftMargin=0//设置左边距Ole_object.ActiveSheet.pagesetup.RightMargin=0//设置右边距Ole_object.ActiveSheet.pagesetup.TopMargin=2/0.035//设置顶边距为2厘米Ole_object.ActiveSheet.pagesetup.BottomMargin=0//设置底边距Ole_object.ActiveSheet.pagesetup.HeaderMargin=2/0.035//设置页眉到顶端边距2厘米Ole_object.ActiveSheet.pagesetup.FooterMargin=3/0.035//设置页脚到底边距为3厘米Ole_object.ActiveSheet.pagesetup.PrintHeadings=FalseOle_object.ActiveSheet.pagesetup.PrintGridlines=False//设置打印单元格网线Ole_object.ActiveSheet.pagesetup.PrintQuality=600Ole_object.ActiveSheet.pagesetup.CenterHorizontally=True//设置页面水平居中Ole_object.ActiveSheet.pagesetup.CenterVertically=True//设置页面垂直居中Ole_object.ActiveSheet.pagesetup.Orientation=2//打印方向1垂直;2水平Ole_object.ActiveSheet.pagesetup.Draft=FalseOle_object.ActiveSheet.pagesetup.PaperSize=9//8-A3;9-A4//设置纸张大小Ole_object.ActiveSheet.pagesetup.Order=1//打印次序:1先上下再左右2先左右再上下Ole_object.ActiveSheet.pagesetup.FitToPagesWide=1//缩放在一页中:0否;1是Ole_edrange.copy//拷贝整个工作表Ole_object.ActiveSheet.cells(1,4).clearcontents//清除单元格公式Ole_object.ActiveSheet.printpreview//打印预览工作表Ole_object.ActiveSheet.printout//打印输出工作表Ole_object.ActiveSheet..range(“a1:e3”).copy//拷贝指定区域Ole_object.worksheet(“sheet2”).range(“a1”).pastespecial//粘贴Ole_object.ActiveSheet.rows(2).insert//在第2行之前插入一行Ole_object.ActiveSheet.colunms(2).insert//在第2列之前插入一列ole_object.Application.DisplayAlerts=False //关闭警告消息对话框,防止提示ole_object.activeworkbook.sheets.Add//新增工作表ole_object.worksheets( "sheet3 ").activate//设活动工作表ole_object.worksheets( "sheet1 ").delete//删除指定工作表ole_object.caption= " "//修改标题ole_object.visible=true//显示Excel窗口ole_object.activeworkbook.saveas( 'C:\adm.xls ')ole_object.activesheet.columns(1).columnwidth=5//设置指定列的宽度(单位:字符个数)ole_object.activesheet.rows(1).rowheight=1/0.035//设置指定行的高度(单位:磅,设定行高为1厘米,1磅=0.035厘米)ole_object.worksheets(…sheel1‟).rows(18).pagebreak=1//在每18行之前插入分页符ole_object.activesheet.columns(4).pagebreak=0//在第4列之前删除分页符ole_object.cells(1,1).value=1//给单元格赋值ole_object.cells(2,1).value=2ole_object.cells(4,5).value= '=sum(a1,a2) 'ole_object.Range( 'a1:z6555 ').locked=false//单元格保护锁定ole_object.Range( 'a1:z6555 ').FormulaHidden=true//单元格公式隐藏ole_object.Application.ActiveSheet.Protect( '12345 ',true,true,true)//对活动单元格进行保护ole_object.Application.Activeworkbook.Protect( '12345 ',true,true)//对活动工作簿进行保护ole_object.Application.ActiveSheet.unProtect( '12345 ')//取消对活动单元格进行保护ole_object.Range( 'A1:c1 ').Select//选取范围ole_object.Range( 'A1:d4 ').Merge//合并单元格ole_object.Selection.Font.Size=24ole_object.Columns(1).Font.Bold=Trueole_object.selection.HorizontalAlignment=3//水平ole_object.Columns(1).ColumnWidth=10//设置列宽ole_object.Range( 'a1:d4 ').borders().weight=4//设置边框线宽ole_object.Range( 'a1:d4 ').borders().linestyle=1//设置边框样式ole_= "xzm "ole_object.workSheet( 'sheet2 ').name= "xzm "//修改当前工作表名称ole_= "xzm "//ole_object.Application.Quit()ole_object.disconnectobject()//取消连接destroyole_object//删除对象//导出成excel时,如‘001‘可先设置对应单元格格式,再导出到对应单元格Ole_object.range(…a1:z99‟).cells(4,5).NumberFormatLocal = "@"Ole_object.range(…a1:z99‟).cells(4,5).values = …001‟。