;;;Elise Moss ;;;www.mossdesigns.com ;;;March 2005 ;;; ;;;import an Excel spreadsheet into an AutoCAD table ;;;save the Excel spreadsheet as a txt file ;;;each line of the txt file should be one cell value (defun c:xtable () ;;; set the list to nil to start (setq xcel-list nil) (setq row-list nil) ;;;; get csv/txt file to import ;;;; each table cell text should be on it's own line (setq filename (getfiled "Get file to import: " "*.*" "*" 8)) ;open the file to read (setq fr (open filename "r")) ; read file contents into a list (while (setq rl (read-line fr)) (setq xcel-list (append xcel-list (list rl))) ) ; close file (close fr) ; time to create the table (setq list-counter 0) (setq colno (getint "\nEnter the number of columns: ")) ; get cell no count (setq cellno (length xcel-list)) (setq cellno (- cellno 1)) ; subtract the heading ;;calculate row count (setq rowno (/ cellno colno)) (setq rowno (1+ rowno));add back the row for the main heading ;;get insertion point for the table (setq ins-pt (getpoint "\nSelect insertion point for the table: ")) (vl-load-com) ;; object Table (setq vla_table (vla-addtable ;; for test place the object on model space (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object)) ) ;; base = 0,0,0 (vlax-3d-point ins-pt) ;; rows number - including title & headers rowno ;; columns number colno ;; row height 0.125 ;; column width 3.0 ) ) ;;;set the main heading title (setq main-heading (nth list-counter xcel-list)) ;; set title name (vla-settext vla_table 0 0 main-heading) ;; cell alignment (vla-setcellalignment vla_table 0 0 acmiddlecenter) ;; cell text height (vla-setcelltextheight vla_table 0 0 0.125) ; we are now on row 2 col 0 (setq row-no 1) (setq col-no 0) (setq col-counter 1) (setq upper-limit (+ col-counter 5)) (setq row-limit (1+ rowno)) (while (< row-no row-limit) (while (< col-counter upper-limit) (setq row-list (append row-list (list (nth col-counter xcel-list))) ) (setq col-counter (1+ col-counter)) ) ;end while (setq itemno 0) ;;place the list values into the row (while (< itemno 5) (setq item (nth itemno row-list)) ; ;; cell text (vla-settext vla_table row-no col-no item) ;; alignment (vla-setcellalignment vla_table row-no col-no acmiddlecenter ) ;; text height (vla-setcelltextheight vla_table row-no col-no 0.125) ;; next column (setq col-no (1+ col-no)) (setq itemno (1+ itemno)) ;;to get the next cell value ) ; end while ;; next row (setq row-no (1+ row-no)) ;; first column (setq col-no 0) (setq upper-limit (+ col-counter 5)) ; re-initialize the row list values (setq row-list nil) ) ;end while (princ) (alert "Table has been created.") ) ;end defun (princ "\nType 'xtable' to create a table from a txt/csv file: ")