以文本方式查看主题

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

--  作者:yetle
--  发布时间:2020/11/17 14:09:00
--  excel导入

图片点击可在新窗口打开查看此主题相关图片如下:2ca7757b-88bc-45c5-ad16-250c92cebead.png
图片点击可在新窗口打开查看

excel导入时,如果表格是的数值内容是这种形式,不调整excel格式的情况下,代码要怎么写才能将数值导入进来
--  作者:有点蓝
--  发布时间:2020/11/17 14:25:00
--  
使用代码去掉数据里的逗号%这些:http://www.foxtable.com/webhelp/topics/2334.htm

比如
……
For n As Integer = 1 To Sheet.Rows.Count -
    Dim 
r As Row = Tables("订单").AddNew()
    For 
m As Integer = 0 To nms.Length - 1
dim s as string = Sheet(n,m).text.replace(","<"")
if s.contains("%") then 
         
r(nms(m)) = val(s.replace("%","")) * 100
else
 r(nms(m)) = val(s)
end if
    Next
Next
……

--  作者:yetle
--  发布时间:2020/11/17 15:01:00
--  
按老师教的这种写法,数值都正常导入进来了,可是字符却导不进来,真正的字符变成了0

Dim dlg As new OpenFileDialog
dlg.Filter = "excel文件|*.xls;*.xlsx"
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)
        
        Tables("版块业绩").StopRedraw()
        \'Dim nms() As String = {"支付金额","外呼时间","录入时间"}
        Dim nms() As String = {"支付金额"}
        Dim dict As new Dictionary(of String,Integer)
        Dim dict2 As new Dictionary(of String,Integer)
        Dim Rg As MSExcel.Range = Ws.UsedRange
        Dim ary = rg.value
        For c As Integer = 1 To rg.Columns.Count
            If array.Indexof(nms, ary(1, c)) >= 0 Then
                dict.add(ary(1, c), c)
            End If
            If Tables("版块业绩").Cols.Contains(ary(1,c)) Then
                dict2.add(ary(1, c), c)
            End If
        Next
        Dim newcount As Integer = 0
        Dim Modifycount As Integer = 0


            For n As Integer = 2 To rg.Rows.count
        Dim rq As Date = e.Form.Controls("日期").value \'ary(n, 4)
        Dim dr As DataRow = DataTables("版块业绩").sqlFind("日期 = \'" & rq & "\'")                
                
                If dr Is Nothing Then \'如果不存在同编号的订单
                    dr =  DataTables("版块业绩").AddNew()
                    dr("日期")=rq
                    For Each key As String In dict2.Keys
Dim s As String = ary(n,dict2(key))
If s.contains("%") Then 
         dr(key) = val(s.replace("%","")) * 100
Else
 dr(key) = val(s)
End If

                        \'dr(key) = ary(n,dict2(key))
                    Next
                    newcount += 1
                    
                Else
                    
                    For Each key As String In dict.Keys

Dim s As String = ary(n,dict(key))
If s.contains("%") Then 
         dr(key) = val(s.replace("%","")) * 100
Else
 dr(key) = val(s)
End If

                        \'dr(key) = ary(n,dict(key))
                    Next
                    Modifycount += 1


                End If

        Next
Tables("店铺流量来源_table1").save()
        Tables("版块业绩").ResumeRedraw()
        msgbox(newcount & "   " & Modifycount)
    catch ex As exception
        msgbox(ex.message)
        app.quit
    End try
End If

--  作者:有点蓝
--  发布时间:2020/11/17 15:09:00
--  
判断一下列类型,数值列再做这种处理
--  作者:yetle
--  发布时间:2020/11/17 15:15:00
--  
请教一下这里怎么写判断列类型
--  作者:有点蓝
--  发布时间:2020/11/17 15:17:00
--  
http://www.foxtable.com/webhelp/topics/1375.htm

If DataTables("版块业绩").datacols(key).IsNumeric