本帖最后由 作者 于 2009-5-25 20:33:40 编辑
通过在明经、晓东里边转了一圈又一圈之后终于能从Excel里边把单元格的数据读出来了。但是,还有个大问题解决不了: 1. (vla-put-visible xlapp 1);1-可见,0-隐藏 这一句把所有打开的Excel文件都给隐藏掉了!!!!!
2.同样的 (vlax-invoke-method xlapp 'quit) 这一句把所有打开的Excel文件都给关掉了!!!!!!!!!!!! 如何处理,能够让我可以把数据读出来且不影响其它打开的Excel文件?或者这么说:是否可以不打开指定的Excel文件就能将其中的数据读出来?
各位大神给伸伸手指点下吧,谢谢啦…… (defun excel-get-data (/ xlapp xlfile fn H8) (vl-load-com) ;读取单元格数据 (defun Excel-Get-CellValue(xlapp cell / xlsrng xlsval) (setq xlsrng (vlax-get-property xlapp "range" cell)) (setq xlsval (vlax-variant-value (vlax-get-property xlsrng "Value"))) ) (setq xfile (getfiled "打开法兰计算文件" "" "xls" 8)) (if (setq fn (findfile xfile)) (if (setq xlapp (vlax-get-or-create-object "Excel.Application")) (progn (vlax-invoke-method (vlax-get-property xlapp 'WorkBooks) 'Open fn ) (vla-put-visible xlapp 0);1-可见,0-隐藏 ;单元格数据读取 (setq H8 (Excel-Get-CellValue xlapp "H8")) ;退出并关闭Excel进程 (vlax-invoke-method xlapp 'quit) (vlax-release-object xlapp) ) ) ) H8 ) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
问题已解决 下面是测试的程序: ;;;取出Excel文件中第一个工作表中指定单元格的数据 (defun c:ttt () (Excel-Get-data) ) (defun Excel-Get-data ( / xfile cell ADOCONNECT ADORECORDSET ConnectionString lst Sheet-name source cell-tmp cell-value) ;指定单元格的数据 (defun Excel-Get-CellValue (Sheet-name cell) (setq source (strcat "SELECT * FROM [" Sheet-name cell":" cell "]")) (vlax-invoke-method ADORecordset "Open" source ADOConnect 1 3 nil) (setq cell-tmp (vlax-safearray->list (vlax-variant-value (vlax-invoke-method ADORecordset "GetRows" 1)))) (vlax-variant-value (car (car cell-tmp))) ) (setq xfile (getfiled "打开法兰计算文件" "" "xls" 8)) (setq ADOConnect (vlax-get-or-create-object "ADODB.Connection")) (setq ADORecordset (vlax-get-or-create-object "ADODB.Recordset")) (setq ConnectionString (strcat "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" xfile ";Extended Properties=;Excel 8.0;HDR=No" )) (if (not (vl-catch-all-error-p (vl-catch-all-apply (function vlax-invoke-method) (list ADOConnect "Open" ConnectionString "admin" "" nil) ) ) ) (progn (setq lst (vlax-safearray->list (vlax-variant-value (vlax-invoke-method (vlax-invoke-method ADOConnect "OpenSchema" 4 ) "GetRows" 1) ) ) ) (setq Sheet-name (vlax-variant-value (car (caddr lst))));确定"第一个"工作表的名称 (setq cell-value (Excel-Get-CellValue Sheet-name "H16"));H16单元格 (vlax-invoke-method ADORecordset "Close") (vlax-invoke-method ADOConnect "Close") ) (progn (princ "\n打开Excel数据文件出错") (vl-catch-all-apply 'vlax-invoke-method (list ADOConnect "Close")) (setq cell-value nil) ) ) (vlax-release-object ADORecordset) (vlax-release-object ADOConnect) cell-value ) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 不用ADO原来也是可以实现的 ;退出并关闭Excel进程 (vlax-invoke-method xlapp 'quit) (vlax-release-object xlapp)上边两句改成以下内容即可 ;退出并关闭Excel文件 (vlax-invoke-method (vlax-get-property xlapp 'ActiveWorkbook) 'close ) |