以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  CXCEL导入  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=82396)

--  作者:yaojun
--  发布时间:2016/3/17 9:14:00
--  CXCEL导入

我有一金华销售表,其中内容是采用每次导入CXCEL表“销售信息”表中的数据的,但是该表中有很多列,跪求如何导入该表相对应的列,而忽略其它不需要导入的列。

 

Dim Book As New XLS.Book("F:\\销售信息.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()


--  作者:大红袍
--  发布时间:2016/3/17 10:03:00
--  

mark 通用excel导入

 

参考例子

 

 

 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:导入excel文件.zip


--  作者:yaojun
--  发布时间:2016/3/17 11:32:00
--  

以上内容如下代码:

Dim dt As DataTable = DataTables("金华销售")
Dim Result As DialogResult
Result = MessageBox.Show("确定要导入吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If Result = DialogResult.Yes Then
    Dim dlg As New OpenFileDialog
    dlg.Filter = "Excel文件|*.xls"
    dlg.MultiSelect = True \'允许选择多个文件
    If dlg.ShowDialog =DialogResult.OK Then
        Dim Book As New XLS.Book(dlg.FileName)
        Dim Sheet As XLS.Sheet = Book.Sheets("3.1全")
        dt.ResumeRedraw()
        dt.StopRedraw()
        \'systemready = False
        Dim nms As New Dictionary(Of String, Integer)
        Dim dic As new Dictionary(Of DataRow, Integer)
        Dim ls As new List(Of Integer)
        For c As Integer = 0 To sheet.Cols.Count - 1
            If  dt.DataCols.Contains(sheet(0,c).Text.replace(" ", "")) Then
                nms.Add(sheet(0,c).Text.replace(" ", ""), c)
            End If
        Next
        For n As Integer = 1 To Sheet.Rows.Count -1
            Dim sfzhm As String = sheet(n,nms("店铺旺旺")).Text
            If sfzhm = "" Then Continue For
            Dim dr As DataRow = dt.Find("店铺旺旺 = \'" & sfzhm & "\'")
            If dr Is Nothing Then
                ls.add(n)
            Else
                dic.Add(dr, n)
            End If
        Next
        For Each key As DataRow In dic.Keys
            For Each m As String In nms.keys
                If dt.DataCols(m).IsBoolean Then
                    If Sheet(dic(key),nms(m)).Text = "" OrElse Sheet(dic(key),nms(m)).Text = "False" OrElse Sheet(dic(key),nms(m)).Value = 0 Then
                        key(m) = False
                    Else
                        key(m) = True
                    End If
                Else If dt.DataCols(m).Expression > "" Then
                    \'表达式列
                Else If dt.DataCols(m).IsNumeric Then
                    key(m) = val(Sheet(dic(key),nms(m)).Value)
                Else If dt.DataCols(m).IsDate Then
                    Dim d As Date
                    If Date.TryParse(Sheet(dic(key),nms(m)).Value, d)
                        key(m) = d
                    End If
                   
                Else
                    key(m) = Sheet(dic(key),nms(m)).Value
                End If
            Next
        Next
        For Each l As Integer In ls
            Dim ndr As DataRow = dt.AddNew
            For Each m As String In nms.keys
                If dt.DataCols(m).IsBoolean Then
                    If Sheet(l,nms(m)).Text = "" OrElse Sheet(l,nms(m)).Text = "False" OrElse Sheet(l,nms(m)).Value = 0 Then
                        ndr (m) = False
                    Else
                        ndr (m) = True
                    End If
                Else If dt.DataCols(m).Expression > "" Then
                    \'表达式列
                Else If dt.DataCols(m).IsNumeric Then
                    ndr (m) = val(Sheet(l,nms(m)).Value)
                Else If dt.DataCols(m).IsDate Then
                    Dim d As Date
                    If Date.TryParse(Sheet(l,nms(m)).Value, d)
                        ndr (m) = d
                    End If
                Else
                   
                    ndr (m) = Sheet(l,nms(m)).Value
                End If
            Next
        Next
        dt.ResumeRedraw()
        \'systemready = True
    End If
End If

可以解决一次导入问题,但是二次导入的时候是在原来的基础上更新,我需要每次的导入都能记录上去,而不是只在原来的上面作出更新,请老师指教


--  作者:大红袍
--  发布时间:2016/3/17 11:38:00
--  

Dim dt As DataTable = DataTables("金华销售")
Dim Result As DialogResult
Result = MessageBox.Show("确定要导入吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If Result = DialogResult.Yes Then
    Dim dlg As New OpenFileDialog
    dlg.Filter = "Excel文件|*.xls"
    dlg.MultiSelect = True \'允许选择多个文件
    If dlg.ShowDialog =DialogResult.OK Then
        Dim Book As New XLS.Book(dlg.FileName)
        Dim Sheet As XLS.Sheet = Book.Sheets("3.1全")
        dt.ResumeRedraw()
        dt.StopRedraw()
        \'systemready = False
        Dim nms As New Dictionary(Of String, Integer)
        Dim dic As new Dictionary(Of DataRow, Integer)
        Dim ls As new List(Of Integer)
        For c As Integer = 0 To sheet.Cols.Count - 1
            If  dt.DataCols.Contains(sheet(0,c).Text.replace(" ", "")) Then
                nms.Add(sheet(0,c).Text.replace(" ", ""), c)
            End If
        Next
        For l As Integer = 1 To Sheet.Rows.Count -1
            Dim ndr As DataRow = dt.AddNew
            For Each m As String In nms.keys
                If dt.DataCols(m).IsBoolean Then
                    If Sheet(l,nms(m)).Text = "" OrElse Sheet(l,nms(m)).Text = "False" OrElse Sheet(l,nms(m)).Value = 0 Then
                        ndr (m) = False
                    Else
                        ndr (m) = True
                    End If
                Else If dt.DataCols(m).Expression > "" Then
                    \'表达式列
                Else If dt.DataCols(m).IsNumeric Then
                    ndr (m) = val(Sheet(l,nms(m)).Value)
                Else If dt.DataCols(m).IsDate Then
                    Dim d As Date
                    If Date.TryParse(Sheet(l,nms(m)).Value, d)
                        ndr (m) = d
                    End If
                Else
                   
                    ndr (m) = Sheet(l,nms(m)).Value
                End If
            Next
        Next
        dt.ResumeRedraw()
        \'systemready = True
    End If
End If

 


--  作者:yaojun
--  发布时间:2016/3/17 11:51:00
--  

谢谢红袍老师,以上代码可以解决问题,不过每次导入在最后面多增加了一行,能消除吗


--  作者:大红袍
--  发布时间:2016/3/17 12:00:00
--  
Dim dt As DataTable = DataTables("金华销售")
Dim Result As DialogResult
Result = MessageBox.Show("确定要导入吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If Result = DialogResult.Yes Then
    Dim dlg As New OpenFileDialog
    dlg.Filter = "Excel文件|*.xls"
    dlg.MultiSelect = True \'允许选择多个文件
    If dlg.ShowDialog =DialogResult.OK Then
        Dim Book As New XLS.Book(dlg.FileName)
        Dim Sheet As XLS.Sheet = Book.Sheets("3.1全")
        dt.ResumeRedraw()
        dt.StopRedraw()
        \'systemready = False
        Dim nms As New Dictionary(Of String, Integer)
        Dim dic As new Dictionary(Of DataRow, Integer)
        Dim ls As new List(Of Integer)
        For c As Integer = 0 To sheet.Cols.Count - 1
            If  dt.DataCols.Contains(sheet(0,c).Text.replace(" ", "")) Then
                nms.Add(sheet(0,c).Text.replace(" ", ""), c)
            End If
        Next
        For l As Integer = 1 To Sheet.Rows.Count -1
            If sheet(l, nms("店铺旺旺")).Text.trim > "" Then
                Dim ndr As DataRow = dt.AddNew
                For Each m As String In nms.keys
                    If dt.DataCols(m).IsBoolean Then
                        If Sheet(l,nms(m)).Text = "" OrElse Sheet(l,nms(m)).Text = "False" OrElse Sheet(l,nms(m)).Value = 0 Then
                            ndr (m) = False
                        Else
                            ndr (m) = True
                        End If
                    Else If dt.DataCols(m).Expression > "" Then
                        \'表达式列
                    Else If dt.DataCols(m).IsNumeric Then
                        ndr (m) = val(Sheet(l,nms(m)).Value)
                    Else If dt.DataCols(m).IsDate Then
                        Dim d As Date
                        If Date.TryParse(Sheet(l,nms(m)).Value, d)
                            ndr (m) = d
                        End If
                    Else
                       
                        ndr (m) = Sheet(l,nms(m)).Value
                    End If
                Next
            End If
        Next
        dt.ResumeRedraw()
        \'systemready = True
    End If
End If
[此贴子已经被作者于2016/3/17 12:00:01编辑过]

--  作者:yaojun
--  发布时间:2016/3/17 12:31:00
--  

非常感谢