Lisp to export table from AutoCAD to excel

Lisp to export table from AutoCAD to excel

S_S_SS
Advocate Advocate
24,623 Views
18 Replies
Message 1 of 19

Lisp to export table from AutoCAD to excel

S_S_SS
Advocate
Advocate

Hello every one 
i need an autolisp code to export table from AutoCAD to excel 
i have a lisp that make more commands and i need the code to add it to that lisp 
I need it to make some properties :-
1- i need excel in xlsx extension 
2- i need the created excel saved in the same file location of the drawing 
3- i need the created excel file have the same name of the drawing
4- i need  the created excel file to be opened  after the exporting
And thanks in advance 

0 Likes
Accepted solutions (3)
24,624 Views
18 Replies
Replies (18)
Message 2 of 19

hak_vz
Advisor
Advisor

I don't know for any solution that would directly write Autocad table contents to .xslx format.

You can either use export to .csv file (command tableexport) or transfer data to Excel sheet with open Excel application using vla-object methods.

This solution requires that you explode autocad table (to lines and text entities) and after you run

command TE it will open Excell and write selected data to new sheet, and at the end you have to save file.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 3 of 19

S_S_SS
Advocate
Advocate

Hello sir
we can make it to .csv 
can i have a lisp code to add it into my lisp ???

0 Likes
Message 4 of 19

hak_vz
Advisor
Advisor
Accepted solution

Here is your code

 

(defun TE (e delim / _openfile eo name fname f row col str)
	;exports Autocad table entity to csv file and opens it in Excel
	(defun _openfile (file / sh)
	  ;thanks to @ronjohnp	
	  (setq sh (vlax-get-or-create-object "Shell.Application"))
	  (vlax-invoke-method sh 'open (findfile file))
	  (vlax-release-object sh)
	)
	(setq eo (vlax-ename->vla-object e))
	(cond 
		((= (vlax-get eo 'ObjectName) "AcDbTable")
			(setq
				name (getvar 'dwgname) 
				name (substr name 1 (- (strlen name)4))
				fname(strcat (getvar 'dwgprefix) name ".csv")
				f (open fname "w")
				row -1
			)
			(while (< (setq row (1+ row)) (vla-get-Rows eo))
				(setq col -1 str "")
				(while (< (setq col (1+ col)) (vla-get-Columns eo))
				   (setq str (strcat str delim (vla-GetText eo row col)))
				)
				(write-line (substr str 2) f)
			)
			(close f)
			(_openfile fname)
		)  
	)
	(princ)
)

 

Usage:

 

;...
;... your code here
;...
;... table entity name is stored in variable e
;(te table_entity_name csv_delimiter)
;let say
(te e ",")  
; if decimal symbol on your system is a "." and csv delimiter is "," 
(te e ";")  
; if decimal symbol on your system is a "," and csv delimiter is ";" 

 

File is saved to same location as dwg file, with name  dwgname.csv and opened in Excel. User have to save Excel file with extension you want i.e. xls or xlsx.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 5 of 19

Sea-Haven
Mentor
Mentor

I already privately provided you "table to excel lisp" its about wanting more and not having a go yourself.

 

 

Message 6 of 19

hak_vz
Advisor
Advisor

@S_S_SS  Have you got any reaction regarding provided code? What else do you need?

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 7 of 19

S_S_SS
Advocate
Advocate

thanks sir about that 
I used the code and there is an error about that 
it don't work and AutoCAD can't recognize that 
when I write (te) AutoCAD don't write this command and don't work 
you wrote two codes I copied them together to make the lisp 

can you explain what would I do ?? 
and thanks in advance 
 

0 Likes
Message 8 of 19

S_S_SS
Advocate
Advocate

Hello my teacher 
I loved what you made and thanks for your effort 
just this lisp that you sent don't work correctly 
it appear a message 

mostafahisham62_1-1644147774084.jpeg

and when i press ok 
the excel sheet export first and second rows only 

mostafahisham62_2-1644147841226.png

 

I'm a civil engineer and I work hard to make a system of working to make my work easier 
thanks for your effort with me just I don't have the time to study auto lisp all of my time is in my department 
i hope you help me about what i need 




 

0 Likes
Message 9 of 19

J-Rocks
Collaborator
Collaborator
Select Table then right click with your mouse to export to excel and there no need to any codes.
Message 10 of 19

hak_vz
Advisor
Advisor
Accepted solution

@S_S_SS wrote:

thanks sir about that 
I used the code and there is an error about that 
it don't work and AutoCAD can't recognize that 
when I write (te) AutoCAD don't write this command and don't work 
you wrote two codes I copied them together to make the lisp 

can you explain what would I do ?? 
and thanks in advance 
 


You say that you have some code where you want this code to be included.

Let say that you have table entity selected to variable named table

To create Excel table you then run function as

 

 

 

(te table ";")
or
(te table ",")
depending on your regional use of "." or "," as decimal sign, i.e. how it is defined in Windows since Excel will use that setting. In autolisp decimal sign is always "."

 

 

 

CSV delimiter symbol is needed since is different in various regions. I would use ";" since in Croatia decimal symbol is ",".

 

Lets create a sample usage function

 

 

 

(defun c:MyTableExport ()
(setq table (car(entsel "\nSelect table")))
(te table ",")
)

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 11 of 19

hak_vz
Advisor
Advisor

@J-Rocks wrote:
Select Table then right click with your mouse to export to excel and there no need to any codes.

Yes, command TABLEEXPORT will create CSV file from selected table.

 

It is not easy to add it to lisp function since there is no function mode to work without showing dialog (-TABLEEXPORT). To answer @S_S_SS  request to create csv named as dwg file in same location an open it automatically in Excel some code is needed.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 12 of 19

Sea-Haven
Mentor
Mentor

Look at using (Command "Shell" "excel filename.csv")

 

Will look at using tableexport in future.

Message 13 of 19

Sea-Haven
Mentor
Mentor
Accepted solution

Found a typo in the code this version works added a couple of things.

 

 

Message 14 of 19

S_S_SS
Advocate
Advocate

thanks about that sir 

0 Likes
Message 15 of 19

gaexcalibur
Enthusiast
Enthusiast

I am using this table to excel but it is skipping the 3rd row absolutely every time that this export happens.  Is there a reason for that?

0 Likes
Message 16 of 19

dkwadkwa
Community Visitor
Community Visitor

i use this lisp,it's so good but why always no third row , i do something wrongs?

0 Likes
Message 17 of 19

gaexcalibur
Enthusiast
Enthusiast

got it working no problem now so things are good.

0 Likes
Message 18 of 19

pedro_vcalves
Explorer
Explorer

@Sea-Haven hi! 
Coming from that other post.

It was your code that I was using. Apparently it has some issue when using it when a different UCS or PLAN is set. I solved that by returning the UCS and Plan to default

0 Likes
Message 19 of 19

Sea-Haven
Mentor
Mentor

Sounds a bit odd as when you read the table just reads the contents of cells so should not matter what UCS is set, will have a play see if can replicate.

0 Likes