以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  [求助]设计导入器的时候 怎么定义导入空表时不报错  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=74866)

--  作者:leopain
--  发布时间:2015/9/21 15:40:00
--  [求助]设计导入器的时候 怎么定义导入空表时不报错
\'-----定义导入参数


\'-----获取Excel文件名
Dim dlg As New OpenFileDialog
dlg.Filter = "Excel文件|*.xls;*.xlsx"
Dim bookName,sheetName As String


If dlg.ShowDialog = DialogResult.OK Then
    bookname = dlg.FileName
    e.Form.Controls("bookName").text = bookName
    \'-----定义excel文件-----
    Dim Book As New XLS.Book(bookname)
    
    \'------遍历表名-----
    For Each st As xls.sheet In book.sheets
        sheetName = sheetName & st.name & "|"
    Next
    
    
    \'------设置默认值 
    Dim stname As WinForm.ComboBox = e.Form.Controls("sheetname")
    stname.combolist = sheetname.TrimEnd("|")
    stname.SelectedIndex = 0
    
    Dim btn As WinForm.Button = e.Form.Controls("读出数据")
    btn.PerformClick()
End If


这是我导入窗口的代码 
但是导入空表时会报错呢.

--  作者:大红袍
--  发布时间:2015/9/21 15:43:00
--  

空表?指excel文件没有sheet?

 

stname.SelectedIndex = 0

 

改成

 

If stname.combolist = Nothing Then Return

stname.SelectedIndex = 0


--  作者:leopain
--  发布时间:2015/9/21 15:46:00
--  
是指sheet里没数据.. 我今天导入时点错点到了一个新建excel文件 出现报错
--  作者:大红袍
--  发布时间:2015/9/21 15:49:00
--  

贴出按钮的代码,贴出报的错误。


--  作者:leopain
--  发布时间:2015/9/21 16:03:00
--  

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



--  作者:大红袍
--  发布时间:2015/9/21 16:22:00
--  

按钮代码

 

Dim bookname As WinForm.TextBox = e.Form.Controls("bookName")
If bookname.Value = "" Then
   
    MessageBox.show("请点击 [Excel文件] 按钮,选取Excel文件! ","错误提示",MessageBoxButtons.OK,MessageBoxIcon.Error)
    Return
End If

Dim sheetname As WinForm.ComboBox = e.Form.Controls("sheetname")
If sheetname.SelectedValue = "" Then
    MessageBox.show("导入工作表不能为空 ! ","错误提示",MessageBoxButtons.OK,MessageBoxIcon.Error)
    Return
End If

Dim Book As New XLS.Book(bookname.text)
Dim sheet As XLS.Sheet = book.sheets(sheetname.SelectedValue)
Dim bt As WinForm.CheckBox = e.Form.Controls("标题")
\'设定最大取值列
Dim maxc As WinForm.NumericComboBox = e.Form.Controls("最大列")
If maxc.value > sheet.Cols.Count Or maxc.value = 0 Then
    maxc.Value = sheet.Cols.Count
End If
Dim qsh As WinForm.NumericComboBox = e.Form.Controls("有效起始行")
Dim cblst As String
\'------遍历数据-----
Dim qs As Integer = cint(qsh.Text)
For r As Integer = qs-1 To qs+6
    Dim lst As String = ""
    For c As Integer = 0 To maxc.value - 1
        Dim data As String = sheet(r,c).value
        lst = lst & data.PadLeft(8," ") & ","
    Next
    cblst = cblst & lst & "|"
Next
\'-----标题-----\'
Dim h As String

Dim cbl As String = ""


For c As Integer = 0 To maxc.Value -1
    h = "第" & cstr(c+1) & "列"
    If bt.Checked Then
        cbl = cbl & h & "-->" & sheet(qs-1,c).Text & "|"
    Else
        cbl = cbl & h & "|"
    End If
Next
Dim lst5r As WinForm.ListBox = e.Form.Controls("lst5r")
lst5r.ComboList = cblst
e.Form.Controls("表字段0").combolist = cbl.TrimEnd("|")

\'dcl.ComboList = cbl.TrimEnd("|")