PB 读取 Excel

PB 读取 Excel
string ls_fileName
ls_fileName = "c:\x1.xls"

OLEObject ole_excel 
ole_excel = CREATE OLEObject 
IF ole_excel.ConnectToNewObject( "Excel.Application") <> 0 Then 
   MessageBox( " ""Connect Excel error!"
    return
END IF

ole_excel.workbooks.open(ls_fileName,0,FALSE

int li_RowsCount,li_ColumnsCount
string ls_cell
// Get Rows Count
// li_RowsCount = ole_excel.Workbooks[1].Sheets[1].UsedRange.Rows.Count
li_RowsCount=0
DO WHILE TRUE
   ls_cell = string(ole_excel.Workbooks[1].Sheets[1].Cells(li_RowsCount + 1,1).Value)
   IF isnull(ls_cell) THEN
          EXIT
    ELSE
        li_RowsCount++
    END IF
LOOP
// Get Columns Count
// li_ColumnsCount = ole_excel.Workbooks[1].Sheets[1].UsedRange.Columns.Count
li_ColumnsCount=0
DO WHILE TRUE
   ls_cell = string(ole_excel.Workbooks[1].Sheets[1].Cells(1,li_ColumnsCount + 1).Value)
   IF isnull(ls_cell) THEN
          EXIT
    ELSE
        li_ColumnsCount++
    END IF
LOOP
string msg 
msg = "Rows count: " + string(li_RowsCount) + "~r~nColumns count: " + string(li_ColumnsCount)
messagebox("",msg)

// Get Cell Value
ls_cell = string(ole_excel.Workbooks[1].Sheets[1].Cells(2,1).Value)
if isnull(ls_cell) then
    messagebox("Cell Value","Cell is null")
else
    messagebox("Cell Value",ls_cell)
end if

ole_excel.Quit() 
ole_excel.DisConnectObject() 
Destroy ole_excel
PS: 不知道还有多人用PB 呵呵

posted on 2011-09-21 11:51  大豆男生  阅读(1218)  评论(0编辑  收藏  举报

导航