以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  从字符串“*”到类型“Double”的转换无效,代码是哪里的问题?  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=116915)

--  作者:sunion
--  发布时间:2018/4/2 17:07:00
--  从字符串“*”到类型“Double”的转换无效,代码是哪里的问题?
导入EXCEL时代码出现错误,其中"语文","数学","英语","物理","化学","政治","历史","地理","生物","体育"在FOX表中的格式为双精度,请问大家这是哪里的问题?考试类型、考试时间、年级、考试时间为字符格式
.NET Framework 版本:2.0.50727.8762
Foxtable 版本:2017.12.31.1
错误所在事件:窗口,录入成绩,Button3,Click
详细错误信息:
从字符串“*”到类型“Double”的转换无效。
输入字符串的格式不正确。

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(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
\'-------------------赋值
        Dim str1 As String = Forms("录入成绩").Controls("ComboBox1").Value  \'从控件获取值
        Dim nm() As String = {"考试名称"}
        For m As Integer = 0 To nm.Length - 1
            r(nm(m)) = str1
        Next
\'--------------------赋值
 Dim str2 As String = Forms("录入成绩").Controls("ComboBox2").Value  \'从控件获取值
        Dim nm2() As String = {"考试类型"}
        For m As Integer = 0 To nm2.Length - 1
            r(nm2(m)) = str2
        Next
\'--------------------赋值
 Dim str3 As String = Forms("录入成绩").Controls("ComboBox3").Value \'从控件获取值
        Dim nm3() As String = {"年级"}
        For m As Integer = 0 To nm3.Length - 1
            r(nm3(m)) = str3
        Next
\'--------------------赋值
 Dim str4 As String = Forms("录入成绩").Controls("textBox1").Value \'从控件获取值
        Dim nm4() As String = {"考试时间"}
        For m As Integer = 0 To nm4.Length - 1
            r(nm4(m)) = str4
        Next
    Next
    Tables("成绩总表").ResumeRedraw()
 MessageBox.Show("成绩导入完成,你可以进行其他操作!") 
End If

--  作者:有点甜
--  发布时间:2018/4/2 17:14:00
--  
        For m As Integer =0 To nms.Length -1
            r(nms(m)) = Sheet(n,m).value
        Next

 

改成

 

        For m As Integer =0 To nms.Length -1
            If r.Table.Cols(nms(m)).IsNumeric then
                r(nms(m)) = val(Sheet(n,m).value)
            Ellse
                r(nms(m)) = Sheet(n,m).value
            End If
        Next