"c:\更新时间.xls" 这个文件的更新时间列含有日期、时间,直接导入到狐表时,可以显示日期、时间。
但是用以下代码导入时,即丢失了“时间”(全为0:0:0)
Dim Book As New XLS.Book("c:\更新时间.xls")
Dim Sheet As XLS.Sheet = Book.Sheets(0)
Tables("测试").StopRedraw()
'注意以下数组中列名称的顺序,必须和Excel表中的列顺序一致
Dim nms() As String = {"序号", "更新时间"}
'注意下面的循环变量从1开始,而不是从0开始,因为Excel表的第一行是标题
For n As Integer = 1 To Sheet.Rows.Count - 1
Dim r As Row = Tables("测试").AddNew()
For m As Integer = 0 To nms.Length - 1
r(nms(m)) = Sheet(n, m).Value
Next
Next
Tables("测试").ResumeRedraw()
[此贴子已经被作者于2024/7/16 23:44:31编辑过]
改为使用vba导:http://www.foxtable.com/bbs/dispbbs.asp?BoardID=2&ID=65686&skin=0
以下代码导入正常
Dim dlg As New OpenFileDialogdlg.Filter= "所有文件|*.*|Excel2003文件|*.xls|Excel2007文件|*.xlsx" '设置筛选器If dlg.ShowDialog = DialogResult.Ok Then '如果用户单击了确定按钮 MessageBox.Show("你选择的是:" & dlg.FileName,"提示") If MessageBox.Show("是否合并?","请确 认!",MessageBoxButtons.OKCancel,MessageBoxIcon.Question) = DialogResult.OK Then DataTables("表A").StopRedraw Dim App As New MSExcel.Application try Dim Wb As MSExcel.WorkBook = App.WorkBooks.Open(dlg.FileName) Dim Ws As MSExcel.WorkSheet = Wb.WorkSheets(1) Dim Rg As MSExcel.Range = Ws.UsedRange For n As Integer = 2 To rg.Rows.Count Dim ro As Row = Tables("表A").AddNew For i As Integer = 0 To Tables("表A").Cols.Count - 1 ro(i) = ws.Cells(n,i+2).Text ‘这里要按实际列位置修改 Next Next catch ex As exception msgbox(ex.message) finally DataTables("表A").ResumeRedraw app.quit End try Else Return End IfEnd IfMessageBox.Show("导入成功!","恭喜!")
[此贴子已经被作者于2024/7/17 21:58:21编辑过]
不过,上面还存在一个小BUG,当XLS文件的时间列较窄,以“#######”形式显示时,上面的VBA导入代码即出错。
请参考2楼论坛提供的用法,使用数组进行转换取值,不要偷懒。
Dim ary = rg.value
For n As Integer = 2 To rg.Rows.Count
Dim ro As Row = Tables("表A").AddNew
For i As Integer = 0 To Tables("表A").Cols.Count - 1
ro(i) = ary(n,i+2)
Next
Next