forked from mcneel/MOVED-rhinoscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExcelDumper.rvb
117 lines (92 loc) · 3.2 KB
/
ExcelDumper.rvb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' ExcelDumper.rvb -- March 2009
' If this code works, it was written by Dale Fugier.
' If not, I don't know who wrote it.
' Works with Rhino 4.0.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Option Explicit
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Prints the contents of an Excel file to the command line.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub ExcelDumper()
' Local variable declarations
Dim strFile, arrSheet, i, j, varCell, strFormat
' Prompt for the Excel file to read
strFile = Rhino.OpenFileName("Open", "Excel Files (*.xls)|*.xls||")
If IsNull(strFile) Then Exit Sub
' Read the Excel file
arrSheet = ReadExcelFile(strFile)
If IsNull(arrSheet) Then Exit Sub
' Dump the worksheet to the command line
For i = 0 To UBound(arrSheet, 1)
For j = 0 To UBound(arrSheet, 2)
strFormat = "Sheet(" & CStr(i) & "," & CStr(j) & ") = "
varCell = arrSheet(i, j)
If IsEmpty(varCell) Then
Rhino.Print strFormat & "<empty>"
Else
Rhino.Print strFormat & CStr(varCell)
End If
Next
Next
End Sub
' Description:
' Reads a Microsoft Excel file.
' Parameters:
' strFile - [in] The name of the Excel file to read.
' Returns:
' A two-dimension array of cell values, if successful.
' Null on error
Function ReadExcelFile(ByVal strFile)
' Local variable declarations
Dim objExcel, objSheet, objCells
Dim nUsedRows, nUsedCols, nTop, nLeft, nRow, nCol
Dim arrSheet()
' Default return value
ReadExcelFile = Null
' Create the Excel object
On Error Resume Next
Set objExcel = CreateObject("Excel.Application")
If (Err.Number <> 0) Then
Exit Function
End If
' Don't display any alert messages
objExcel.DisplayAlerts = 0
' Open the document as read-only
On Error Resume Next
Call objExcel.Workbooks.Open(strFile, False, True)
If (Err.Number <> 0) Then
Exit Function
End If
' If you wanted to read all sheets, you could call
' objExcel.Worksheets.Count to get the number of sheets
' and the loop through each one. But in this example, we
' will just read the first sheet.
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)
' Get the number of used rows
nUsedRows = objSheet.UsedRange.Rows.Count
' Get the number of used columns
nUsedCols = objSheet.UsedRange.Columns.Count
' Get the topmost row that has data
nTop = objSheet.UsedRange.Row
' Get leftmost column that has data
nLeft = objSheet.UsedRange.Column
' Get the used cells
Set objCells = objSheet.Cells
' Dimension the sheet array
ReDim arrSheet(nUsedRows - 1, nUsedCols - 1)
' Loop through each row
For nRow = 0 To (nUsedRows - 1)
' Loop through each column
For nCol = 0 To (nUsedCols - 1)
' Add the cell value to the sheet array
arrSheet(nRow, nCol) = objCells(nRow + nTop, nCol + nLeft).Value
Next
Next
' Close the workbook without saving
Call objExcel.ActiveWorkbook.Close(False)
' Quit Excel
objExcel.Application.Quit
' Return the sheet data to the caller
ReadExcelFile = arrSheet
End Function