Foxtable(狐表)用户栏目专家坐堂 → [求助]execl导入时间列时只导入了日期部分


  共有4664人关注过本帖树形打印复制链接

主题:[求助]execl导入时间列时只导入了日期部分

帅哥哟,离线,有人找我吗?
有点甜
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2017/9/17 20:17:00 [显示全部帖子]


 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2017/9/17 21:55:00 [显示全部帖子]

以下是引用jyh7081在2017/9/17 21:53:00的发言:
可以正确导入时间列,要是能识别列标题,且不用设置列数、列位置就好啦。

 

参考

 

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


 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2017/9/17 22:52:00 [显示全部帖子]

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

 

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


 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By: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编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  5楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2017/10/30 12:30:00 [显示全部帖子]

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

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


 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  6楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2017/10/30 12:40:00 [显示全部帖子]

 执行代码有什么问题,具体举例说明。

 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  7楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2018/1/16 14:26:00 [显示全部帖子]

新增导入,这样写;覆盖导入参考8楼

 

Dim dt As DataTable = DataTables("事务主表")
Dim Result As DialogResult
Result = MessageBox.Show("有重复案号,是否覆盖导入?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If Result = DialogResult.Yes Then
    '覆盖导入

Else
    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
                ls.add(n)
            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


 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  8楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2018/1/16 17:13: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 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
           
            Dim flag As Boolean = True
            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 IsNot Nothing Then
                    Result = MessageBox.Show("有重复案号,是否覆盖导入?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                    If Result = DialogResult.Yes Then
                        flag = True
                    Else
                        flag = False
                    End If
                    Exit For
                End If
            Next
            If flag = True
                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
            Else
                For n As Integer = 2 To Rg.Rows.count
                    Dim sfzhm As String = ary(n,nms("案号"))
                    If sfzhm = "" Then Continue For
                    ls.add(n)
                Next
            End If
           
            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

 回到顶部