Office中国论坛/Access中国论坛

标题: 用什么函数可以链接到EXCEL [打印本页]

作者: SUQUANJIN    时间: 2007-12-12 15:57
标题: 用什么函数可以链接到EXCEL
请教一下:  有时ACCESS和EXCEL及WORD都是配合着用的,,有一些数据和表格是用EXCEL做的,在ACCESS中要怎么样才能用命令打开EXCEL的工作表呢?
作者: andymark    时间: 2007-12-12 16:05
下面是一个ACCESS操作EXCEL的例子
  1. Sub FileImportFromExcel(ByVal sFile As String)

  2. Dim appExcel As Excel.Application
  3. Dim wbk As Excel.Workbook
  4. Dim wks As Excel.Worksheet
  5. Dim Rs As New ADODB.Recordset
  6. Dim Conn As New ADODB.Connection
  7. Dim I As Integer
  8. Dim J As Integer
  9. Dim StrWeekName As String

  10. Set appExcel = Excel.Application
  11. Set wbk = appExcel.Workbooks.Open(sFile)

  12. Set wks = appExcel.Worksheets("NB LIPC")

  13. Set Conn = CurrentProject.Connection

  14. On Error Resume Next

  15. Rs.Open "Select * from TemCommitFromExcel", Conn, adOpenDynamic, adLockOptimistic

  16. For I = 1 To 500

  17. If wks.Cells(I, 1).Value = "Technology" Then
  18. Rs.AddNew
  19. Rs.Fields("Year") = Me.CmbYear
  20. Rs.Fields("Site") = Me.TxtSite
  21. Rs.Fields("RequirementWeek") = Me.CmbRequirementWeek
  22. Rs.Fields("PartNumber") = Trim(wks.Cells(I, 5).Value)

  23. For J = 0 To 13
  24. StrWeekName = "WK" & J
  25. Rs.Fields(StrWeekName) = wks.Cells(I + 5, 2 + J).Value
  26. Next J

  27. Rs.Update
  28. End If

  29. Next I

  30. Set wks = Nothing
  31. wbk.Close True
  32. Set wbk = Nothing
  33. appExcel.Quit
  34. Set appExcel = Nothing
  35. Set Rs = Nothing
  36. Set Conn = Nothing

  37. Exit Sub


  38. End Sub
复制代码





欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3