以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  批量合并execl数据  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=92362)

--  作者:jyh7081
--  发布时间:2016/11/1 20:58:00
--  批量合并execl数据
1、foxtable内有数张表,除表A、表B外,其余数据表的表结构均相同,但列数不同。数据表的第一列都是时间列,后面的每一列是一个股票数据。
2、同路径下有数个execl数据表,每个数据表的第一列都是时间列,后面的每一列也是一个股票数据。
3、想批量把数个execl数据表内的数据合并进foxtable的数个数据表。
问题是两种数据表的个数不是一一对应的,列数也不是一一对应的。
按照foxtable内数据表中含有的股票种类合并。
感觉就是要遍历所有execl工作表中的列和foxtable所有数据表中的列以后,再导入。但不会写代码,请指教!

图片点击可在新窗口打开查看此主题相关图片如下:image 1.jpg
图片点击可在新窗口打开查看

附件:
 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:数据表.rar




--  作者:有点蓝
--  发布时间:2016/11/1 22:02:00
--  
http://www.foxtable.com/webhelp/scr/0328.htm


--  作者:有点蓝
--  发布时间:2016/11/1 22:03:00
--  
Dim dlg As New OpenFileDialog
dlg.MultiSelect = True
If dlg.ShowDialog = DialogResult.OK Then
    Dim lst As new List(of String)
    lst.Add("_Identify")
    lst.Add("_Locked")
    lst.Add("_SortKey")
    lst.Add("时间")
    Dim dict As new Dictionary(of String,String)
    For Each dt As DataTable In DataTables
        If dt.Name.StartsWith("数据") Then
            For Each c As DataCol In dt.DataCols
                If lst.Contains(c.Name) = False AndAlso dict.ContainsKey(c.Name) = False Then dict.Add(c.Name,dt.Name)
            Next
        End If
    Next
    Dim dict2 As new Dictionary(of String,DataRow)
    SystemReady = False
    For Each fl As String In dlg.FileNames
        Dim Book As New XLS.Book(fl)
        Dim Sheet As XLS.Sheet = Book.Sheets(0)
        
        For m As Integer = 1 To Sheet.Cols.Count -1
            If dict.ContainsKey(Sheet(0,m).Value) Then
                For n As Integer = 1 To Sheet.Rows.Count -1
                    Dim dr As DataRow
                    Dim cl As String  = dict(Sheet(0,m).Value) & "|" & Sheet(n,0).Value
                    If dict2.ContainsKey(cl) Then
                        dr = dict2(cl)
                    Else
                        dr = DataTables(dict(Sheet(0,m).Value)).AddNew()
                        dr("时间") =  Sheet(n,0).Value
                        dict2.Add(cl,dr)
                    End If
                    dr(Sheet(0,m).Value) = Sheet(n,m).Value
                    dict2(cl) = dr
                    
                Next
            End If
        Next
    Next
    SystemReady = True
End If
[此贴子已经被作者于2016/11/1 22:03:02编辑过]

--  作者:jyh7081
--  发布时间:2016/11/1 22:35:00
--  
谢谢版主!
有点小问题,就是foxtable表中原先有的  “时间”  行数据没能覆盖,出现了重复的 “时间”行。能不能不是追加,而是覆盖写入?

图片点击可在新窗口打开查看此主题相关图片如下:image 3.jpg
图片点击可在新窗口打开查看


[此贴子已经被作者于2016/11/1 22:40:25编辑过]

--  作者:有点蓝
--  发布时间:2016/11/1 22:58:00
--  
参考2楼的第二个帮助
--  作者:jyh7081
--  发布时间:2016/11/2 13:08:00
--  
版主,代码难度太大了,看了一早晨还是无从下手。麻烦有空再给看看。谢谢!
--  作者:有点蓝
--  发布时间:2016/11/2 14:49:00
--  
初学者先系统看看开发指南,和视频教程
--  作者:有点青
--  发布时间:2016/11/2 18:00:00
--  

Dim dlg As New OpenFileDialog
dlg.MultiSelect = True
If dlg.ShowDialog = DialogResult.OK Then
    Dim lst As new List(of String)
    lst.Add("_Identify")
    lst.Add("_Locked")
    lst.Add("_SortKey")
    lst.Add("时间")
    Dim dict As new Dictionary(of String,String)
    Dim dict2 As new Dictionary(of String,DataRow)
    For Each dt As DataTable In DataTables
        If dt.Name.StartsWith("数据") Then
            For Each c As DataCol In dt.DataCols
                If lst.Contains(c.Name) = False AndAlso dict.ContainsKey(c.Name) = False Then
                    dict.Add(c.Name,dt.Name)
                End If
            Next
            For Each r As DataRow In dt.Select("")
                If dict2.ContainsKey(dt.name & "|" & r("时间")) = False Then
                    dict2.add(dt.name & "|" & r("时间"), r)
                End If
            Next
        End If
    Next
    SystemReady = False
    For Each fl As String In dlg.FileNames
        Dim Book As New XLS.Book(fl)
        Dim Sheet As XLS.Sheet = Book.Sheets(0)
       
        For m As Integer = 1 To Sheet.Cols.Count -1
            If dict.ContainsKey(Sheet(0,m).Value) Then
                For n As Integer = 1 To Sheet.Rows.Count -1
                    Dim dr As DataRow
                    Dim cl As String  = dict(Sheet(0,m).Value) & "|" & Sheet(n,0).Value
                    If dict2.ContainsKey(cl) Then
                        dr = dict2(cl)
                    Else
                        dr = DataTables(dict(Sheet(0,m).Value)).AddNew()
                        dr("时间") =  Sheet(n,0).Value
                        dict2.Add(cl,dr)
                    End If
                    dr(Sheet(0,m).Value) = Sheet(n,m).Value
                    dict2(cl) = dr
                Next
            End If
        Next
    Next
    SystemReady = True
End If

 


--  作者:jyh7081
--  发布时间:2016/11/2 19:09:00
--  
谢谢版主和 有点青 老师!