Aller au contenu

Autocad et Excel


Patrick_35

Messages recommandés

A titre d'information et sous toutes réserves, voilà une routine de Bill Kramer qui fait cela, je n'ai pas testée... Plus d'infos et le téléchargement ici.

 

;; Point A

;; A scroll from the Wizard's Lab.

;;

;; Bill Kramer 2002

;;

;; Interface with Excel Worksheet

;; Example that writes a list of data to a spreadsheet.

;;

;; Global Variables

;; xL - Object reference to EXCEL instance

;; myXLWbs - Object reference to EXCEL work books

;; myXLWb1 - Object reference to Work book #1

;; myXLShs - Object reference to Sheets in Work book

;; myXLSh1 - Object reference to Sheet 1

;;

;;---------------------------------------------------------------------

;;

(defun List_2_Excel (aList /

Row ;;integer row number

Col ;;integer column number

RowItem ;;each primary element in list

ColItem ;;each secondary element in list

myRange ;;object reference for cell in sheet

)

;;

;;Does an interface to Excel already exist?

(if (null xL)

(setq xL ;;if not, then try to make one

(vlax-get-or-create-object "Excel.Application")))

(if xL ;;success?

(progn

;;

;; We have the application object, now we need to tunnel

;; down into a work book.

;;

(if (null xl-open) ;;Check to see if type library defined

;;It is not, import the type library for EXCEL

(vlax-import-type-library

:tlb-filename "C:/Program Files/Microsoft Office/Office/Excel8.olb"

:methods-prefix "xL-"

:properties-prefix "xLp-"

:constants-prefix "xLc-"))

;;

;; Now dig into the EXCEL object

(setq myXLWbs (vlax-get xL "Workbooks") ;workbooks

myXLWb1 (xl-add myXLWbs) ;add a new workbook

myXLShs (vlax-get myXLWb1 "Sheets") ;worksheets of new workbook

myXLSh1 (xlp-get-item myXLShs 1) ;worksheet number 1

Row 1

)

;;

;; Loop through the list

(foreach RowItem aList

(setq Col 1) ;;reset column counter

;;

;; List expected to be nested, if not, then we fake it.

(if (atom RowItem) (setq RowItem (list RowItem)))

;;

;; Loop through each nested list

(foreach ColItem RowItem

;;

;; Create range object given column and row location

(setq myRange (xlp-get-range

myXLSh1

(strcat

(ColLetter Col) ;;see below

(itoa Row)

))

Col (1+ Col)) ;;increment column counter

;;

;; Write the value to the spreadsheet

(xlp-Put-Value myRange ColItem)

)

(setq Row (1+ Row)) ;;increment row counter

) ;;end FOREACH

)) ;;end IF PROGN

)

;;---------------------------------------------------------------------

;; ColLetter - Given a number, returns a string that is the Excel

;; method of numbering columns.

;; 1="A", 2="B", ... 26="Z", 27="AA", 28 = "Ab" ...

;;

(defun ColLetter (N /

Res ;;resulting string

TMP ;;work variable

)

(setq Res "")

(while (> N 0)

(setq TMP (rem N 26) ;;remainder of N divided by 26

TMP (if (zerop TMP) ;;reset to "Z"

(setq N (1- N) ;;move under next order

TMP 26 ;;set to Z offset value

)

TMP) ;;use value as it is

Res (strcat ;;Add character

(chr (+ 64 TMP)) ;;Offset plus 64 ("A" = 65)

Res) ;;existing string

N (/ N 26) ;;shift down an order

)

)

Res

)

;;==================================================

Lien vers le commentaire
Partager sur d’autres sites

Créer un compte ou se connecter pour commenter

Vous devez être membre afin de pouvoir déposer un commentaire

Créer un compte

Créez un compte sur notre communauté. C’est facile !

Créer un nouveau compte

Se connecter

Vous avez déjà un compte ? Connectez-vous ici.

Connectez-vous maintenant
×
×
  • Créer...

Information importante

Nous avons placé des cookies sur votre appareil pour aider à améliorer ce site. Vous pouvez choisir d’ajuster vos paramètres de cookie, sinon nous supposerons que vous êtes d’accord pour continuer. Politique de confidentialité