【发布时间】:2015-10-01 02:29:07
【问题描述】:
我有一个 MS Access 数据库程序,它从 MS Excel 工作簿(它只是一张表)中引入数据,然后对其进行处理并为用户踢出一个完整的表格。
整个事情运行良好,但是当他们在报告中遇到错误时,我去使用数据库程序查找问题并出现了一个新问题。 MS Access 仅导入单元格 A1 和 A2。
当我尝试手动导入 Excel 文件并附加到表格时,导入向导仅将这两个单元格显示为包含在文件中。不知何故,MS Access 没有看到整个数据表。
我曾尝试手动更改 DoCmd.TransferSpreadsheet 中的 acSpreadsheetType 选项,并且它工作过一次。我也尝试过以编程方式更改它,但没有任何效果(即,我仍然只得到单元格 A1 和 A2)。
这是处理这部分程序的 VBA 段:
Dim acSpreadsheetType As Integer
strFile2Import = txtFindFile
acSpreadsheetType = ExcelVersion(strFile2Import)
DoCmd.Hourglass (HourglassOn)
DoCmd.SetWarnings (WarningsOff)
DoCmd.RunSQL _
"DELETE tbl_Work_Plan_Item_Import.*, * " & _
"FROM tbl_Work_Plan_Item_Import;"
DoCmd.TransferSpreadsheet _
acImport, acSpreadsheetType, "tbl_Work_Plan_Item_Import", strFile2Import, True
这里是“ExcelVersion”函数:
Public Function ExcelVersion(ByVal strFile2Import As String)
'https://msdn.microsoft.com/en-us/library/office/ff840717.aspx
'https://msdn.microsoft.com/en-us/library/office/ff198017.aspx
Set objapp = CreateObject("Excel.Application")
objapp.Visible = True
Set wb = objapp.workbooks.Open(strFile2Import, True, False)
ExcelVersion = wb.FileFormat
wb.Close
objapp.Quit
Set objapp = Nothing
Select Case ExcelVersion
Case 29 'xlExcel3 (Excel3)
ExcelVersion = 0 'acSpreadsheetTypeExcel3 (Microsoft Excel 3.0 format)
Case 33 'xlExcel4 (Excel4)
ExcelVersion = 6 'acSpreadsheetTypeExcel4 (Microsoft Excel 4.0 format)
Case 39 'xlExcel5 (Excel5)
'xlExcel7 (Excel7)
ExcelVersion = 5 'acSpreadsheetTypeExcel5 (Microsoft Excel 5.0 format)
'acSpreadsheetTypeExcel7 (Microsoft Excel 95 format)
Case 46 'xlXMLSpreadsheet (XML Spreadsheet)
ExcelVersion = 10 'acSpreadsheetTypeExcel12Xml (Microsoft Excel 2010 XML format)
Case 50, 51 'xlExcel12 (Excel12)
'xlWorkbookDefault (Workbook Default)
ExcelVersion = 9 'acSpreadsheetTypeExcel12 (Microsoft Excel 2010 format)
Case 56 'xlExcel8 (Excel8)
ExcelVersion = 8 'acSpreadsheetTypeExcel8 (Microsoft Excel 97 format)
'acSpreadsheetTypeExcel9 (Microsoft Excel 2000 format)
End Select
End Function
“strFile2Import”和“txtFindFile”是从文件对话框中获取的要导入的文件名和完整路径。
我的原始程序没有 ExcelVersion 函数,我将 acSpreadsheetType 硬编码为“acSpreadsheetTypeExcel12XML”,当我将其更改为“acSpreadsheetTypeExcel9”时,它可以正常工作一次。
我错过了什么?
作为记录,我还尝试在执行导入后对整个表单执行 .recalc 和 .requery,但也没有成功。
如果你们能帮我解决问题,我们将不胜感激!
【问题讨论】:
-
我觉得你写的有意思的是“手动导入Excel文件”也只导入了A1和A2。暂时忘记让代码工作,并尝试手动导入工作簿以完成您的期望!我会尝试以各种不同的 excel 格式保存 excel 文件。我会尝试重新创建一个新的 excel 文件,你可以相信它不会以某种方式“损坏”。这将更清楚地说明出了什么问题。我会尝试将不起作用的 xl 字段导入安装在另一台 PC 上的访问权限中。好主意?