yxh1202 发表于 2013-12-26 10:20:10

关于单元格地址为变量时读取excel数据

本帖最后由 yxh1202 于 2014-11-18 15:18 编辑

看了好多的文章,都是指定单元格地址读取excel工作表中的数据,其实我遇到的问题是,需要判断工作表中符合条件的单元格,然后找到相应的有用的数据,自己编了一个,但返回结果为0。

excel表格中数据格式为:

桩号          X坐标   Y坐标   。。。。
0               1456      1234
150            4587      1000
300            3522       1526

想通过内插求125桩号处的X坐标   Y坐标,求高人指点。

;; Example demonstrating how to read the plain text content of all cells within
;; the UsedRange of the active worksheet of a supplied Excel file.
;; - Lee Mac-2013-10-29
(vl-load-com)
(defun c:rexl
              (/ xls xlapp xlcls xlcol xlrng xlrow xlsht xlwbk xlwbs)
(if
    (and
      (setq xlapp (vlax-get-or-create-object "excel.application"))
      (setq xls (getfiled "打开纵断面文件" "" "xls" 8))

    )
   (progn
       (setq xlwbs (vlax-get-property xlapp 'workbooks)
             xlwbk (vlax-invoke-method xlwbs 'open xls)
             xlsht (vlax-get-property xlapp 'activesheet)
             xlrng (vlax-get-property xlsht 'usedrange)
             xlcls (vlax-get-property xlrng 'cells)
             xlrow (vlax-get-property xlrng 'rows)
             xlcol (vlax-get-property xlrng 'columns)
       )
       (repeat (setq row (vlax-get-property xlrow 'count))
       (setq dyvlue (vlax-variant-value
                        (vlax-get-property
                          (vlax-variant-value
                          (vlax-get-property xlcls 'item row 2)
                          )
                          'value
                        )
                      )
       )
       (if (<= dyvalue 125)
           row
       )                                ;endif
       (setq row (1- row))
       )                                ;endrepeat
       (if (= 'vla-object (type xlwbk))
       (vl-catch-all-apply
           'vlax-invoke-method
           (list xlwbk 'close :vlax-false)
       )
       )
       (vl-catch-all-apply 'vlax-invoke-method (list xlapp 'quit))
       (foreach        obj
                (list xlcol xlrow xlcls xlrng xlsht xlwbk xlwbs xlapp)
       (if (= 'vla-object (type obj))
           (vlax-release-object obj)
       )
       )
       (gc)
       (if (vl-catch-all-error-p row)
       (prompt
           (strcat "\nError: " (vl-catch-all-error-message row))
       )
       row
       )
   )                                        ;prong结束
   (prompt "\nUnable to interface with Excel Application.")
)
)
(vl-load-com)
(princ)

yxh1202 发表于 2014-2-25 16:20:29

本帖最后由 yxh1202 于 2014-2-25 16:22 编辑

搞了好久才弄好,但是在家里的电脑上能用,换一台机子不行,提示
命 令: ty
【错误】参数类型错误: VLA-OBJECT nil!,都是CAD2010和excel2010,求高人指点:
(defun c:ty
          (/ excelapp0 activeworkbook0 activesheet0 cells0)
(vl-load-com)
;;简单示例lisp读取excel里的数据(没有判断容错功能)by snddd2000 2011-11-17
(setq xfile (getfiled "打开纵断面文件" "" "xls" 8))
(setq excelapp0 (vlax-get-object "Excel.Application"))
                                        ;excel程序对象,没开excel下边的都是白搭
(setq activeworkbook0 (vlax-get-property excelapp0 'ActiveWorkbook))
                                        ;excel工作簿对象
(setq activesheet0 (vlax-get-property activeworkbook0 'ActiveSheet))
                                        ;excel工作表对象
(setq cells0 (vlax-get-property activesheet0 'cells))
                                        ;excel单元格对象
(setq        aLastcell (vlax-get-property
                  cells0
                  'item
                  (vlax-get-property
                      (vlax-get-property
                        (vlax-get-property cells0 'cells)
                        'rows
                      )
                      'count
                  )
                  1
                  )
)

(setq        endxluprow0 ;_A列有数据的最后一行的行号
       (vlax-get-property
           (vlax-get-property
             (vlax-variant-value aLastcell)
             'end
             -4162
           )
           'row
       )
)
(setq k 3
        lc (getreal "\n请输入查询的桩号"))

(repeat endxluprow0
    (setq vv
           (vlxls-get-cellvalue cells0 k 2)
    )
    (if        (> lc vv)
      (setq k (1+ k))
      (setq q k)
    )
)
(setq dd (vlxls-cell-leicha lc q))
(princ dd)
(mapcar 'vlax-release-object
          (list cells0 activesheet0 activeworkbook0 excelapp0)
)
(gc)
(princ)
)

;;网上找的,返回一个单元格range地址
(defun vlxls-get-cell (obj row col / item cells)
(setq        item (vlax-get-property
             (setq cells (vlax-get-property obj "Cells"))
             "Item"
             (vlax-make-variant row)
             (vlax-make-variant col)
             )
)
(vlax-release-object cells)
(vlax-variant-value item)
)
;;=====返回单元格的值------
(defun vlxls-get-cellvalue (obj row col / item cells cellvalue)
(setq        item (vlax-get-property
             (setq cells (vlax-get-property obj "Cells"))
             "Item"
             (vlax-make-variant row)
             (vlax-make-variant col)
             )
)
(vlax-release-object cells)
(vlax-variant-value item)
(setq        cellvalue
       (vlax-variant-value
           (vlax-get-property
             (vlxls-get-cell obj row col)
             'value
             ;;或者'value
           )
       )
       ;;单元格里面的值

)
)

;;===内插求值函数=======
(defun vlxls-cell-leicha (lc row@ / a1 a2 lc2)
(if (= lc (vlxls-get-cellvalue cells0 row@ 2))
    (setq a (vlxls-get-cellvalue cells0 row@ 4))
    (setq a1(vlxls-get-cellvalue cells0 row@ 4)
          a2(vlxls-get-cellvalue cells0 (- row@ 1) 4)
          lc1 (vlxls-get-cellvalue cells0 row@ 2)
          lc2 (vlxls-get-cellvalue cells0 (- row@ 1) 2)
          a   (+ (* (/ (- a1 a2) (- lc1 lc2)) (- lc lc2)) a2)
    )
)
)


yxh1202 发表于 2014-6-30 18:00:26

怎么都没人解答下呢,高手都去抄笔记去了吗

xiabin68 发表于 2014-11-13 15:26:57

我也在找这方面的知识,请问你解决没有?

yxh1202 发表于 2014-11-13 17:05:10

xiabin68 发表于 2014-11-13 15:26 static/image/common/back.gif
我也在找这方面的知识,请问你解决没有?

弄好啦,请问你是做什么的

xyp1964 发表于 2014-11-13 20:21:44

”excel表格中数据格式为:
桩号          X坐标   Y坐标   。。。。
0               1456      1234
150            4587      1000
300            3522       1526
想通过内插求125桩号处的X坐标   Y坐标“

内插的含义不详;桩号是否连续?最好给出excel文件。

可爱的神爱世人 发表于 2014-11-16 21:22:47

学习中,,,,努力,,,

xiabin68 发表于 2014-12-1 21:14:17

yxh1202 发表于 2014-11-13 17:05 static/image/common/back.gif
弄好啦,请问你是做什么的

搞测量的,,,,,,,,,,
页: [1]
查看完整版本: 关于单元格地址为变量时读取excel数据