以文本方式查看主题 - Foxtable(狐表) (http://foxtable.net/bbs/index.asp) -- 专家坐堂 (http://foxtable.net/bbs/list.asp?boardid=2) ---- 导入EXCEL (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=154018) |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
-- 作者:nbsugu_z -- 发布时间:2020/9/1 11:14:00 -- 导入EXCEL 以下是表hyjd 从外部excel表导入不重复的数据,为什么只增加了空行,没有任何数据? excel表结构是这样的
表hyjd结构是一样的,只是后面增加了几列,以后要设置计算公式用,以“券号” qh 作为不重复的标志 Dim dlg As New OpenFileDialog dlg.Filter = "Excel文件|*.xls;*.xlsx" If dlg.ShowDialog =DialogResult.OK Then Dim t As Table = Tables("hyjd") t.StopRedraw() Dim Book As New XLS.Book(dlg.FileName) Dim Sheet As XLS.Sheet = Book.Sheets(0) Dim newcount As Integer = 0 Dim oldcount As Integer = 0 For n As Integer = 1 To Sheet.Rows.Count -1 Dim r As DataRow = t.DataTable.Find("qh = \'" & sheet(n, 0).text & "\'") If r Is Nothing Then r = t.DataTable.AddNew() newcount += 1 Else oldcount += 1 End If For i As Integer = 0 To sheet.Cols.Count -1 Dim cname As String = sheet(0, i).text If t.Cols.Contains(cname) Then If t.Cols(cname).IsBoolean Then If sheet(n, i).Text = 1 OrElse sheet(n, i).Text = "true" Then r(cname) = True Else r(cname) = False End If Else r(cname) = sheet(n, i).Text End If End If Next Next msgbox("新增" & newcount & " " & "更新旧数据" & oldcount) t.ResumeRedraw() End If
[此贴子已经被作者于2020/9/1 11:20:50编辑过]
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
-- 作者:有点蓝 -- 发布时间:2020/9/1 11:45:00 -- foxtable要通过列名查询数据,而不是标题名称 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
-- 作者:nbsugu_z -- 发布时间:2020/9/1 13:45:00 -- Dim r As DataRow = t.DataTable.Find("qh = \'" & sheet(n, 0).text & "\'") 这个是列名麻 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
-- 作者:有点蓝 -- 发布时间:2020/9/1 13:54:00 -- 卷号是第5列,不是0列 Dim r As DataRow = t.DataTable.Find("qh = \'" & sheet(n, 5).text & "\'") 另外execl里面是标题名,您通过列名来判断是否不会标题名,是木有意义的 Dim cname As String = sheet(0, i).text If t.Cols.Contains(cname) Then 要么先把列名和标题名的对应放到字典里,发布查询;要么遍历所有列,看哪一个列的标题等于cname ,在获取对应的列名,比如 For i As Integer = 0 To sheet.Cols.Count -1 Dim cname As String = sheet(0, i).text If t.Cols(i).caption = cname Then If t.Cols(i).IsBoolean Then …… r(t.Cols(i).name) = sheet(n, i).Text |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
-- 作者:nbsugu_z -- 发布时间:2020/9/1 14:03:00 -- 谢谢老师指点 |