明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 629|回复: 1

[提问] 求助,这个GetExcel程序怎么修改成能用的,请大家解决

[复制链接]
发表于 2015-7-13 12:55 | 显示全部楼层 |阅读模式
在其他论坛上看到如下一个LISP函数,觉得很好,但是不会修改整理,望各位大侠整理,造福明经大众。
  1. ;-------------------------------------------------------------------------------
  2. ; Program Name: GetExcel.lsp [GetExcel R4]
  3. ; Created By:   Terry Miller (Email: terrycadd@yahoo.com)
  4. ;               (URL: http://web2.airmail.net/terrycad)
  5. ; Date Created: 9-20-03
  6. ; Function:     Several functions to get and put values into Excel cells.
  7. ;-------------------------------------------------------------------------------
  8. ; Revision History
  9. ; Rev  By     Date    Description
  10. ;-------------------------------------------------------------------------------
  11. ; 1    TM   9-20-03   Initial version
  12. ; 2    TM   8-20-07   Rewrote GetExcel.lsp and added several new sub-functions
  13. ;                     including ColumnRow, Alpha2Number and Number2Alpha written
  14. ;                     by Gilles Chanteau from Marseille, France.
  15. ; 3    TM   12-1-07   Added several sub-functions written by Gilles Chanteau
  16. ;                     including Cell-p, Row+n, and Column+n. Also added his
  17. ;                     revision of the PutCell function.
  18. ; 4    GC   9-20-08   Revised the GetExcel argument MaxRange$ to accept a nil
  19. ;                     and get the current region from cell A1.
  20. ;-------------------------------------------------------------------------------
  21. ; Overview of Main functions
  22. ;-------------------------------------------------------------------------------
  23. ; GetExcel - Stores the values from an Excel spreadsheet into *ExcelData@ list
  24. ;   Syntax:  (GetExcel ExcelFile$ SheetName$ MaxRange$)
  25. ;   Example: (GetExcel "C:\\Folder\\Filename.xls" "Sheet1" "L30")
  26. ; GetCell - Returns the cell value from the *ExcelData@ list
  27. ;   Syntax:  (GetCell Cell$)
  28. ;   Example: (GetCell "H15")
  29. ; Function example of usage:
  30. ; (defun c:Get-Example ()
  31. ;   (GetExcel "C:\\Folder\\Filename.xls" "Sheet1" "L30");<-- Edit Filename.xls
  32. ;   (GetCell "H21");Or you can just use the global *ExcelData@ list
  33. ; );defun
  34. ;-------------------------------------------------------------------------------
  35. ; OpenExcel - Opens an Excel spreadsheet
  36. ;   Syntax:  (OpenExcel ExcelFile$ SheetName$ Visible)
  37. ;   Example: (OpenExcel "C:\\Folder\\Filename.xls" "Sheet1" nil)
  38. ; PutCell - Put values into Excel cells
  39. ;   Syntax:  (PutCell StartCell$ Data$) or (PutCell StartCell$ DataList@)
  40. ;   Example: (PutCell "A1" (list "GP093" 58.5 17 "Base" "3'-6 1/4""))
  41. ; CloseExcel - Closes Excel session
  42. ;   Syntax:  (CloseExcel ExcelFile$)
  43. ;   Example: (CloseExcel "C:\\Folder\\Filename.xls")
  44. ; Function example of usage:
  45. ; (defun c:Put-Example ()
  46. ;   (OpenExcel "C:\\Folder\\Filename.xls" "Sheet1" nil);<-- Edit Filename.xls
  47. ;   (PutCell "A1" (list "GP093" 58.5 17 "Base" "3'-6 1/4""));Repeat as required
  48. ;   (CloseExcel "C:\\Folder\\Filename.xls");<-- Edit Filename.xls
  49. ;   (princ)
  50. ; );defun
  51. ;-------------------------------------------------------------------------------
  52. ; Note: Review the conditions of each argument in the function headings
  53. ;-------------------------------------------------------------------------------
  54. ; GetExcel - Stores the values from an Excel spreadsheet into *ExcelData@ list
  55. ; Arguments: 3
  56. ;   ExcelFile$ = Path and filename
  57. ;   SheetName$ = Sheet name or nil for not specified
  58. ;   MaxRange$ = Maximum cell ID range to include or nil to get the current region from cell A1
  59. ; Syntax examples:
  60. ; (GetExcel "C:\\Temp\\Temp.xls" "Sheet1" "E19") = Open C:\Temp\Temp.xls on Sheet1 and read up to cell E19
  61. ; (GetExcel "C:\\Temp\\Temp.xls" nil "XYZ123") = Open C:\Temp\Temp.xls on current sheet and read up to cell XYZ123
  62. ;-------------------------------------------------------------------------------
  63. (defun GetExcel (ExcelFile$ SheetName$ MaxRange$ / Column# ColumnRow@ Data@ ExcelRange^
  64.   ExcelValue ExcelValue ExcelVariant^ MaxColumn# MaxRow# Range$ Row# Worksheet)
  65.   (if (= (type ExcelFile$) 'STR)
  66.     (if (not (findfile ExcelFile$))
  67.       (progn
  68.         (alert (strcat "Excel file " ExcelFile$ " not found."))
  69.         (exit)
  70.       );progn
  71.     );if
  72.     (progn
  73.       (alert "Excel file not specified.")
  74.       (exit)
  75.     );progn
  76.   );if
  77.   (gc)
  78.   (if (setq *ExcelApp% (vlax-get-object "Excel.Application"))
  79.     (progn
  80.       (alert "Close all Excel spreadsheets to continue!")
  81.       (vlax-release-object *ExcelApp%)(gc)
  82.     );progn
  83.   );if
  84.   (setq ExcelFile$ (findfile ExcelFile$))
  85.   (setq *ExcelApp% (vlax-get-or-create-object "Excel.Application"))
  86.   (vlax-invoke-method (vlax-get-property *ExcelApp% 'WorkBooks) 'Open ExcelFile$)
  87.   (if SheetName$
  88.     (vlax-for Worksheet (vlax-get-property *ExcelApp% "Sheets")
  89.       (if (= (vlax-get-property Worksheet "Name") SheetName$)
  90.         (vlax-invoke-method Worksheet "Activate")
  91.       );if
  92.     );vlax-for
  93.   );if
  94.   (if MaxRange$
  95.     (progn
  96.       (setq ColumnRow@ (ColumnRow MaxRange$))
  97.       (setq MaxColumn# (nth 0 ColumnRow@))
  98.       (setq MaxRow# (nth 1 ColumnRow@))
  99.     );progn
  100.     (progn
  101.       (setq CurRegion (vlax-get-property (vlax-get-property
  102.         (vlax-get-property *ExcelApp% "ActiveSheet") "Range" "A1") "CurrentRegion")
  103.       );setq
  104.       (setq MaxRow# (vlax-get-property (vlax-get-property CurRegion "Rows") "Count"))
  105.       (setq MaxColumn# (vlax-get-property (vlax-get-property CurRegion "Columns") "Count"))
  106.     );progn
  107.   );if
  108.   (setq *ExcelData@ nil)
  109.   (setq Row# 1)
  110.   (repeat MaxRow#
  111.     (setq Data@ nil)
  112.     (setq Column# 1)
  113.     (repeat MaxColumn#
  114.       (setq Range$ (strcat (Number2Alpha Column#)(itoa Row#)))
  115.       (setq ExcelRange^ (vlax-get-property *ExcelApp% "Range" Range$))
  116.       (setq ExcelVariant^ (vlax-get-property ExcelRange^ 'Value))
  117.       (setq ExcelValue (vlax-variant-value ExcelVariant^))
  118.       (setq ExcelValue
  119.         (cond
  120.           ((= (type ExcelValue) 'INT) (itoa ExcelValue))
  121.           ((= (type ExcelValue) 'REAL) (rtosr ExcelValue))
  122.           ((= (type ExcelValue) 'STR) (vl-string-trim " " ExcelValue))
  123.           ((/= (type ExcelValue) 'STR) "")
  124.         );cond
  125.       );setq
  126.       (setq Data@ (append Data@ (list ExcelValue)))
  127.       (setq Column# (1+ Column#))
  128.     );repeat
  129.     (setq *ExcelData@ (append *ExcelData@ (list Data@)))
  130.     (setq Row# (1+ Row#))
  131.   );repeat
  132.   (vlax-invoke-method (vlax-get-property *ExcelApp% "ActiveWorkbook") 'Close :vlax-False)
  133.   (vlax-invoke-method *ExcelApp% 'Quit)
  134.   (vlax-release-object *ExcelApp%)(gc)
  135.   (setq *ExcelApp% nil)
  136.   *ExcelData@
  137. );defun GetExcel
  138. ;-------------------------------------------------------------------------------
  139. ; GetCell - Returns the cell value from the *ExcelData@ list
  140. ; Arguments: 1
  141. ;   Cell$ = Cell ID
  142. ; Syntax example: (GetCell "E19") = value of cell E19
  143. ;-------------------------------------------------------------------------------
  144. (defun GetCell (Cell$ / Column# ColumnRow@ Return Row#)
  145.   (setq ColumnRow@ (ColumnRow Cell$))
  146.   (setq Column# (1- (nth 0 ColumnRow@)))
  147.   (setq Row# (1- (nth 1 ColumnRow@)))
  148.   (setq Return "")
  149.   (if *ExcelData@
  150.     (if (and (>= (length *ExcelData@) Row#)(>= (length (nth 0 *ExcelData@)) Column#))
  151.       (setq Return (nth Column# (nth Row# *ExcelData@)))
  152.     );if
  153.   );if
  154.   Return
  155. );defun GetCell
  156. ;-------------------------------------------------------------------------------
  157. ; OpenExcel - Opens an Excel spreadsheet
  158. ; Arguments: 3
  159. ;   ExcelFile$ = Excel filename or nil for new spreadsheet
  160. ;   SheetName$ = Sheet name or nil for not specified
  161. ;   Visible = t for visible or nil for hidden
  162. ; Syntax examples:
  163. ; (OpenExcel "C:\\Temp\\Temp.xls" "Sheet2" t) = Opens C:\Temp\Temp.xls on Sheet2 as visible session
  164. ; (OpenExcel "C:\\Temp\\Temp.xls" nil nil) = Opens C:\Temp\Temp.xls on current sheet as hidden session
  165. ; (OpenExcel nil "Parts List" nil) =  Opens a new spreadsheet and creates a Part List sheet as hidden session
  166. ;-------------------------------------------------------------------------------
  167. (defun OpenExcel (ExcelFile$ SheetName$ Visible / Sheet$ Sheets@ Worksheet)
  168.   (if (= (type ExcelFile$) 'STR)
  169.     (if (findfile ExcelFile$)
  170.       (setq *ExcelFile$ ExcelFile$)
  171.       (progn
  172.         (alert (strcat "Excel file " ExcelFile$ " not found."))
  173.         (exit)
  174.       );progn
  175.     );if
  176.     (setq *ExcelFile$ "")
  177.   );if
  178.   (gc)
  179.   (if (setq *ExcelApp% (vlax-get-object "Excel.Application"))
  180.     (progn
  181.       (alert "Close all Excel spreadsheets to continue!")
  182.       (vlax-release-object *ExcelApp%)(gc)
  183.     );progn
  184.   );if
  185.   (setq *ExcelApp% (vlax-get-or-create-object "Excel.Application"))
  186.   (if ExcelFile$
  187.     (if (findfile ExcelFile$)
  188.       (vlax-invoke-method (vlax-get-property *ExcelApp% 'WorkBooks) 'Open ExcelFile$)
  189.       (vlax-invoke-method (vlax-get-property *ExcelApp% 'WorkBooks) 'Add)
  190.     );if
  191.     (vlax-invoke-method (vlax-get-property *ExcelApp% 'WorkBooks) 'Add)
  192.   );if
  193.   (if Visible
  194.     (vla-put-visible *ExcelApp% :vlax-true)
  195.   );if
  196.   (if (= (type SheetName$) 'STR)
  197.     (progn
  198.       (vlax-for Sheet$ (vlax-get-property *ExcelApp% "Sheets")
  199.         (setq Sheets@ (append Sheets@ (list (vlax-get-property Sheet$ "Name"))))
  200.       );vlax-for
  201.       (if (member SheetName$ Sheets@)
  202.         (vlax-for Worksheet (vlax-get-property *ExcelApp% "Sheets")
  203.           (if (= (vlax-get-property Worksheet "Name") SheetName$)
  204.             (vlax-invoke-method Worksheet "Activate")
  205.           );if
  206.         );vlax-for
  207.         (vlax-put-property (vlax-invoke-method (vlax-get-property *ExcelApp% "Sheets") "Add") "Name" SheetName$)
  208.       );if
  209.     );progn
  210.   );if
  211.   (princ)
  212. );defun OpenExcel
  213. ;-------------------------------------------------------------------------------
  214. ; PutCell - Put values into Excel cells
  215. ; Arguments: 2
  216. ;   StartCell$ = Starting Cell ID
  217. ;   Data@ = Value or list of values
  218. ; Syntax examples:
  219. ; (PutCell "A1" "PART NUMBER") = Puts PART NUMBER in cell A1
  220. ; (PutCell "B3" '("Dim" 7.5 "9.75")) = Starting with cell B3 put Dim, 7.5, and 9.75 across
  221. ;-------------------------------------------------------------------------------
  222. (defun PutCell (StartCell$ Data@ / Cell$ Column# ExcelRange Row#)
  223.   (if (= (type Data@) 'STR)
  224.     (setq Data@ (list Data@))
  225.   )
  226.   (setq ExcelRange (vlax-get-property *ExcelApp% "Cells"))
  227.   (if (Cell-p StartCell$)
  228.     (setq Column# (car (ColumnRow StartCell$))
  229.           Row# (cadr (ColumnRow StartCell$))
  230.     );setq
  231.     (if (vl-catch-all-error-p
  232.           (setq Cell$ (vl-catch-all-apply 'vlax-get-property
  233.             (list (vlax-get-property *ExcelApp% "ActiveSheet") "Range" StartCell$))
  234.           );setq
  235.         );vl-catch-all-error-p
  236.         (alert (strcat "The cell ID "" StartCell$ "" is invalid."))
  237.         (setq Column# (vlax-get-property Cell$ "Column")
  238.               Row# (vlax-get-property Cell$ "Row")
  239.         );setq
  240.     );if
  241.   );if
  242.   (if (and Column# Row#)
  243.     (foreach Item Data@
  244.       (vlax-put-property ExcelRange "Item" Row# Column# (vl-princ-to-string Item))
  245.       (setq Column# (1+ Column#))
  246.     );foreach
  247.   );if
  248.   (princ)
  249. );defun PutCell
  250. ;-------------------------------------------------------------------------------
  251. ; CloseExcel - Closes Excel spreadsheet
  252. ; Arguments: 1
  253. ;   ExcelFile$ = Excel saveas filename or nil to close without saving
  254. ; Syntax examples:
  255. ; (CloseExcel "C:\\Temp\\Temp.xls") = Saveas C:\Temp\Temp.xls and close
  256. ; (CloseExcel nil) = Close without saving
  257. ;-------------------------------------------------------------------------------
  258. (defun CloseExcel (ExcelFile$ / Saveas)
  259.   (if ExcelFile$
  260.     (if (= (strcase ExcelFile$) (strcase *ExcelFile$))
  261.       (if (findfile ExcelFile$)
  262.         (vlax-invoke-method (vlax-get-property *ExcelApp% "ActiveWorkbook") "Save")
  263.         (setq Saveas t)
  264.       );if
  265.       (if (findfile ExcelFile$)
  266.         (progn
  267.           (vl-file-delete (findfile ExcelFile$))
  268.           (setq Saveas t)
  269.         );progn
  270.         (setq Saveas t)
  271.       );if
  272.     );if
  273.   );if
  274.   (if Saveas
  275.     (vlax-invoke-method (vlax-get-property *ExcelApp% "ActiveWorkbook")
  276.       "SaveAs" ExcelFile$ -4143 "" "" :vlax-false :vlax-false nil
  277.     );vlax-invoke-method
  278.   );if
  279.   (vlax-invoke-method (vlax-get-property *ExcelApp% "ActiveWorkbook") 'Close :vlax-False)
  280.   (vlax-invoke-method *ExcelApp% 'Quit)
  281.   (vlax-release-object *ExcelApp%)(gc)
  282.   (setq *ExcelApp% nil *ExcelFile$ nil)
  283.   (princ)
  284. );defun CloseExcel
  285. ;-------------------------------------------------------------------------------
  286. ; ColumnRow - Returns a list of the Column and Row number
  287. ; Function By: Gilles Chanteau from Marseille, France
  288. ; Arguments: 1
  289. ;   Cell$ = Cell ID
  290. ; Syntax example: (ColumnRow "ABC987") = '(731 987)
  291. ;-------------------------------------------------------------------------------
  292. (defun ColumnRow (Cell$ / Column$ Char$ Row#)
  293.   (setq Column$ "")
  294.   (while (< 64 (ascii (setq Char$ (strcase (substr Cell$ 1 1)))) 91)
  295.     (setq Column$ (strcat Column$ Char$)
  296.           Cell$ (substr Cell$ 2)
  297.     );setq
  298.   );while
  299.   (if (and (/= Column$ "") (numberp (setq Row# (read Cell$))))
  300.     (list (Alpha2Number Column$) Row#)
  301.     '(1 1);default to "A1" if there's a problem
  302.   );if
  303. );defun ColumnRow
  304. ;-------------------------------------------------------------------------------
  305. ; Alpha2Number - Converts Alpha string into Number
  306. ; Function By: Gilles Chanteau from Marseille, France
  307. ; Arguments: 1
  308. ;   Str$ = String to convert
  309. ; Syntax example: (Alpha2Number "ABC") = 731
  310. ;-------------------------------------------------------------------------------
  311. (defun Alpha2Number (Str$ / Num#)
  312.   (if (= 0 (setq Num# (strlen Str$)))
  313.     0
  314.     (+ (* (- (ascii (strcase (substr Str$ 1 1))) 64) (expt 26 (1- Num#)))
  315.        (Alpha2Number (substr Str$ 2))
  316.     );+
  317.   );if
  318. );defun Alpha2Number
  319. ;-------------------------------------------------------------------------------
  320. ; Number2Alpha - Converts Number into Alpha string
  321. ; Function By: Gilles Chanteau from Marseille, France
  322. ; Arguments: 1
  323. ;   Num# = Number to convert
  324. ; Syntax example: (Number2Alpha 731) = "ABC"
  325. ;-------------------------------------------------------------------------------
  326. (defun Number2Alpha (Num# / Val#)
  327.   (if (< Num# 27)
  328.     (chr (+ 64 Num#))
  329.     (if (= 0 (setq Val# (rem Num# 26)))
  330.       (strcat (Number2Alpha (1- (/ Num# 26))) "Z")
  331.       (strcat (Number2Alpha (/ Num# 26)) (chr (+ 64 Val#)))
  332.     );if
  333.   );if
  334. );defun Number2Alpha
  335. ;-------------------------------------------------------------------------------
  336. ; Cell-p - Evaluates if the argument Cell$ is a valid cell ID
  337. ; Function By: Gilles Chanteau from Marseille, France
  338. ; Arguments: 1
  339. ;   Cell$ = String of the cell ID to evaluate
  340. ; Syntax examples: (Cell-p "B12") = t, (Cell-p "BT") = nil
  341. ;-------------------------------------------------------------------------------
  342. (defun Cell-p (Cell$)
  343.   (and (= (type Cell$) 'STR)
  344.     (or (= (strcase Cell$) "A1")
  345.       (not (equal (ColumnRow Cell$) '(1 1)))
  346.     );or
  347.   );and
  348. );defun Cell-p
  349. ;-------------------------------------------------------------------------------
  350. ; Row+n - Returns the cell ID located a number of rows from cell
  351. ; Function By: Gilles Chanteau from Marseille, France
  352. ; Arguments: 2
  353. ;   Cell$ = Starting cell ID
  354. ;   Num# = Number of rows from cell
  355. ; Syntax examples: (Row+n "B12" 3) = "B15", (Row+n "B12" -3) = "B9"
  356. ;-------------------------------------------------------------------------------
  357. (defun Row+n (Cell$ Num#)
  358.   (setq Cell$ (ColumnRow Cell$))
  359.   (strcat (Number2Alpha (car Cell$)) (itoa (max 1 (+ (cadr Cell$) Num#))))
  360. );defun Row+n
  361. ;-------------------------------------------------------------------------------
  362. ; Column+n - Returns the cell ID located a number of columns from cell
  363. ; Function By: Gilles Chanteau from Marseille, France
  364. ; Arguments: 2
  365. ;   Cell$ = Starting cell ID
  366. ;   Num# = Number of columns from cell
  367. ; Syntax examples: (Column+n "B12" 3) = "E12", (Column+n "B12" -1) = "A12"
  368. ;-------------------------------------------------------------------------------
  369. (defun Column+n (Cell$ Num#)
  370.   (setq Cell$ (ColumnRow Cell$))
  371.   (strcat (Number2Alpha (max 1 (+ (car Cell$) Num#))) (itoa (cadr Cell$)))
  372. );defun Column+n
  373. ;-------------------------------------------------------------------------------
  374. ; rtosr - Used to change a real number into a short real number string
  375. ; stripping off all trailing 0's.
  376. ; Arguments: 1
  377. ;   RealNum~ = Real number to convert to a short string real number
  378. ; Returns: ShortReal$ the short string real number value of the real number.
  379. ;-------------------------------------------------------------------------------
  380. (defun rtosr (RealNum~ / DimZin# ShortReal$)
  381.   (setq DimZin# (getvar "DIMZIN"))
  382.   (setvar "DIMZIN" 8)
  383.   (setq ShortReal$ (rtos RealNum~ 2 8))
  384.   (setvar "DIMZIN" DimZin#)
  385.   ShortReal$
  386. );defun rtosr
  387. ;-------------------------------------------------------------------------------
  388. (princ);End of GetExcel.lsp
"觉得好,就打赏"
还没有人打赏,支持一下
 楼主| 发表于 2015-7-21 12:27 | 显示全部楼层
我自己先来顶一下,请高手帮忙解决一下,造福明经大家。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-4-25 08:21 , Processed in 0.398766 second(s), 30 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表