Jump to content

Lisp lệnh EXCEL trong chủ đề “AutoCAD với Excel”

Tôi có một file Excel và một lisp để mở file Excel rồi ghi số liệu mới vào.

Tôi muốn lisp thực hiện theo hai cách sau:

1- Mở file Excel, xóa toàn bộ nội dung cũ trong các Sheet để ghi số liệu mới.

2- Mở file Excel, xóa toàn bộ các Sheet cũ, tạo các Sheet mới và ghi số liệu mới.

Tôi quá kém về các hàm Vla... nên không thực hiện được, nhờ mọi người giúp đỡ nhé,

xin cảm ơn.

fIle Excel:  https://www.cadviet.com/upfiles/3/88193_du_tkk_1.rar

file lsp:

; Test lisp 09/09/2014
;-----------------------------------------------------------------------------------------
(defun C:excel()
  (vl-load-com)
  (setq lst_Sheet '("Kenh_chinh" "Kenh_CI" "Kenh_CII" "Kenh_CIII" "Kenh_ND"))
  (if (not fn)
    (progn
	  (setq fn (getfiled "Select Excel File" "" "xls" 0))
	  (if fn
        (progn
		  (WriteToExcel fn lst_Sheet T)
		)
		(progn
		  (WriteToExcel nil lst_Sheet T)
        )
	  )	
	)
  )
)
;------------------------------------------------------------------------------
(defun WriteToExcel (ExcelFile$ lst_Sheet Visible / Sheet$ Sheets@ Worksheet n)
  (if (= (type ExcelFile$) 'STR)
    (if (findfile ExcelFile$)
      (setq *ExcelFile$ ExcelFile$)
      (progn
        (alert (strcat "Excel file " ExcelFile$ " not found."))
        (exit)
      )
    )
    (setq *ExcelFile$ "")
  )
  (gc)
  (if (setq *ExcelApp% (vlax-get-object "Excel.Application"))
    (progn
      (alert "Close all Excel spreadsheets to continue!")
      (vlax-release-object *ExcelApp%)(gc)
    )
  )
  (setq *ExcelApp% (vlax-get-or-create-object "Excel.Application"))
  (if ExcelFile$
    (if (findfile ExcelFile$)
      (vlax-invoke-method (vlax-get-property *ExcelApp% 'WorkBooks) 'Open ExcelFile$)
      (vlax-invoke-method (vlax-get-property *ExcelApp% 'WorkBooks) 'Add)
    )
    (vlax-invoke-method (vlax-get-property *ExcelApp% 'WorkBooks) 'Add)
  )
  (if Visible
    (vla-put-visible *ExcelApp% :vlax-true)
  )
  (setq n 0)
  (repeat (length lst_Sheet)
    (setq SheetName$ (nth n lst_Sheet))
    (if (= (type SheetName$) 'STR)
      (progn
        (vlax-for Sheet$ (vlax-get-property *ExcelApp% "Sheets")
          (setq Sheets@ (append Sheets@ (list (vlax-get-property Sheet$ "Name"))))
        )
;		(if (member SheetName$ Sheets@)
;          (vlax-for Worksheet (vlax-get-property *ExcelApp% "Sheets")
;            (if (= (vlax-get-property Worksheet "Name") "Kenh_ND")
;              (vlax-invoke-method Worksheet "Delete")
;            )
;          )
;        )
		(if (member SheetName$ Sheets@)
          (vlax-for Worksheet (vlax-get-property *ExcelApp% "Sheets")
            (if (= (vlax-get-property Worksheet "Name") "Kenh_ND")
              (vlax-invoke-method Worksheet "ClearContents")
            )
          )
        )
        (if (member SheetName$ Sheets@)
          (vlax-for Worksheet (vlax-get-property *ExcelApp% "Sheets")
            (if (= (vlax-get-property Worksheet "Name") SheetName$)
              (vlax-invoke-method Worksheet "Activate")	  
            )
          )
          (vlax-put-property (vlax-invoke-method (vlax-get-property *ExcelApp% "Sheets") "Add") "Name" SheetName$)
		)
      )
    )
    (setq n (1+ n))
  )
  (princ)
)

Tệp .lsp gốc không còn trên máy chủ. Hãy chép mã nguồn bên dưới (lấy từ bài viết gốc) vào một tệp văn bản, lưu với đuôi .lsp.

Cách dùng

  1. Chép mã nguồn, lưu thành tệp 310559_excel.lsp.
  2. Trong AutoCAD gõ lệnh APPLOAD, chọn tệp vừa tải rồi bấm Load. Cũng có thể kéo thả tệp .lsp vào cửa sổ bản vẽ.
  3. Gõ lệnh EXCEL để chạy lisp.

Mã nguồn 310559_excel.lsp (trích từ bài viết gốc)

; Test lisp 09/09/2014
;-----------------------------------------------------------------------------------------
(defun C:excel()
  (vl-load-com)
  (setq lst_Sheet '("Kenh_chinh" "Kenh_CI" "Kenh_CII" "Kenh_CIII" "Kenh_ND"))
  (if (not fn)
    (progn
	  (setq fn (getfiled "Select Excel File" "" "xls" 0))
	  (if fn
        (progn
		  (WriteToExcel fn lst_Sheet T)
		)
		(progn
		  (WriteToExcel nil lst_Sheet T)
        )
	  )	
	)
  )
)
;------------------------------------------------------------------------------
(defun WriteToExcel (ExcelFile$ lst_Sheet Visible / Sheet$ Sheets@ Worksheet n)
  (if (= (type ExcelFile$) 'STR)
    (if (findfile ExcelFile$)
      (setq *ExcelFile$ ExcelFile$)
      (progn
        (alert (strcat "Excel file " ExcelFile$ " not found."))
        (exit)
      )
    )
    (setq *ExcelFile$ "")
  )
  (gc)
  (if (setq *ExcelApp% (vlax-get-object "Excel.Application"))
    (progn
      (alert "Close all Excel spreadsheets to continue!")
      (vlax-release-object *ExcelApp%)(gc)
    )
  )
  (setq *ExcelApp% (vlax-get-or-create-object "Excel.Application"))
  (if ExcelFile$
    (if (findfile ExcelFile$)
      (vlax-invoke-method (vlax-get-property *ExcelApp% 'WorkBooks) 'Open ExcelFile$)
      (vlax-invoke-method (vlax-get-property *ExcelApp% 'WorkBooks) 'Add)
    )
    (vlax-invoke-method (vlax-get-property *ExcelApp% 'WorkBooks) 'Add)
  )
  (if Visible
    (vla-put-visible *ExcelApp% :vlax-true)
  )
  (setq n 0)
  (repeat (length lst_Sheet)
    (setq SheetName$ (nth n lst_Sheet))
    (if (= (type SheetName$) 'STR)
      (progn
        (vlax-for Sheet$ (vlax-get-property *ExcelApp% "Sheets")
          (setq Sheets@ (append Sheets@ (list (vlax-get-property Sheet$ "Name"))))
        )
;		(if (member SheetName$ Sheets@)
;          (vlax-for Worksheet (vlax-get-property *ExcelApp% "Sheets")
;            (if (= (vlax-get-property Worksheet "Name") "Kenh_ND")
;              (vlax-invoke-method Worksheet "Delete")
;            )
;          )
;        )
		(if (member SheetName$ Sheets@)
          (vlax-for Worksheet (vlax-get-property *ExcelApp% "Sheets")
            (if (= (vlax-get-property Worksheet "Name") "Kenh_ND")
              (vlax-invoke-method Worksheet "ClearContents")
            )
          )
        )
        (if (member SheetName$ Sheets@)
          (vlax-for Worksheet (vlax-get-property *ExcelApp% "Sheets")
            (if (= (vlax-get-property Worksheet "Name") SheetName$)
              (vlax-invoke-method Worksheet "Activate")	  
            )
          )
          (vlax-put-property (vlax-invoke-method (vlax-get-property *ExcelApp% "Sheets") "Add") "Name" SheetName$)
		)
      )
    )
    (setq n (1+ n))
  )
  (princ)
)

Thảo luận gốc

Lisp được chia sẻ trong chủ đề AutoCAD với Excel. Xem đúng bài viết #310559 để đọc hướng dẫn và trao đổi thêm.

Về Thư viện AutoLisp (trang 171)