See also Read CSV
|Function Syntax||(LM:writecsv <lst> <csv>)|
|View HTML Version||WriteCSV-V1-1.html|
|lst||List||A list of lists where each sublist is a row of cell values (strings)|
|csv||String||Filename of CSV file to write|
|Symbol||T if successful, else nil|
This function will write a supplied matrix list of the cell values (that is, a list of lists where each sublist is a row of cell values) to an Excel Comma Separated Value (CSV) file and return T if successful.
The function requires two parameters: the full filename of the CSV file to be created, and a list of rows of cell values (strings) to be written to the new file. If the filename can be successfully opened for writing, each sublist of the supplied list is formatted for use within a CSV file and is then written to the file.
If a CSV file with the supplied filename already exists, this function will overwrite the existing file.
If the file cannot be created (most likely a result of read/write permissions set on the directory in question), this function will return nil.
Empty cells to be written to the CSV file should be supplied as empty strings ("") in the sublist of cell values.
The following example program will first prompt the user to make a selection of AutoCAD Point objects, then present the user with a dialog interface prompting for the name and location of an output CSV file to be created.
Following valid responses to both prompts, the program will proceed to write the coordinates of all selected points to the CSV file utilising the above LM:writecsv function, then open the created file.
(defun c:test ( / fn in lst ss ) (if (and (setq ss (ssget '((0 . "POINT")))) (setq fn (getfiled "Create Output File" "" "csv" 1)) ) (progn (repeat (setq in (sslength ss)) (setq lst (cons (mapcar 'rtos (cdr (assoc 10 (entget (ssname ss (setq in (1- in))))))) lst)) ) (if (LM:WriteCSV (reverse lst) fn) (startapp "explorer" fn) ) ) ) (princ) ) (princ)
Example of Output
Below is an example of the list format of the lst parameter as required by the above LM:writecsv function.
( ("CellA1" "CellB1" "CellC1" "CellD1") ("CellA2" "CellB2" "CellC2" "CellD2") ("CellA3" "CellB3" "CellC3" "CellD3") )
Upon calling the LM:writecsv function with the above list, the resultant CSV file will have the following content:
See also Read CSV