Excel的数据导入到PB的DataWindow中
//====================================================================
// Event:cb_1::clicked()
//--------------------------------------------------------------------
// Description:两种方法进行
//1、通过数组一行一行读入(定义数据有点麻烦)
//2、通过剪贴板直接
//网友自行选择用哪种方法
//--------------------------------------------------------------------
// Arguments:(None)
//--------------------------------------------------------------------
// Returns:
//--------------------------------------------------------------------
// Author: FlyStone Date: 2008-09-13 17:31:44 Sep
//--------------------------------------------------------------------
// Modify History:
//
//--------------------------------------------------------------------
// Copyright (C) 2007-008 Flystone Co.,Ltd! All rights reserved.
//====================================================================
String ls_path,ls_name
ls_path = "C:\Documents and Settings\Administrator.FLYSTONE\桌面\新建文件夹 (3)\test.xls"
dw_1.Reset() //clean DW
String a[1000,1000]
Integer i,j,li_ret
li_ret = GetFileopenName('选择XLS',ls_path,ls_name,"XLS","EXCEL (*.XLS),*.XLS")
if li_ret <> 1 then return
OLEObject ExcelServer
Int li_ConnectErr
ExcelServer = Create OLEObject
li_ConnectErr = ExcelServer.ConnectToNewObject( "excel.application" )
If li_ConnectErr < 0 Then
Choose Case li_ConnectErr
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
End If
ExcelServer.Workbooks.Open(ls_path,0,False)
//对XLS文件进行了改动以后,在关闭该文件时是否需要向用户提出警告:剪贴板提示信息。
ExcelServer.Application.DisplayAlerts = False
//选择sheets表
//ExcelServer.activeworkbook.worksheets("sheet2").Select()
//方法一:
Int li_rows,li_columns
li_rows = ExcelServer.ActiveSheet.UsedRange.Rows.Count //取得总行数
li_columns= ExcelServer.ActiveSheet.UsedRange.columns.Count //取得总行数
// # of columns in excel
sle_2.Text = TRIM(STRING(li_rows))
// # of rows in excel
sle_1.Text = STRING(li_columns)
//lole_sheet = ole_1.Application.ActiveWorkbook.WorkSheets[1] //得到第当前work的第一个sheet
for i = 1 to li_rows
dw_1.insertrow(0)
for j = 1 to li_columns
a[i,j] = string(ExcelServer.ActiveSheet.Cells(i,j).value)
dw_1.Setitem(i,j,a[i,j])
end for
end for
//方法二,利用剪贴板
ExcelServer.ActiveSheet.cells.Copy
dw_1.ImportClipboard(2) //导入数据需要标题
ExcelServer.Application.activeworkbook.Close(False)
ExcelServer.Application.quit()
ExcelServer.DisconnectObject()
-----------------------------------------------------------------------
global type gf_import_excel from function_object
end type
forward prototypes
global subroutine gf_import_excel (datawindow idw_dw)
end prototypes
global subroutine gf_import_excel (datawindow idw_dw);string path,filename
integer value,result
value = GetFileOpenName("Select File",path,filename,"xls","xls Files (*.xls),*.xls,Text Files (*.TXT),*.TXT,DBF Files (*.DBF),*.DBF")
if value<>1 then return
if right(path,3)='xls' or right(path,3)='XLS' then
OLEObject ObjExcel
ObjExcel = CREATE OLEObject
result = ObjExcel.ConnectToNewObject( "excel.application")
if result <> 0 then
messagebox("信息提示","连接EXCEL失败,请检查计算机中是否安装了EXCEL!")
Return
else
ObjExcel.Workbooks.Open(path) //ls_rj是文件的路径
ObjExcel.ActiveSheet.Cells.Copy
ObjExcel.Application.Visible = false
idw_dw.SetTransObject(SQLCA)
idw_dw.ImportClipboard(2) //将系统剪切版上的内容粘贴到数据窗口中,其中2为起始行参数
Clipboard("") //请空剪切版上的内容
ObjExcel.Quit()
ObjExcel.DisconnectObject() //断开与OLE的连接
Destroy ObjExcel
end if
elseif right(path,3)='txt' or right(path,3)='TXT' then
idw_dw.importfile(path,2)
elseif right(path,3)='dbf' or right(path,3)='DBF' then
idw_dw.importfile(path,1)
end if
end subroutine