如何以编程方式消除excel文件的只读功能,以便将其保存在vb.net中?

问题描述

如何以编程方式消除excel文件的只读功能,以便将其保存在vb.net中。我做了很多研究并阅读了很多文章,但我找不到正确的代码来实现这一点。触发错误的部分是

oSheet.SaveAs("C:\Users\Vince\Desktop\Research\Current Research\Automating the GSRS\Usernames.xlsx")

    Private Sub Button1_Click_1(sender As Object,e As EventArgs) Handles butReset.Click
    Path = "C:\Users\Vince\Desktop\Research\Current Research\Automating the GSRS\Usernames.xlsx"
    oExcel = CreateObject("Excel.Application")
    oBook = oExcel.Workbooks.Open(Path)
    oSheet = oBook.Worksheets(1)
    oSheet.activate()
    sheetCount = oSheet.Cells(oSheet.Rows.Count,"A").End(XlDirection.xlUp).row
    initialusername = "admin"
    initialpassword = "password"
    adminusername = InputBox("Please Enter admin username","Alert")
    adminpassword = InputBox("Please Enter admin password","Alert")
    usernameverify = InputBox("Please Enter new admin username","Alert")
    passwordverify = InputBox("Please Enter new admin password","Alert")
    do while String.IsNullOrEmpty(adminusername) = True And String.IsNullOrEmpty(adminpassword) = True
        MsgBox("You are not authorized to reset the login credentials!","Alert")
        Me.txtusername.Text = ""
        Me.txtpassword.Text = ""
        Exit Do
    Loop
    do while String.IsNullOrEmpty(adminusername) = False And String.IsNullOrEmpty(adminpassword) = False
        For j = 2 To sheetCount
            For k = 2 To sheetCount
                If oSheet.Cells(j,5).Text = "Administrator" And oSheet.Cells(k,5).Text = "Administrator" Then
                    ReDim Preserve usernames(j)
                    ReDim Preserve passwords(k)
                    usernames(j) = oSheet.Cells(j,3).Text
                    passwords(k) = oSheet.Cells(k,4).Text
                    Me.txtusername.Text = ""
                    Me.txtpassword.Text = ""
                End If
            Next
        Next
        For Each username As String In usernames
            For Each password As String In passwords
                If (adminusername = initialusername Or adminusername = username) And (adminpassword = initialpassword Or adminpassword = password) Then
                    For j = 2 To sheetCount
                        For k = 2 To sheetCount
                            If oSheet.Cells(j,5).Text = "Administrator" Then
                                oSheet.Cells(j,3).Value = usernameverify
                                oSheet.Cells(k,4).Value = passwordverify
                                MsgBox("Administrator credentials have been updated","Success")
                                oSheet.SaveAs("C:\Users\Vince\Desktop\Research\Current Research\Automating the GSRS\Usernames.xlsx")
                                Me.txtusername.Text = ""
                                Me.txtpassword.Text = ""
                            End If
                        Next
                    Next
                End If
            Next
        Next
        For Each username As String In usernames
            For Each password As String In passwords
                If (adminusername <> initialusername Or adminusername <> username) And (adminpassword <> initialpassword Or adminpassword <> password) Then
                    MsgBox("You are not authorized to reset the login credentials!","Alert")
                    Me.txtusername.Text = ""
                    Me.txtpassword.Text = ""
                    Exit Sub
                End If
            Next
        Next
    Loop
    do while String.IsNullOrEmpty(usernameverify) = True And String.IsNullOrEmpty(passwordverify) = False
        MsgBox("You are not authorized to reset the login credentials!","Alert")
        Me.txtusername.Text = ""
        Me.txtpassword.Text = ""
        Exit Do
    Loop
    do while String.IsNullOrEmpty(usernameverify) = False And String.IsNullOrEmpty(passwordverify) = True
        MsgBox("You are not authorized to reset the login credentials!","Alert")
        Me.txtusername.Text = ""
        Me.txtpassword.Text = ""
        Exit Do
    Loop
    do while String.IsNullOrEmpty(adminusername) = True And String.IsNullOrEmpty(adminpassword) = False
        MsgBox("You are not authorized to reset the login credentials!","Alert")
        Me.txtusername.Text = ""
        Me.txtpassword.Text = ""
        Exit Do
    Loop
    do while String.IsNullOrEmpty(adminusername) = False And String.IsNullOrEmpty(adminpassword) = True
        MsgBox("You are not authorized to reset the login credentials!","Alert")
        Me.txtusername.Text = ""
        Me.txtpassword.Text = ""
        Exit Do
    Loop
End Sub

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)