以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  [求助]代码出错(已解决)  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=25507)

--  作者:yyzlxc
--  发布时间:2012/11/10 14:36:00
--  [求助]代码出错(已解决)
以下这段代码可以合并带空格的excel表格,当接收表有数据时,可以正常运行。但是当接收表为空时,则会出现错误提示(见下图)。如何达到接收表为空时能够正常运行,请各位老师指教,谢谢!!

Dim Sheet As XLS.Sheet = Book.Sheets(i)
For r As Integer = 1 To Sheet.Rows.Count - 1
    Dim Filter1 As String = ""
    For c As Integer = 0 To Sheet.Cols.Count - 1
        If c > 0 Then
            Filter1 = Filter1 & " And "
        End If
        If sheet(r,c).Value <> "" AndAlso CurrentTable.DataTable.DataCols(sheet(0,c).Value).IsNumeric Then
            Filter1 = Filter1 & sheet(0,c).Value & " = " & sheet(r,c).Value
        ElseIf sheet(r,c).Value = "" Then
            Filter1 = Filter1 & sheet(0,c).Value & " is null "
        Else
            Filter1 = Filter1 & sheet(0,c).Value & " = \'" & sheet(r,c).Value & "\'"
        End If
    Next
    If CurrentTable.DataTable.Find(Filter1) Is Nothing Then
        Dim dr As Row = CurrentTable.AddNew()
        For c As Integer = 0 To Sheet.Cols.Count - 1
            dr(Sheet(0,c).Value) = Sheet(r,c).Value
        Next
    End If
Next


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

[此贴子已经被作者于2012-11-10 16:59:00编辑过]

--  作者:lin_hailun
--  发布时间:2012/11/10 14:51:00
--  
 上例子,上数据。
--  作者:sgs
--  发布时间:2012/11/10 15:12:00
--  

http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&Id=17564&page=3

 

这里有个例子,楼主可以参考下合并数据


--  作者:yyzlxc
--  发布时间:2012/11/10 16:03:00
--  
谢谢lin_hailun老师的回复。上传案例,删除3月份的数据,保存后,可以导入;如果将全部数据删除后,按导入按钮,就会出现出错提示,问题在哪里,请指教,谢谢!!

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



--  作者:lin_hailun
--  发布时间:2012/11/10 16:30:00
--  
 楼主,我测试了一下,是你Excel表格数据的问题,第一张表,有14列,但列看不见,你重建一下表格吧。

 或者改一下代码

Dim ny As String = Forms("合并").Controls("ComboBox1").Text
Dim y1 As String = CInt(Left(ny,4))-1
\'生成ysmx临时表
Dim o As new QueryBuilder
o.TableName = "ysmx"
o.SelectString = "Select DISTINCT 年月 From {应收明细} WHERE Left(年月,4)>= \'" & y1 & "\'"
o.Build
If DataTables("ysmx").Find("年月 = \'" & ny & "\'") IsNot Nothing Then
    messagebox.show("当月数据已导入","提示")
    Return
End If
Dim Book As New XLS.Book(ProjectPath & "应收明细.xls")
MainTable = Tables("应收明细")
For i As Integer = 0 To Book.Sheets.Count -1
    Dim a As String = Book.Sheets(i).Name
    If a = ny  Then
        Dim Sheet As XLS.Sheet = Book.Sheets(i)
        Tables("应收明细").StopRedraw()
        For r As Integer = 1 To Sheet.Rows.Count - 1
            Dim Filter1 As String = ""
            For c As Integer = 0 To Sheet.Cols.Count - 1
                If c > 0 Then
                    Filter1 = Filter1 & " And "
                End If
                If sheet(r,c).Value <> "" AndAlso CurrentTable.DataTable.DataCols(sheet(0,c).Value).IsNumeric Then
                    Filter1 = Filter1 & sheet(0,c).Value & " = " & sheet(r,c).Value
                ElseIf sheet(r,c).Value = "" And sheet(0, c).Value <> "" Then
                    Filter1 = Filter1 & sheet(0,c).Value & " is null "
                Else
                    Filter1 = Filter1 & sheet(0,c).Value & " = \'" & sheet(r,c).Value & "\'"
                End If
            Next

            If CurrentTable.DataTable.Find(Filter1) Is Nothing Then
                Dim dr As Row = CurrentTable.AddNew()
                For c As Integer = 0 To Sheet.Cols.Count - 1
                    dr(Sheet(0,c).Value) = Sheet(r,c).Value
                Next
            End If
        Next
        DataTables("应收明细").ReplaceFor("年月", ny ,"[年月] is null")
        \'数据格式化
        Dim t As Table = Tables("应收明细")
        Dim s As String
        For Each dc As DataCol In t.DataTable.DataCols
            If dc.name <> "年月" AndAlso dc.name <> "单位" Then
                s = "0.00"
                t.DataTable.DataCols(dc.Name).SetFormat(s)
            End  If
        Next
        Tables("应收明细").Sort = "年月,单位"
        Tables("应收明细").AutoSizeCols()
        DataTables("应收明细").DeleteFor("[单位] is null ")
        Tables("应收明细").ListMode = True
        DataTables("应收明细").Save()
        Tables("应收明细").ResumeRedraw()
        Return
    End If
Next
Messagebox.show("需导入的数据不存在!","提示")

--  作者:yyzlxc
--  发布时间:2012/11/10 16:59:00
--  
谢谢lin_hailun老师的指教,发现是excel的问题,重建表格后,问题解决,再次感谢lin_hailun老师!!