大剑
天天加班,哪有时间写博客 ---- 专注于ERP软件开发技术,关注Oracle相关技术

在PB中使用OLEObject操作Excel

glave posted @ 2009年7月14日 06:15 in PB with tags PB OLE excel , 10544 阅读

在PB使用OLEObject操作Excel

1.申明和定义(其他变量定义省略)
 OLEObject  xlapp   // EXCEL application object
 OLEObject xlwk   // EXCEL workbook object
 OLEObject xlsub   // EXCEL worksheet object
 OLEObject xlcel   // EXCEL cell object

2.连接Excel文件,ls_path为Excel路径
 xlapp = create OLEObject
 
 li_ret = xlapp.ConnectToObject(ls_path)

 if li_ret < 0  then
  MessageBox("ERR","")
  destroy xlapp
 end if

3.打开Excel的workbook
 ll_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.Activate
 
4.操作Excel的sheet
 //设置单元格背景颜色
 xlsub.cells(1,1).Interior.Color = rgb(255,0,0)
 xlsub.cells(1,1).Interior.Pattern = "1"
 
 //设置列的filter
 xlsub.Rows("1:1").AutoFilter
 
 //获取Excel行数
 ll_rowcnt_xls = xlsub.UsedRange.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 = True
 xlsub.Range(ls_range).Validation.InCellDropdown = True
 xlsub.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 = True
 xlsub.Range(ls_range).Validation.ShowError = True
 
 xlsub.Range(ls_range).FormatConditions.Delete
 xlsub.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.关闭Excel
 xlapp.Application.DisplayAlerts   =   False // 不提示保存等提示信息,且为不保存
 xlwk.save()
 xlwk.Close()
 
 xlapp.ConnectToObject(ls_path) //从新打开一个Excel对象
 ll_cnt = xlapp.Application.Workbooks.Count
 xlwk = 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 xlsub
 Destroy xlwk
 Destroy xlapp
 
 其他一些简单的操作,网上有很多很好的资料,这里只是提到一些特殊的操作,例如:下拉列表框,选择条件,设置保护等等
 而这些也正是网上还不容易找到答案的.
 因为在Excel录制了宏之后,把宏放到PB里还是会出现很多问题,需要查阅资料以及自己的实验才能得出正确结果,
 希望对需要的人有帮助.

Avatar_small
AP SSC sa 2 Model Pa 说:
2022年9月09日 03:38 SA-2 Exams called Summative Assignment-2 exams are known as Annual final public exams, every year those exams are held at the end of the academic session as Term-2 exams. Every State Board Telugu Medium, AP SSC sa 2 Model Paper English Medium & Urdu Medium Students can download the AP 10th Class SA 2 Model Paper 2023 Pdf with Answer Solutions designed by the Board based on the revised syllabus and curriculum.Class teachers and others have designed and suggested the AP 10th Class SA 2 Model Paper 2023 for all languages and subjects of the course for theory, objective,
Avatar_small
Kerala 10th Class S 说:
2023年8月23日 22:44

KBPE Conducts all the Public Exams in Kerala, This Kerala High School Level Exam Attended Every Year More than 40 Laks of Students, Students Studying Kerala 6th Class Revised Syllabus 2024 helps Students to Learn Logic and order and hence,Kerala Class Syllabus 2024 is Designed in Accordance with the NCERT Based Guidelines and helps Students to get an Overview of the Malayalam,Kerala 10th Class Syllabus 2024 English Medium All Subject, KBPE keeps a Class Exam Pattern with the aim to Provide a Quality Education for All Students.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter