一旦你知道如何以一个体面的方式打开te excel后期绑定,并再次很好地关闭它,你就可以通过VBA或VB,C++和其他语言来做任何事情。
导入和导出数据可以通过这种方式完成,而无需支付软件费用,但这会消耗时间。
基础知识,在飞行中:
- Option Explicit
- Const WorkBookName = "TestThis.Xls"
- Const ImportPath = "C:\Test"
- Const DataSheet = "ImportDataSheet"
- Sub ExclHandler()
- Dim Excl As Excel.Application
- Dim Wb As Excel.Workbook
- Dim Ws As Excel.Worksheet
- Dim ExcelWasOpen As Boolean
- Dim WorkBookFound As Boolean
- 'Get an instance of excel if it is open that one to save resources.
- On Error GoTo NoExcelOpen
- ExcelWasOpen = True
- Set Excl = GetObject(, "Excel.Application")
- On Error GoTo 0
- 'Search for the Workbook if Excel was already open (it might be)
- If ExcelWasOpen Then
- For Each Wb In Excl.Workbooks
- If Wb.Name = WorkBookName Then
- WorkBookFound = True
- Exit For
- End If
- Next
- If Not WorkBookFound Then
- Set Wb = Excl.Workbooks.Open(ImportPath & WorkBookName)
- End If
- Else
- Set Wb = Excl.Workbooks.Open(ImportPath & WorkBookName)
- End If
- Set Ws = Wb.Sheets(DataSheet)
- 'Do whatever you want read or write to it.
- 'make it look nice
- 'to prove my point write something
- Ws.Cells(1, 1).Value = "Driesign.nl"
- 'and read it into a msgbox
- MsgBox Ws.Cells(1, 1).Value, vbCritical, "Stupid Test"
- '............
- '............
- '............
- '............
- '............
- 'finish by cleaning up
- Set Ws = Nothing
- If workbookwasopen Then
- Wb.Save
- Else
- Wb.Close True, "Optional Alternative Filename.xls"
- End If
- Set Wb = Nothing
- If Not ExcelWasOpen Then
- Excl.Quit
- End If
- Set Excl = Nothing
- Exit Sub
- NoExcelOpen:
- ExcelWasOpen = False
- Set Excl = CreateObject("Excel.Application")
- Resume Next
- End Sub
玩得开心.....
|