nissl-lab / npoi

a .NET library that can read/write Office formats without Microsoft Office installed. No COM+, no interop.
Apache License 2.0
5.74k stars 1.43k forks source link

How can protected workbook excel file with npoi #1312

Open mjjalala opened 7 months ago

mjjalala commented 7 months ago

@tonyqus I have code that uses the NPOI library to export files to Excel. I want to use Protected Workbook to protect the file after exporting it. I see there is a problem when using EncryptionInfo. The error is "Object reference null"

Is there any example of password protecting workbook? Not the worksheet but whole workbook.

My code is: ' **** Code I am trying to get to work that encrypts and password protects the Excel file 'sheet1.ProtectSheet("password") ' to be replaced with dynamic password - this is a hardcoded placeholder 'Dim fs As New NPOIFSFileSystem 'Dim cm As ChainingMode = ChainingMode.cbc 'Dim info As New EncryptionInfo(fs, EncryptionMode.Agile) ' EncryptionInfo(EncryptionMode.Agile, CipherAlgorithm.aes192, HashAlgorithm.sha384, -1, -1, cm) 'Dim enc As Encryptor = info.Encryptor 'enc.ConfirmPassword("password") ' to be replaced with dynamic password - this is a hardcoded placeholder 'Using encryptedStream As Stream = enc.GetDataStream(fs) 'Using memoryStream As MemoryStream = New MemoryStream() 'workbook.Write(memoryStream) 'Dim encryptedBytes As Byte() = memoryStream.ToArray() 'encryptedStream.Write(encryptedBytes, 0, encryptedBytes.Length) 'Using fileStream As FileStream = New FileStream("encrypted.xlsx", FileMode.Create) 'encryptedStream.CopyTo(fileStream) 'End Using 'End Using 'End Using ' **** Code I am trying to get to work that encrypts and password protects the Excel file

File Type

tonyqus commented 7 months ago

It's not supported so far. NPOI can only open encrypted xlsx since 2.6.0.

Check PR #883 for detail

mjjalala commented 7 months ago

@tonyqus Do you have an estimated date to have this issue solved?

tonyqus commented 7 months ago

There is no plan for this feature