Message 1 of 16
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello every one
I need to make an autolisp code to import specific data from excel sheet ".xlsx" to an autocad table
I need the cell "B2" in the sheet to be in the autocad table it's name is " newtblobj" in cell (9, 4) in the autocad table
I've made something but it the excelData gives me nil
that's not what i need specific but i can edit the code to make what i need but i need to fix this error
(defun c:updateTableFromXLSX ()
; Read the data from the specified Excel file and sheet
(setq excelData (read-xlsx "G:\\55.xlsx" "11")) ; Update file path and sheet name
; Update the table in AutoCAD if the condition is met
(update-table-if-condition-met newTblObj excelData)
(princ))
(defun read-xlsx (filePath sheetName)
(setq excelApp (vlax-get-or-create-object "Excel.Application")) ; Start Excel
; Ensure the file exists
(if (not (findfile filePath))
(progn
(princ (strcat "\nFile not found or path is incorrect: " filePath))
(exit))
)
; Open the workbook
(setq workbooks (vlax-get excelApp 'Workbooks))
(setq workbook
(vl-catch-all-apply
'vlax-invoke-method
(list workbooks 'Open filePath))) ; Attempt to open the file
; Handle workbook opening errors
(if (vl-catch-all-error-p workbook)
(progn
(princ (strcat "\nFailed to open the Excel file. Check the file path or file integrity: " filePath))
(vlax-release-object workbooks)
(vlax-invoke-method excelApp 'Quit)
(vlax-release-object excelApp)
(exit))
)
; Get the sheets in the workbook
(setq sheets (vlax-get workbook 'Sheets))
; Retrieve the specific sheet by name
(setq sheet (vl-catch-all-apply 'vlax-get-property (list sheets 'Item sheetName)))
; Handle sheet retrieval errors
(if (vl-catch-all-error-p sheet)
(progn
(princ (strcat "\nSheet named \"" sheetName "\" not found."))
(vlax-invoke-method workbook 'Close :vlax-false)
(vlax-release-object workbook)
(vlax-release-object workbooks)
(vlax-invoke-method excelApp 'Quit)
(vlax-release-object excelApp)
(exit))
)
; Get the cell object at B2 (row 2, column 2)
(setq cell (vlax-get-property (vlax-get-property sheet 'Cells) 'Item 2 2))
; Get the value of the cell, handling both direct values and variants
(setq cellValue
(vl-catch-all-apply 'vlax-get-property (list cell 'Value)))
; Handle any errors during value retrieval
(if (vl-catch-all-error-p cellValue)
(setq cellValue nil)) ; Set to nil if there is an error
; Close the workbook and Excel
(vlax-invoke-method workbook 'Close :vlax-false)
(vlax-release-object workbook)
(vlax-release-object workbooks)
(vlax-invoke-method excelApp 'Quit)
(vlax-release-object excelApp)
; Print the retrieved value for debugging
(princ (strcat "\nRetrieved value from Excel cell B2: " (if cellValue (vl-princ-to-string cellValue) "nil")))
; Return the cell value
cellValue
)
(defun update-table-if-condition-met (tableObj excelData)
; Check if the Excel data was successfully retrieved
(if (not excelData)
(princ "\nExcel data is missing or could not be read.")
; Ensure that the Excel data is numeric and valid for the table update
(if (numberp excelData)
(progn
; Update the table at the specified location (adjust indices as necessary)
; Example: Setting cell (9, 4) in the AutoCAD table to the value from Excel (B2)
(vla-settext tableObj 9 4 (rtos excelData 2 2)) ; Convert the number to string for display
(princ "\nTable updated successfully."))
(princ "\nExcel data is not a number or is invalid.")))
(princ))
and this text to make the newTblObj
(setq newTblObj
(vla-AddTable
(vla-get-ModelSpace
(vla-get-ActiveDocument
(vlax-get-acad-object)))
(vlax-3D-point insPt) ; Convert list to variant array
20 ; Row count, +1 for headers
4 ; Column count
100 ; Row height
50)) ; Column width
thanks in advance
Solved! Go to Solution.