Jump to content

Writing LISP Data to an External Program (such as Excel/Notepad...etc)


Lee Mac

Recommended Posts

This thread is solely in response to an original thread posted by Gazzalp, which needed the extraction of various data to an excel spreadsheet.

 

But I am intrigued, as I too do not know the first thing about writing data to external programs such as Excel.

 

If, for instance, one has variables: var1, var2, var3; can one write the values of these variables to various cells in an Excel Sheet? or lines in a Txt doc? And if so, how? :huh:

 

Help & advice is always appreciated. o:)

Link to comment
Share on other sites

The simplest way is to write in text files. First define and open a file for writing:

(setq file (open "my_file.txt" "w"))

Now you can write into it, just convert the data to be written into string:

(write-line (rtos var1) file)

when finished, close the file

(close file)

Better see in AutoLISP help file the commands Open, Close, Write-char, Write-line, print, ...

Also Getfiled worth a look

 

Sometimes I write in the same way but in files with extension CSV instead of TXT. Those files can be opened in Excel. Just be carefull to write a comma in the file to separate two neighbor data.

Link to comment
Share on other sites

I have created a test txt file in a known directory, and have used your example to write the line to the text file.

 

but, after closing the file using (close "file"), and manually opening the file myself, nothing seems to be written... :?

Link to comment
Share on other sites

Lee Mac,

 

You may also want to think about exporting in an AutoLisp list format

 (setq x 45 y 50 z 55)

 (setq wf (open "mydata.dat" "w"))
 (write-line "(setq mydata '(" wf)
 (foreach v '(x y z)
    (princ "(" wf)
    (prin1 v wf)
    (princ (strcat " . " (rtos (eval v) 2  ")") wf)
    (write-line "" wf))
 (write-line "))" wf)
 (close wf)

Then you access it later by using the (load) function

 

Command: (load "mydata.dat")

Command: !mydata
((X . 45.0) (Y . 50.0) (Z . 55.0))

 (assoc 'x mydata)
 (cdr (assoc 'x mydata))

This is very similar to a XML file but in AutoLISPs list format. -David

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...