以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  [求助] 导入Excel文件时显示非法文件格式  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=118755)

--  作者:2900819580
--  发布时间:2018/5/10 13:40:00
--  [求助] 导入Excel文件时显示非法文件格式

.NET Framework 版本:2.0.50727.5485
Foxtable 版本:2018.3.9.1
错误所在事件:窗口,PCBA-BOM,But导入,Click
详细错误信息:
非法文件格式。

If e.Form.Controls("DropBox1").text > "" Then
    Dim dlg As New OpenFileDialog \'定义一个新的OpenFileDialog
    dlg.Filter= "Excel文件|*.xls;*.xlsx" \'设置筛选器
    dlg.InitialDirectory =  "d:\\king\\"
   
    If dlg.ShowDialog = DialogResult.Ok Then \'如果用户单击了确定按钮
        e.Form.Controls("But生成表").performclick
        Dim tb As Table = Tables(Vars("TbName"))

        \'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        Dim Book As New XLS.Book(dlg.FileName)
        Dim Sheet As XLS.Sheet = Book.Sheets(0)
        tb.StopRedraw()
        \'注意以下数组中列名称的顺序,必须和Excel表中的列顺序一致
        Dim nms() As String = {"Comment","Footprint","Quantity"}
        \'注意下面的循环变量从1开始,而不是从0开始,因为Excel表的第一行是标题
        For n As Integer = 1 To Sheet.Rows.Count -1
            Dim r As Row = tb.AddNew()
            For m As Integer = 0 To nms.Length - 1
                r(nms(m)) = Sheet(n,m).Value
            Next
        Next
        tb.ResumeRedraw()       
       
        \'------------------------------------------------------------
        e.Form.Controls("But生成物料").performclick
    Else
        Exit Sub
    End If
Else
    MessageBox.Show("请输入产品或半成品代码后再导入")
End If


--  作者:有点甜
--  发布时间:2018/5/10 14:40:00
--  

1、单独测试代码

 

Dim dlg As New OpenFileDialog \'定义一个新的OpenFileDialog
dlg.Filter= "Excel文件|*.xls;*.xlsx" \'设置筛选器
dlg.InitialDirectory =  "d:\\king\\"
If dlg.ShowDialog = DialogResult.Ok Then \'如果用户单击了确定按钮
    Dim tb As Table = Tables(Vars("TbName"))
    \'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Dim Book As New XLS.Book(dlg.FileName)
    Dim Sheet As XLS.Sheet = Book.Sheets(0)
    tb.StopRedraw()
    \'注意以下数组中列名称的顺序,必须和Excel表中的列顺序一致
    Dim nms() As String = {"Comment","Footprint","Quantity"}
    \'注意下面的循环变量从1开始,而不是从0开始,因为Excel表的第一行是标题
    For n As Integer = 1 To Sheet.Rows.Count -1
        Dim r As Row = tb.AddNew()
        For m As Integer = 0 To nms.Length - 1
            r(nms(m)) = Sheet(n,m).Value
        Next
    Next
    tb.ResumeRedraw()
   
End If

 

2、如果还是有问题,excel文件发上来测试