pb操作EXCEL的一个对象

以下内容保存为nvo_excel.sru文件,再导入即可

 

$PBExportHeader$nvo_excel.sru forward global type nvo_excel from nonvisualobject end type end forward global type nvo_excel from nonvisualobject end type global nvo_excel nvo_excel type variables oleobject excel end variables forward prototypes public 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 prototypes public function integer of_connect ();of_disconnect() int excelok excel=create oleobject; excelok=excel.connecttonewobject("excel.application"); if excelok<>0 then choose case excelok 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 -1 end if return 1 end function public function integer of_disconnect ();if isvalid(excel) then excel.workbooks.close excel.Application.quit(); excel.disconnectobject(); destroy excel; end if return 1 end function public function string of_splitpath (string as, integer ai);//分隔文件 //1返回路径,2返回文件名(带后缀名),32返回文件名(不带后缀名) choose case ai case 1 if posw(as, '.') = 0 then return as return leftw(as,lenw(as) - posw(reverse(as), '/') + 1) case 2 if posw(as, '/') = 0 then return as return rightw(as,posw(reverse(as), '/') - 1) case 3 if posw(as, '/') > 0 then as = rightw(as,posw(reverse(as), '/') - 1) return leftw(as, lastpos(as, '.') - 1) case else return as end choose end function public function integer of_check ();if not isvalid(excel) then return -1 return 1 end function public 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 ls try ls = trim(string(excel.workbooks(of_splitpath(as_path, 2)).sheets(as_sheet).cells(row, col).value)) catch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回-1就好了 return '' end try if isnull(ls) then ls = '' return ls end function public function string of_get_excel_colid (long al);//取得列号 long ll string ls if 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 = 26 ls += string(char(64 + ll)) return ls end function public function long of_get_num_colid (string as);//取得列号 if as = '' then return 0 as = reverse(upper(as)) long ll, lr string ls for ll = 1 to lenw(as) ls = midw(as, ll, 1) lr += (asc(ls) - 64) * 26^(ll - 1) next return lr end function public 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 function public function string of_getvalue (string as_path, string as_sheet, string as_row, long col);//取得单元格的内容 if of_check() < 0 then return '' long row row = of_get_num_colid( as_row) return of_getvalue(as_path, as_sheet, row, col) end function public function string of_getvalue (string as_path, integer ai_sheet, string as_row, long col);//取得单元格的内容 if of_check() < 0 then return '' long row row = of_get_num_colid( as_row) return of_getvalue(as_path, ai_sheet, row, col) end function public function long of_insertrow (string as_path, string as_sheet, long row);//插入行 if of_check(as_path,as_sheet) < 0 then return 0 try excel.workbooks(of_splitpath(as_path, 2)).sheets(as_sheet).Rows(row + 1 ).Insert catch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回-1就好了 return -1 end try return row + 1 end function public function string of_sheetname (string as_path, integer ai_sheet);//关闭excel工作簿 if of_check(as_path) < 0 then return '' try return string(excel.workbooks(of_splitpath(as_path, 2)).Sheets(ai_sheet).name) catch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回-1就好了 return '' end try end function public function long of_colcount (string as_path, string as_sheet);//取得列数 if of_check(as_path,as_sheet) < 0 then return 0 long ll try ll = excel.workbooks(of_splitpath(as_path, 2)).sheets(as_sheet).usedrange.Columns.count catch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回-1就好了 return -1 end try return ll end function public function long of_rowcount (string as_path, string as_sheet);//取得行数 if of_check(as_path) < 0 then return -1 long ll try ll = excel.workbooks(of_splitpath(as_path, 2)).sheets(as_sheet).usedrange.rows.count catch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回-1就好了 return -1 end try return ll end function public function long of_sheetcount (string as_path);//取得工作表数目 if of_check(as_path) < 0 then return -1 long ll try ll = long(excel.workbooks(of_splitpath(as_path, 2)).Sheets.count) catch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回-1就好了 return -1 end try return ll end function public 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 ls oleobject findrange if ai_column > 0 then//在指定列查找 try findrange = excel.workbooks(of_splitpath(as_path, 2)).sheets(as_sheet).columns(ai_column).find(as_find) catch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回-1就好了 return '' end try if isvalid(findrange) then return '('+string(findrange.row) + ',' + string(findrange.column) + '):' + string(findrange.value) else return '' end if else//在整个表查找 try findrange = excel.workbooks(of_splitpath(as_path, 2)).sheets(as_sheet).usedrange.find(as_find) catch( runtimeerror er1) //出错的原因可能是工作簿并没有打开,直接返回-1就好了 return '' end try if isvalid(findrange) then return '('+string(findrange.row) + ',' + string(findrange.column) + '):' + string(findrange.value) else return '' end if end if return ls end function public function boolean of_is_opened (string as_path);//判断同名工作簿是否已经打开(路径可能不同) oleobject lworkbook try lworkbook = excel.Workbooks(of_splitpath(as_path, 2)) catch( runtimeerror er) return false end try return true end function public function integer of_check (string as_path);if of_check() < 0 then return -1 if not of_is_opened( as_path ) then //messagebox('提示','工作簿[' + as_path + ']尚未打开,无法执行相应操作!') //重新打开 return of_open(as_path) end if return 1 end function public function integer of_workbookcount ();//已经打开的工作簿的数目 if of_check() < 0 then return -1 int li try li = integer(excel.Workbooks.Count) catch(runtimeerror er) return -1 end try return li end function public function integer of_check (string as_path, string as_sheetname);if of_check(as_path) < 0 then return -1 if not of_is_sheet_exists( as_path, as_sheetname ) then //messagebox('提示','工作簿[' + as_path + ']中不存在工作表“‘ + as_sheetname + ’”,无法执行相应操作!') return -1 end if return 1 end function public function long of_sheetadd (string as_path, string as_sheet, integer ai_pos);//新增工作表数目 if of_check(as_path) < 0 then return -1 if of_is_sheet_exists(as_path, as_sheet) then //工作表已经存在 of_sheetselect(as_path, as_sheet) return 0 end if long ll try ll = of_sheetcount( as_path) //先插为第一个表 if ll > 0 then excel.workbooks(of_splitpath(as_path, 2)).Sheets(1).select excel.workbooks(of_splitpath(as_path, 2)).Sheets.add() ll ++ excel.workbooks(of_splitpath(as_path, 2)).Sheets(1).name = as_sheet if ai_pos <= 0 then ai_pos = ll + 1 else ai_pos ++ end if excel.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 then excel.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 if catch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回-1就好了 return -1 end try return min(ai_pos - 1, ll) end function public function boolean of_is_sheet_exists (string as_path, string as_sheet);//判断同名工作簿是否已经打开(路径可能不同) oleobject lworksheet try lworksheet = excel.Workbooks(of_splitpath(as_path, 2)).sheets(as_sheet) catch( runtimeerror er) return false end try return true end function public function integer of_open (string as_path);//打开工作簿,绝对路径 if of_check() < 0 then return -1 if not fileexists(as_path) then messagebox('提示','文件“' + as_path + '”不存在,无法打开!') return -1 end if try excel.Application.workbooks.open(as_path) catch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回-1就好了 return -1 end try excel.Application.DisplayAlerts = False return 1 end function public function integer of_openwithsheet (string as_path, string as_sheet);//打开工作簿,绝对路径,并切换到as_sheet工作表 if of_check() < 0 then return -1 if of_open(as_path) < 0 then return -1 if of_is_sheet_exists(as_path, as_sheet) = false then return -1 return of_sheetselect(as_path, as_sheet) end function public function integer of_close (string as);//关闭excel工作簿 if of_check() < 0 then return -1 try excel.workbooks(of_splitpath(as, 2)).close catch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回就好了 return -1 end try return 1 end function public function integer of_closewithsave (string as);//保存并关闭工作簿 if of_check() < 0 then return -1 if of_save(as) < 0 then return -1 return of_close(as) end function public function integer of_save (string as);//保存工作簿 if of_check() < 0 then return -1 excel.Application.DisplayAlerts = False try excel.workbooks(of_splitpath(as, 2)).save() catch( runtimeerror er) //出错的原因可能是工作簿并没有打开,因此也不需要再保存了,直接返回就好了 return -1 end try return 1 end function public function integer of_deletehypelink (string as_path, string as_sheet);//删除所有单元格的超链接 if of_check(as_path,as_sheet) < 0 then return -1 try excel.workbooks(of_splitpath(as_path, 2)).sheets(as_sheet).cells.Hyperlinks.Delete catch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回-1就好了 return -1 end try return 1 end function public function integer of_openwithsheet (string as_path, integer ai_sheet);//打开工作簿,绝对路径,并切换到第ai_sheet个工作表 if of_check() < 0 then return -1 if of_open(as_path) < 0 then return -1 return of_sheetselect(as_path, ai_sheet) end function public 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 -1 try excel.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 -1 end try return 1 end function public function integer of_setvalue (string as_path, integer ai_sheet, long row, long col, string as_value);//设置单元格的内容 if of_check() < 0 then return -1 return of_setvalue(as_path, of_sheetname(as_path, ai_sheet), row, col, as_value) end function public 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 -1 try excel.workbooks(of_splitpath(as_path, 2)).sheets(as_sheet).cells(row, col).value = as_value catch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回-1就好了 return -1 end try return 1 end function public function integer of_sheetselect (string as_path, integer ai_sheet);//切换到sheet,前提是as已经打开 if of_check(as_path) < 0 then return -1 excel.Windows(of_splitpath(as_path, 2)).Activate try excel.ActiveWorkBook.Sheets(ai_sheet).Select////选择文件中一个工作表(按序号选择,比较通用的选择) catch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回-1就好了 return -1 end try return 1 end function public function integer of_sheetselect (string as_path, string as_sheet);//切换到sheet,前提是as已经打开 if of_check(as_path,as_sheet) < 0 then return -1 excel.Windows(of_splitpath(as_path, 2)).Activate try excel.ActiveWorkBook.Sheets(as_sheet).Select//选择文件中一个工作表(按sheet名称选择,sheet名称确定的情况下使用) catch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回-1就好了 return -1 end try return 1 end function public function integer of_show (boolean ab_show);//是否显示Excel界面 if of_check() < 0 then return -1 excel.visible = ab_show return 1 end function public function integer of_showalldata (string as_path, string as_sheet);//显示所有数据 if of_check(as_path,as_sheet) < 0 then return -1 boolean lb try lb = excel.workbooks(of_splitpath(as_path, 2)).sheets(as_sheet).AutoFilterMode catch(runtimeerror er) return -1 end try IF lb THEN excel.workbooks(of_splitpath(as_path, 2)).sheets(as_sheet).ShowAllData //显示所有数据,关闭自动筛选 excel.workbooks(of_splitpath(as_path, 2)).sheets(as_sheet).AutoFilterMode = false END IF excel.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 1 end function public function integer of_quit ();//退出Excel if of_check() < 0 then return -1 excel.workbooks.close excel.Application.quit(); return 1 end function public function integer of_sheetdelete (string as_path, string as_sheet);//删除指定sheet,前提是as已经打开 if of_check(as_path,as_sheet) < 0 then return -1 if of_sheetselect(as_path, as_sheet) < 0 then return -1 try excel.workbooks(of_splitpath(as_path, 2)).sheets(as_sheet).Delete//选择文件中一个工作表(按sheet名称选择,sheet名称确定的情况下使用) catch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回-1就好了 return -1 end try return 1 end function public function long of_get_firstemprow (string as_path, string as_sheet, long al_col[]);//显示所有数据 if of_check(as_path,as_sheet) < 0 then return -1 boolean lb long row,ll_rowcount int i string ls if upperbound(al_col) = 0 then return -1 ll_rowcount = of_rowcount(as_path, as_sheet) for row = ll_rowcount to 1 step -1 ls = '' for i = 1 to upperbound(al_col) ls += of_getvalue(as_path, as_sheet, row, al_col[i]) next if ls = '' then continue end if exit next return row end function public function integer of_setvalue (string as_path, integer ai_sheet, long row, string as_col, string as_value);//设置单元格的内容 if of_check() < 0 then return -1 long col col = of_get_num_colid( as_col) return of_setvalue(as_path, ai_sheet, row, col, as_value) end function public function integer of_setvalue (string as_path, string as_sheet, long row, string as_col, string as_value);//设置单元格的内容 if of_check() < 0 then return -1 long col col = of_get_num_colid( as_col) return of_setvalue(as_path, as_sheet, row, col, as_value) end function public function string of_relative_path (string a1, string a2);// //如:在a1中做超链接到a2:a1 = "c:/abc/a.xls", a2 = "c:/abc/def/b.xls" string ls int i a1 = lower(a1); a2 = lower(a2) if a1 = a2 then return '' //同一个工作簿,返回空值 do while true i = posw(a1, '/') if i <= 0 then else ls = leftw(a1, i) if leftw(a2, lenw(ls)) = ls and leftw(a2, 3) <> '../' then //相同 a1 = midw(a1, i + 1); a2 = midw(a2, i+ 1) else a1 = midw(a1, i + 1);a2 = "../" + a2 end if end if loop return a2 end function public 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 -1 if of_check(as_path,as_sheet) < 0 then return -1 try excel.activewindow.scrollrow = row//workbooks(of_splitpath(as_path, 2)).sheets(as_sheet). catch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回-1就好了 return -1 end try return 1 end function public 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 -1 if of_check(as_path,as_sheet) < 0 then return -1 try excel.activewindow.scrollcolumn = col catch( runtimeerror er) //出错的原因可能是工作簿并没有打开,直接返回-1就好了 return -1 end try return 1 end function public 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 // excel.Application.DisplayStatusBar = ast.DisplayStatusBar // excel.Application.EnableEvents = ast.EnableEvents // excel.Application.Calculation = ast.Calculation // excel.Application.CalculateBeforeSave = ast.CalculateBeforeSave //// excel.Workbooks(of_splitpath(as_path, 2)).sheets(as_sheet).DisplayPageBreaks = ast.DisplayPageBreaks //end if //of_save(as_path) //return lst end subroutine on nvo_excel.create call super::create TriggerEvent( this, "constructor" ) end on on nvo_excel.destroy TriggerEvent( this, "destructor" ) call super::destroy end on event constructor;of_connect() end event event destructor;of_disconnect() end event

你可能感兴趣的:(工作,String,function,Excel,Integer,Path)