以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  [求助]execl导入时间列时只导入了日期部分  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=106892)

--  作者:jyh7081
--  发布时间:2017/9/17 20:08:00
--  [求助]execl导入时间列时只导入了日期部分
列属性查看没有问题,导入后日期部分正确,时间部分都是00:00。见下图:

图片点击可在新窗口打开查看此主题相关图片如下:image 1.png
图片点击可在新窗口打开查看
实例:
 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:导入.rar

代码如下:
Dim dlg As New OpenFileDialog     
dlg.Filter = "Excel文件|*.xls;*.xlsx"
If dlg.ShowDialog =DialogResult.OK Then
    Dim t As Table = Tables("事务主表")
    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("案号 = \'" & 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
                r(cname) = sheet(n, i).Text
            End If
        Next
    Next
    msgbox("新增" & newcount & "    " & "更新旧数据" & oldcount)
    \'msgbox("新增" & newcount)
    
    t.ResumeRedraw()
End If

--  作者:有点甜
--  发布时间:2017/9/17 20:17:00
--  

bug 要换方法

 

http://foxtable.com/bbs/dispbbs.asp?BoardID=2&ID=65686&skin=0

 


--  作者:jyh7081
--  发布时间:2017/9/17 21:53:00
--  [求助]
可以正确导入时间列,要是能识别列标题,且不用设置列数、列位置就好啦。
--  作者:有点甜
--  发布时间:2017/9/17 21:55:00
--  
以下是引用jyh7081在2017/9/17 21:53:00的发言:
可以正确导入时间列,要是能识别列标题,且不用设置列数、列位置就好啦。

 

参考

 

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


--  作者:jyh7081
--  发布时间:2017/9/17 22:13:00
--  
但这个例子不能正确导入长时间列
--  作者:有点甜
--  发布时间:2017/9/17 22:52:00
--  
以下是引用jyh7081在2017/9/17 22:13:00的发言:
但这个例子不能正确导入长时间列

 

可以导入的,你略加修改即可。或者,你上传一个例子说明。


--  作者:jyh7081
--  发布时间:2017/9/17 23:09:00
--  
 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:导入.rar


--  作者:有点甜
--  发布时间:2017/9/18
--  

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 App As New MSExcel.Application
        try
            Dim Wb As MSExcel.WorkBook = App.WorkBooks.Open(dlg.FileName)
            Dim Ws As MSExcel.WorkSheet = Wb.WorkSheets(1)
            wb.saved = True
            Dim Rg As MSExcel.Range = Ws.UsedRange
            Dim ary = rg.value
           
            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 = 1 To Rg.Columns.count
                If  dt.DataCols.Contains(ary(1,c).replace(" ", "")) Then
                    nms.Add(ary(1,c).replace(" ", ""), c)
                End If
            Next

            For n As Integer = 2 To Rg.Rows.count
                Dim sfzhm As String = ary(n,nms("案号"))
                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 ary(dic(key),nms(m)) = "" OrElse ary(dic(key),nms(m))= "False" OrElse ary(dic(key),nms(m)) = 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(ary(dic(key),nms(m)))
                    Else If dt.DataCols(m).IsDate Then
                        Dim d As Date
                        If Date.TryParse(ary(dic(key),nms(m)), d)
                            key(m) = d
                        End If
                       
                    Else
                        key(m) = ary(dic(key),nms(m))
                    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 ary(l,nms(m))= "" OrElse ary(l,nms(m))= "False" OrElse ary(l,nms(m))= 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(ary(l,nms(m)))
                    Else If dt.DataCols(m).IsDate Then
                        Dim d As Date
                        If Date.TryParse(ary(l,nms(m)), d)
                            ndr (m) = d
                        End If
                    Else
                       
                        ndr (m) = ary(l,nms(m))
                    End If
                Next
            Next
            dt.ResumeRedraw()

            app.quit
            \'systemready = True
        catch ex As exception
            msgbox(ex.message)
            app.quit
        End try
    End If
End If

[此贴子已经被作者于2017/9/18 0:03:20编辑过]

--  作者:jyh7081
--  发布时间:2017/10/30 12:23:00
--  

甜老师好!

这段代码如果是以标志列追加数据,完全可以通用了。

但还是想请老师再完善一下:如果标志列已有的记录就更新(覆盖),没有的数据就追加,导入后再提示更新记录数、追加记录数。

以上想法,自己没有能力修改,希望老师再次相助。

谢谢!


--  作者:有点甜
--  发布时间:2017/10/30 12:30:00
--  

msgbox("新增了" & ls.count)

msgbox("覆盖了" & dic.count)