Office中国论坛/Access中国论坛

标题: 导入指定的EXCL表 [打印本页]

作者: 123shusheng    时间: 2010-5-25 16:29
标题: 导入指定的EXCL表
我在论坛里找到一段代码,可以将指定的EXCL表导入TEMP表,但它的字段名是默认的,我希望使用导入的EXCL表的第一行作为字段名,请高手帮我改改,谢谢!
[attach]42284[/attach]
Private Sub ComChoseFile_Click()
    On Error GoTo Err_ComChoseFile_Click
    Dim strfilter, strdlgtitle, stropentitle, strfile As String
    strfilter = "Excel 文件 (*.XLS)"
    strdlgtitle = "选择后台数据库的文件"
    stropentitle = "选择文件"
    strfile = SelectFileName(strfilter, strdlgtitle, stropentitle)
    If strfile = "" Then Exit Sub
    TextName = Replace(strfile, Chr(9), vbCrLf)
   
    Dim ex As Excel.Application
    Dim exwbook As Excel.Workbook
    Dim exsheet As Excel.Worksheet
    Dim v
    Set ex = CreateObject("Excel.Application")
    Set exwbook = ex.Workbooks.Open(TextName)
    For Each exsheet In exwbook.Worksheets
        v = v & ";" & exsheet.Name
    Next
    v = Right(v, Len(v) - 1)
    Me.Sheet.RowSource = v
    exwbook.Save
    exwbook.Close
    ex.Quit
    Set exsheet = Nothing
    Set exwbook = Nothing
    Set ex = Nothing
    Sheet.SetFocus
Exit_ComChoseFile_Click:
    Exit Sub
   
Err_ComChoseFile_Click:
    Resume Exit_ComChoseFile_Click
   
End Sub
作者: qmyjhj    时间: 2010-5-25 16:45
看了你的实例文件,你贴出来的代码只是选择文件真正的复制的代码是
DoCmd.TransferSpreadsheet acImport, 8, "TEMP", Me.TextName, False, Me.Sheet & "!"
这个在宏里面可以指定
作者: qmyjhj    时间: 2010-5-25 16:55
忘了说了。把那个false改为true就OK了




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