ljx-read-excel-data、ljx-read-excel-data、Ljx-vlxls-get-range-value三个函数在收集的大量的函数的支持下运行成功,但这确实显得过于复杂繁琐,多年来自己收集了大量的函数,但换一个人来用就非常不方便,从自己使用的体会来看,越不要外挂,使用系统自带的东西就能运行,越是方便适用,来了点兴趣啊,按以上思路,不依靠那些类型库声明代码、不依靠大量mslx函数和自定义函数就能运行,现基本搞成,在自己的机子上运行良好,以下是经过修改后的函数和运行实例,excel表还是用原来的"ABC.xls"文件啊:
- ;;;;;读取excel数据函数
- (defun Ljx-read-excel-data1 (vv hor sh) ;;;vv为列数直接输入字母、hor为 第几行,直接输入数字,sh为工作表对象sheet
- (vlax-variant-value (vlax-get-property (vlax-get-property sh 'Range (strcat vv (itoa hor))) 'Value2))
- )
- ;;;;;读取excel数据函数输出text格式
- (defun Ljx-read-excel-text1 (vv hor sh ) ;;;vv为列数直接输入字母、hor为 第几行,直接输入数字,sh为工作表对象sheet
- (vl-princ-to-string (vlax-variant-value (vlax-get-property (vlax-get-property sh 'Range (strcat vv (itoa hor))) 'Value2)))
- )
- ;;;;;;取得单元格数据,支持区域输入
- (defun ljx-vlxls-get-range-value1 ( sheet rangeid / range value valuelist )
- (setq range (vlax-get-property sheet 'Range rangeid))
- (setq value (vlax-get-property range 'Value2))
- (cond
- ((= (vlax-variant-type value) 8204);;;为数组时,即为区域;
- (progn
- (setq value (vlax-safearray->list(vlax-variant-value value)))
- (setq valuelist (mapcar (function (lambda (x) (mapcar 'vlax-variant-value x) )) value))
- )
- )
- ( T;;;;为单个单元格;
- (setq valuelist (vlax-variant-value value))
- )
- );;;;cond
- valuelist
- )
- ;;;;运行函数
- (defun test1 ()
- (vl-load-com)
- (setq exname "d:\\ABC.xls")
- (setq *excel* (vlax-create-object "excel.application"))
- (setq *xlapp* (vlax-invoke-method (vlax-get-property *excel* 'Workbooks) 'Open exname))
- (vla-put-visible *excel* 1)
- (setq *sheet* (vlax-get-property (vlax-get-property *xlapp* 'Worksheets) 'item "DEF"))
- (setq dat0 (Ljx-vlxls-get-range-value1 *sheet* "E5")
- dat1 (Ljx-vlxls-get-range-value1 *sheet* "E5:F5");;;;输出:((-25.10 "张三"))
- dat2 (Ljx-vlxls-get-range-value1 *sheet* "E5:E6");;;;输出:((-25.10)(26.85))
- dat3 (Ljx-vlxls-get-range-value1 *sheet* "E5:F6");;;;输出:((-25.10 "张三")(26.85 "李四"))
- dat4 (Ljx-read-excel-data1 "E" 5 *sheet*);;;;-->-25.10
- dat5 (Ljx-read-excel-text1 "E" 5 *sheet* );;;;-->"-25.10"
- dat6 (Ljx-read-excel-data1 "E" 6 *sheet* );;;;-->26.85
- dat7 (Ljx-read-excel-text1 "E" 6 *sheet*);;;;-->"26.85"
- dat8 (Ljx-read-excel-data1 "F" 5 *sheet*);;;;-->“张三”
- dat9 (Ljx-read-excel-text1 "F" 5 *sheet*);;;;-->“张三”
- dat10 (Ljx-read-excel-data1 "F" 6 *sheet* );;;;-->"李四"
- dat11 (Ljx-read-excel-text1 "F" 6 *sheet* );;;;-->"李四"
- )
- (vlax-invoke-method (vlax-get-property *excel* "ActiveWorkbook") 'Close 0)
- (vlax-invoke-method *excel* 'QUIT)
- (vlax-release-object *sheet*)
- (vlax-release-object *xlapp*)
- )
- ;;;;具体使用时先将ABC.xls(解压ABC.zip)拷贝至d盘根目录下,加载test1.lsp文件,在CAD命令行输入 (test1)即可运行
|