Author Topic: Autolisp to export autocad table to excel?  (Read 13521 times)

0 Members and 1 Guest are viewing this topic.

fathihvac

  • Guest
Autolisp to export autocad table to excel?
« on: April 11, 2013, 06:02:05 AM »
Hello everybody,
I need an autolisp that asks me to select an autocad table from a drawing and then browse for existing excel file to put the data in a new created worksheet to that excel file.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Autolisp to export autocad table to excel?
« Reply #1 on: April 11, 2013, 01:49:28 PM »
You can export an AutoCAD Table to an Excel CSV file using the TABLEEXPORT command.

fathihvac

  • Guest
Re: Autolisp to export autocad table to excel?
« Reply #2 on: April 13, 2013, 05:16:56 AM »
Thank you Mr.LEE .
I need to be quick . With TABLEEXPORT  need to save the data to a csv  file ,browse for it  and open it, save its content and then browse for my excel file and paste the data to a new worksheet.
So could help me to achieve my request i am sure it will be helpful to many.

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: Autolisp to export autocad table to excel?
« Reply #3 on: April 13, 2013, 02:29:33 PM »
Code: [Select]
(defun vk_PutXls
;;;Âåðñ³ÿ 0.10
;;;³ä 20.08.2008
(FileName SheetName
  InList /
  ExcelObj BooksObj
  BookObj SheetObj
  SheetsObj CellsObj
  RowCounter ColumnCounter
  IntFormat RealFormat
  StrFormat GeneralFormat
  Result FoundFileName
  *error*
)
    (setq IntFormat "0"
  RealFormat (strcat "0"
(vl-registry-read "HKEY_CURRENT_USER\\Control Panel\\International\\"
  "sDecimal"
)
"0000"
)
  StrFormat "@"
  GeneralFormat "General"
  FoundFileName (if FileName
    (findfile FileName)
)
    )
    (if (and (setq ExcelObj (vlax-get-or-create-object "Excel.Application"))
     (not (vl-catch-all-error-p (vl-catch-all-apply 'vlax-put-property
    (list ExcelObj
  "Visible"
  (if FileName
      :vlax-false
      :vlax-true
  )
    )
)
  )
     )
     (not
(vl-catch-all-error-p
     (setq BooksObj (vl-catch-all-apply 'vlax-get-property (list ExcelObj "WorkBooks")))
)
     )
     (not
(if FoundFileName
     (vl-catch-all-error-p
(setq BookObj (vl-catch-all-apply 'vlax-invoke-method (list BooksObj "Open" FoundFileName)))
     )
     (if (vl-catch-all-error-p
     (setq BookObj (vl-catch-all-apply 'vlax-invoke-method (list BooksObj "Add")))
)
t
(if FileName
     (vl-catch-all-error-p
(vl-catch-all-apply 'vlax-invoke-method
     (list BookObj "SaveAs" FileName nil nil nil nil nil nil)
)
     )
     nil
)
     )
)
     )
;;; (not (vl-catch-all-error-p
;;; (setq BookObj (vl-catch-all-apply
;;; 'vlax-invoke-method
;;; (if FoundFileName
;;;   (list BooksObj "Open" FoundFileName)
;;;   (list BooksObj "Add")
;;; )
;;;       )
;;; )
;;;       )
;;; )
     (not
(vl-catch-all-error-p
     (setq SheetsObj (vl-catch-all-apply 'vlax-get-property (list BookObj "Sheets")))
)
     )
     (or (not (vl-catch-all-error-p
  (setq SheetObj (vl-catch-all-apply 'vlax-get-property
     (if SheetName
(list SheetsObj "Item" SheetName)
(list BookObj "ActiveSheet")
     )
)
  )
      )
)
(not
     (or (vl-catch-all-error-p
     (setq SheetObj (vl-catch-all-apply 'vlax-invoke-method (list SheetsObj "Add")))
)
(vl-catch-all-error-p
     (vl-catch-all-apply 'vlax-put-property (list SheetObj "Name" SheetName))
)
     )
)
     )
     (not
(vl-catch-all-error-p
     (setq CellsObj (vl-catch-all-apply 'vlax-get-property (list SheetObj "Cells")))
)
     )
)
(progn (setq RowCounter 1)
       (foreach Row InList
   (setq ColumnCounter 1)
   (foreach Cell Row
       ((lambda (CellObj)
    (vlax-put-property CellObj
       "NumberFormat"
       ((lambda (CellType)
    (cond ((= CellType 'INT) IntFormat)
  ((= CellType 'REAL) RealFormat)
  ((= CellType 'STR) StrFormat)
  (t GeneralFormat)
    )
)   (type Cell)
       )
    )
    (vlax-put-property CellObj "Value2" Cell)
    (vlax-release-object CellObj)
)  (vlax-variant-value (vlax-get-property CellsObj "Item" RowCounter ColumnCounter)
   )
       )
       (setq ColumnCounter (1+ ColumnCounter))
   )
   (setq RowCounter (1+ RowCounter))
       )
       (setq Result t)
)
    )
    (setq Result (and (if FileName
  (eval
      (list 'and
    (and BookObj
(not (vl-catch-all-error-p BookObj))
(not (vl-catch-all-error-p
  (vl-catch-all-apply 'vlax-invoke-method (list BookObj "Close" 1))
      )
)
    )
    (and ExcelObj
(not (vl-catch-all-error-p
  (vl-catch-all-apply 'vlax-invoke-method (list ExcelObj "Quit"))
      )
)
    )
      )
  )
  t
      )
      Result
)
    )
    (foreach Obj (list CellsObj SheetObj SheetsObj BookObj BooksObj ExcelObj)
(if (and Obj (not (vl-catch-all-error-p Obj)))
    (vlax-release-object Obj)
)
    )
    (gc)
    Result
)
;;;(vk_PutXls "C:\\Temp\\Book111.xls" "SheetName" (list (list "a" "s" "d") (list 1 2 3.12)))
(defun vk_DivideListByN (lst n / tmp nlst)
  (while lst
    (repeat n
      (setq tmp (cons (car lst) tmp)
    lst (cdr lst)
      )
    )
    (setq nlst (cons (reverse tmp) nlst)
  tmp  nil
    )
  )
  (reverse nlst)
)
(defun vk_ExportTable2Excel (EntName / EntProps TableData)
  (setq EntProps (entget EntName))
  (setq TableData (vk_DivideListByN
    (mapcar 'cdr
    (vl-remove-if-not
      (function (lambda (e) (= (car e) 1)))
      EntProps
    )
    )
    (cdr (assoc 92 EntProps))
  )
  )
  (vk_PutXls nil nil TableData)
)
;;;(vk_ExportTable2Excel (car (entsel "\nSelect table: ")))

i don't have Excel installed on home computer, so i can't test it
i hope it works

fathihvac

  • Guest
Re: Autolisp to export autocad table to excel?
« Reply #4 on: April 13, 2013, 04:22:56 PM »
Thank you very much? what command should be applied (i mean where is c:_commandname)

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: Autolisp to export autocad table to excel?
« Reply #5 on: April 13, 2013, 05:28:20 PM »
Code: [Select]
(vk_ExportTable2Excel (car (entsel "\nSelect table: ")))

fathihvac

  • Guest
Re: Autolisp to export autocad table to excel?
« Reply #6 on: April 14, 2013, 04:37:44 AM »
Its ok but i need to browse for an existing excel file and put the data of the selected table in a new named worksheet of that excel file.

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: Autolisp to export autocad table to excel?
« Reply #7 on: April 14, 2013, 06:36:36 AM »
vk_PutXls accepts both file name and sheet name as arguments

fathihvac

  • Guest
Re: Autolisp to export autocad table to excel?
« Reply #8 on: April 15, 2013, 11:55:37 AM »
Thank you very much.
Thank you mr. LEE .i am not good in autolisp programming could you update  VovKa 'code to do the following:
*type command (example :exporttabletoexcel)
*asks to to select table.
*open browse for excel file dialog box .
the result: the data of the table copied into a new excel worksheet already named (example : equipmentschedule)


fathihvac

  • Guest
Re: Autolisp to export autocad table to excel?
« Reply #9 on: April 21, 2013, 10:30:08 AM »
REMINDER!!!
Still waiting for help on the above post.

fixo

  • Guest
Re: Autolisp to export autocad table to excel?
« Reply #10 on: April 21, 2013, 02:39:51 PM »
Change this code to your needs (for a plain acad table only)
didn't work with merged cells combined to many rows / columns

mhy3sx

  • Newt
  • Posts: 120
Re: Autolisp to export autocad table to excel?
« Reply #11 on: June 13, 2023, 06:49:46 AM »
Hi Vovka. I know that this post is very old but the code is not running.

can you fix it


Code - Auto/Visual Lisp: [Select]
  1. (defun vk_PutXls
  2. ;;;Aa?n³y 0.10
  3. ;;;A³a 20.08.2008
  4.                 (FileName       SheetName
  5.                   InList        /
  6.                   ExcelObj      BooksObj
  7.                   BookObj       SheetObj
  8.                   SheetsObj     CellsObj
  9.                   RowCounter    ColumnCounter
  10.                   IntFormat     RealFormat
  11.                   StrFormat     GeneralFormat
  12.                   Result        FoundFileName
  13.                   *error*
  14.                 )
  15.     (setq IntFormat     "0"
  16.           RealFormat    (strcat "0"
  17.                                 (vl-registry-read "HKEY_CURRENT_USER\\Control Panel\\International\\"
  18.                                                   "sDecimal"
  19.                                 )
  20.                                 "0000"
  21.                         )
  22.           StrFormat     "@"
  23.           GeneralFormat "General"
  24.           FoundFileName (if FileName
  25.                             (findfile FileName)
  26.                         )
  27.     )
  28.     (if (and (setq ExcelObj (vlax-get-or-create-object "Excel.Application"))
  29.              (not (vl-catch-all-error-p (vl-catch-all-apply 'vlax-put-property
  30.                                                             (list ExcelObj
  31.                                                                   "Visible"
  32.                                                                   (if FileName
  33.                                                                       :vlax-false
  34.                                                                       :vlax-true
  35.                                                                   )
  36.                                                             )
  37.                                         )
  38.                   )
  39.              )
  40.              (not
  41.                 (vl-catch-all-error-p
  42.                      (setq BooksObj (vl-catch-all-apply 'vlax-get-property (list ExcelObj "WorkBooks")))
  43.                 )
  44.              )
  45.              (not
  46.                 (if FoundFileName
  47.                      (vl-catch-all-error-p
  48.                         (setq BookObj (vl-catch-all-apply 'vlax-invoke-method (list BooksObj "Open" FoundFileName)))
  49.                      )
  50.                      (if (vl-catch-all-error-p
  51.                              (setq BookObj (vl-catch-all-apply 'vlax-invoke-method (list BooksObj "Add")))
  52.                         )
  53.                         t
  54.                         (if FileName
  55.                              (vl-catch-all-error-p
  56.                                 (vl-catch-all-apply 'vlax-invoke-method
  57.                                                      (list BookObj "SaveAs" FileName nil nil nil nil nil nil)
  58.                                 )
  59.                              )
  60.                              nil
  61.                         )
  62.                      )
  63.                 )
  64.              )
  65. ;;;     (not (vl-catch-all-error-p
  66. ;;;             (setq BookObj (vl-catch-all-apply
  67. ;;;                             'vlax-invoke-method
  68. ;;;                             (if FoundFileName
  69. ;;;                               (list BooksObj "Open" FoundFileName)
  70. ;;;                               (list BooksObj "Add")
  71. ;;;                             )
  72. ;;;                           )
  73. ;;;             )
  74. ;;;           )
  75. ;;;     )
  76.              (not
  77.                 (vl-catch-all-error-p
  78.                      (setq SheetsObj (vl-catch-all-apply 'vlax-get-property (list BookObj "Sheets")))
  79.                 )
  80.              )
  81.              (or (not (vl-catch-all-error-p
  82.                           (setq SheetObj (vl-catch-all-apply 'vlax-get-property
  83.                                                              (if SheetName
  84.                                                                 (list SheetsObj "Item" SheetName)
  85.                                                                 (list BookObj "ActiveSheet")
  86.                                                              )
  87.                                         )
  88.                           )
  89.                       )
  90.                 )
  91.                 (not
  92.                      (or (vl-catch-all-error-p
  93.                              (setq SheetObj (vl-catch-all-apply 'vlax-invoke-method (list SheetsObj "Add")))
  94.                         )
  95.                         (vl-catch-all-error-p
  96.                              (vl-catch-all-apply 'vlax-put-property (list SheetObj "Name" SheetName))
  97.                         )
  98.                      )
  99.                 )
  100.              )
  101.              (not
  102.                 (vl-catch-all-error-p
  103.                      (setq CellsObj (vl-catch-all-apply 'vlax-get-property (list SheetObj "Cells")))
  104.                 )
  105.              )
  106.         )
  107.         (progn (setq RowCounter 1)
  108.                (foreach Row InList
  109.                    (setq ColumnCounter 1)
  110.                    (foreach Cell Row
  111.                        ((lambda (CellObj)
  112.                             (vlax-put-property CellObj
  113.                                                "NumberFormat"
  114.                                                ((lambda (CellType)
  115.                                                     (cond ((= CellType 'INT) IntFormat)
  116.                                                           ((= CellType 'REAL) RealFormat)
  117.                                                           ((= CellType 'STR) StrFormat)
  118.                                                           (t GeneralFormat)
  119.                                                     )
  120.                                                 )                                         (type Cell)
  121.                                                )
  122.                             )
  123.                             (vlax-put-property CellObj "Value2" Cell)
  124.                             (vlax-release-object CellObj)
  125.                         )  (vlax-variant-value (vlax-get-property CellsObj "Item" RowCounter ColumnCounter)
  126.                            )
  127.                        )
  128.                        (setq ColumnCounter (1+ ColumnCounter))
  129.                    )
  130.                    (setq RowCounter (1+ RowCounter))
  131.                )
  132.                (setq Result t)
  133.         )
  134.     )
  135.     (setq Result (and (if FileName
  136.                           (eval
  137.                               (list 'and
  138.                                     (and BookObj
  139.                                         (not (vl-catch-all-error-p BookObj))
  140.                                         (not (vl-catch-all-error-p
  141.                                                   (vl-catch-all-apply 'vlax-invoke-method (list BookObj "Close" 1))
  142.                                               )
  143.                                         )
  144.                                     )
  145.                                     (and ExcelObj
  146.                                         (not (vl-catch-all-error-p
  147.                                                   (vl-catch-all-apply 'vlax-invoke-method (list ExcelObj "Quit"))
  148.                                               )
  149.                                         )
  150.                                     )
  151.                               )
  152.                           )
  153.                           t
  154.                       )
  155.                       Result
  156.                 )
  157.     )
  158.     (foreach Obj (list CellsObj SheetObj SheetsObj BookObj BooksObj ExcelObj)
  159.         (if (and Obj (not (vl-catch-all-error-p Obj)))
  160.             (vlax-release-object Obj)
  161.         )
  162.     )
  163.     (gc)
  164.     Result
  165. )
  166. ;;;(vk_PutXls "C:\\Temp\\Book111.xls" "SheetName" (list (list "a" "s" "d") (list 1 2 3.12)))
  167. (defun vk_DivideListByN (lst n / tmp nlst)
  168.   (while lst
  169.     (repeat n
  170.       (setq tmp (cons (car lst) tmp)
  171.             lst (cdr lst)
  172.       )
  173.     )
  174.     (setq nlst (cons (reverse tmp) nlst)
  175.           tmp  nil
  176.     )
  177.   )
  178.   (reverse nlst)
  179. )
  180. (defun c:vk_ExportTable2Excel (EntName / EntProps TableData)
  181.   (setq EntProps (entget EntName))
  182.   (setq TableData (vk_DivideListByN
  183.                     (mapcar 'cdr
  184.                             (vl-remove-if-not
  185.                               (function (lambda (e) (= (car e) 1)))
  186.                               EntProps
  187.                             )
  188.                     )
  189.                     (cdr (assoc 92 EntProps))
  190.                   )
  191.   )
  192.   (vk_PutXls nil nil TableData)
  193. )
  194. ;;;(vk_ExportTable2Excel (car (entsel "\nSelect table: ")))
  195.  
  196.  
  197.  


Thanks

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: Autolisp to export autocad table to excel?
« Reply #12 on: June 13, 2023, 09:15:23 AM »
I know that this post is very old but the code is not running.
looks like it's expired :)

i'm afraid i need more details