以文本方式查看主题

-  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=70554)

--  作者:yinyb36
--  发布时间:2015/6/24 11:24:00
--  [求助]选择EXCEL,当有密码时出错

Dim dlg As New OpenFileDialog
dlg.Filter= "Excel 97-2003文件(*.xls)|*.xls|Excel 2007文件(*.xlsx)|*.xlsx"
If dlg.ShowDialog = DialogResult.Ok Then
    Dim cmb1 As WinForm.ComboBox = e.Form.Controls("ComboBox1")
    Dim txtb1 As WinForm.TextBox = e.Form.Controls("TextBox1")
    txtb1.value = dlg.FileName
    cmb1.Value = ""
    If txtb1.Value > "" Then
        Book = New XLS.Book(txtb1.value)
        cmb1.items.clear
        Dim str(Book.Sheets.count - 1) As String
        Dim Sheet As XLS.Sheet
        For i As Integer = 0 To Book.Sheets.count - 1
            str(i) = Book.Sheets(i).name \' str.add(Book.Sheets(i).name)
        Next
        cmb1.Items.AddRange(str)
    End If
    cmb1.Select()
End If

 

有密码时出错:

.NET Framework 版本:2.0.50727.3655
Foxtable 版本:2014.11.11.1
错误所在事件:窗口,Excel导入,Button1,Click
详细错误信息:
Object reference not set to an instance of an object.

 

 


怎么提前判断是否有密码?


--  作者:大红袍
--  发布时间:2015/6/24 11:35:00
--  

加入 try catch

 

Dim dlg As New OpenFileDialog
dlg.Filter= "Excel 97-2003文件(*.xls)|*.xls|Excel 2007文件(*.xlsx)|*.xlsx"
If dlg.ShowDialog = DialogResult.Ok Then
    Dim cmb1 As WinForm.ComboBox = e.Form.Controls("ComboBox1")
    Dim txtb1 As WinForm.TextBox = e.Form.Controls("TextBox1")
    txtb1.value = dlg.FileName
    cmb1.Value = ""
    If txtb1.Value > "" Then
        try
            Book = New XLS.Book(txtb1.value)
            cmb1.items.clear
            Dim str(Book.Sheets.count - 1) As String
            Dim Sheet As XLS.Sheet
            For i As Integer = 0 To Book.Sheets.count - 1
                str(i) = Book.Sheets(i).name \' str.add(Book.Sheets(i).name)
            Next
            cmb1.Items.AddRange(str)
        catch ex As exception
            msgbox("打开excel文件出错")
        End try
    End If
    cmb1.Select()
End If


--  作者:yinyb36
--  发布时间:2015/6/24 11:38:00
--  

OK!已解决!谢谢!

Dim dlg As New OpenFileDialog
dlg.Filter= "Excel 97-2003文件(*.xls)|*.xls|Excel 2007文件(*.xlsx)|*.xlsx"
If dlg.ShowDialog = DialogResult.Ok Then
    Dim cmb1 As WinForm.ComboBox = e.Form.Controls("ComboBox1")
    Dim txtb1 As WinForm.TextBox = e.Form.Controls("TextBox1")
    txtb1.value = dlg.FileName
    cmb1.Value = ""
    If txtb1.Value > "" Then
        Try
            Book = New XLS.Book(txtb1.value)
            cmb1.items.clear
            Dim str(Book.Sheets.count - 1) As String
            Dim Sheet As XLS.Sheet
            For i As Integer = 0 To Book.Sheets.count - 1
                str(i) = Book.Sheets(i).name \' str.add(Book.Sheets(i).name)
            Next
            cmb1.Items.AddRange(str)
        catch ex As exception
            MessageBox.Show("打开Excel文件出错!" & vbcrlf & vbcrlf & "如果Excel文件有密码,请去掉密码后再导入!")
            txtb1.value = Nothing
        End try
    End If
    cmb1.Select()
End If