Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
设置现有Excel文件的密码_Excel_Vb.net - Fatal编程技术网

设置现有Excel文件的密码

设置现有Excel文件的密码,excel,vb.net,Excel,Vb.net,我的应用程序创建并导出Excel(.xls),其中包含数据库中的数据,我需要为其设置密码。我正在使用以下代码创建文件: Sub ExportDataSetToExcel(ByVal dataSet As DataSet, filePath As String) Dim result() As Byte = Nothing Try Dim fileTemp As String = My.Computer.FileSystem.GetTempFileNa

我的应用程序创建并导出Excel(.xls),其中包含数据库中的数据,我需要为其设置密码。我正在使用以下代码创建文件:

    Sub ExportDataSetToExcel(ByVal dataSet As DataSet, filePath As String)

    Dim result() As Byte = Nothing

    Try

        Dim fileTemp As String = My.Computer.FileSystem.GetTempFileName
        If IsNothing(fileTemp) OrElse fileTemp.Length = 0 Then
            Throw New Exception("Connot write on temp folder. Check folder permissions")
        End If

        fileTemp &= ".xls"

        If dataSet IsNot Nothing AndAlso dataSet.Tables.Count > 0 Then
            Dim sConn As String = GetConnectionString(fileTemp, "Write")

            Using connection As OleDbConnection = New OleDbConnection(sConn)

                connection.Open()

                For Each dt As DataTable In dataSet.Tables

                    Dim strCreateTableStruct As String = BuildCreateTableCommand(dt)

                    If String.IsNullOrEmpty(strCreateTableStruct) Then Return

                    Using command As OleDbCommand = New OleDbCommand(strCreateTableStruct, connection)
                        command.ExecuteNonQuery()
                        Dim totRows As Integer = dt.Rows.Count - 1

                        RaiseEvent OnTotalRowsDetected(totRows)

                        For rowIndex As Integer = 0 To dt.Rows.Count - 1
                            Using command1 As OleDbCommand = New OleDbCommand(BuildInsertCommand(dt, rowIndex), connection)
                                command1.ExecuteNonQuery()
                            End Using
                            RaiseEvent OnRowExported(rowIndex)
                        Next
                    End Using

                Next

            End Using

        End If


        If IO.File.Exists(fileTemp) Then
            result = IO.File.ReadAllBytes(fileTemp)
            IO.File.Delete(fileTemp)
        End If

        If result IsNot Nothing AndAlso result.Length > 0 Then
            Using Fs As FileStream = New FileStream(filePath, FileMode.OpenOrCreate)
                Fs.Write(result, 0, result.Length)
                'OrCreate
            End Using
        End If

    Catch eX As Exception
        MsgBox(eX.ToString)

    End Try

End Sub
    Sub SecureExcel(filePath As String)

    Dim book As New Workbook()
    book.loadfromfile(filePath)
    book.Password = "abc123"
    book.SaveAs("bdp.xls")

End Sub
因为这段代码是其他人写的,我只是重复使用了它,不知道有一半的事情是怎么做的,所以我不能修改它来创建带有密码的Excel,甚至是.xlsx格式的Excel

要创建密码,我使用了以下代码:

    Sub ExportDataSetToExcel(ByVal dataSet As DataSet, filePath As String)

    Dim result() As Byte = Nothing

    Try

        Dim fileTemp As String = My.Computer.FileSystem.GetTempFileName
        If IsNothing(fileTemp) OrElse fileTemp.Length = 0 Then
            Throw New Exception("Connot write on temp folder. Check folder permissions")
        End If

        fileTemp &= ".xls"

        If dataSet IsNot Nothing AndAlso dataSet.Tables.Count > 0 Then
            Dim sConn As String = GetConnectionString(fileTemp, "Write")

            Using connection As OleDbConnection = New OleDbConnection(sConn)

                connection.Open()

                For Each dt As DataTable In dataSet.Tables

                    Dim strCreateTableStruct As String = BuildCreateTableCommand(dt)

                    If String.IsNullOrEmpty(strCreateTableStruct) Then Return

                    Using command As OleDbCommand = New OleDbCommand(strCreateTableStruct, connection)
                        command.ExecuteNonQuery()
                        Dim totRows As Integer = dt.Rows.Count - 1

                        RaiseEvent OnTotalRowsDetected(totRows)

                        For rowIndex As Integer = 0 To dt.Rows.Count - 1
                            Using command1 As OleDbCommand = New OleDbCommand(BuildInsertCommand(dt, rowIndex), connection)
                                command1.ExecuteNonQuery()
                            End Using
                            RaiseEvent OnRowExported(rowIndex)
                        Next
                    End Using

                Next

            End Using

        End If


        If IO.File.Exists(fileTemp) Then
            result = IO.File.ReadAllBytes(fileTemp)
            IO.File.Delete(fileTemp)
        End If

        If result IsNot Nothing AndAlso result.Length > 0 Then
            Using Fs As FileStream = New FileStream(filePath, FileMode.OpenOrCreate)
                Fs.Write(result, 0, result.Length)
                'OrCreate
            End Using
        End If

    Catch eX As Exception
        MsgBox(eX.ToString)

    End Try

End Sub
    Sub SecureExcel(filePath As String)

    Dim book As New Workbook()
    book.loadfromfile(filePath)
    book.Password = "abc123"
    book.SaveAs("bdp.xls")

End Sub
但我得到了一个错误:

System.Runtime.InteropServices.COMException:'正在检索COM类 具有CLSID的组件的工厂 {00020819-0000-0000-C000-0000000000 46}由于以下原因失败 错误:80040154未注册类别(0x80040154 (REGDB_E_CLASSNOTREG))。'

我看到了一些关于这个错误的其他问题,但其中一些问题最终只是使用XML或其他什么,我不想进入其他技术领域,因为这是一个短期的解决方案,我只想要一个插入密码的简单解决方案。 另一个是“配置”或其他东西,将COM对象配置为DCOM或其他东西,但我找不到它们描述的资源

我们尝试的第一件事是手动插入密码,但每次我们通过应用程序更新文件时,密码都会消失

我可能的解决方案是什么?
谢谢。

当我将光标悬停在
GetTempFileName
上时,它会返回零字节文件的路径。我不知道它如何通过条件
OrElse fileTemp.Length=0
。嗨,谢谢你的回复,就像我说的我不是代码的原始作者一样,我只是从办公室的另一个人那里借来的。我只是想用这个或其他代码在文件中插入密码。