nissl-lab / npoi

a .NET library that can read/write Office formats without Microsoft Office installed. No COM+, no interop.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How can protected workbook excel file with npoi

mjjalala opened this issue · comments

@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

  • XLSX
  • XLS
  • DOCX
  • XLSM
  • OTHER

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

Check PR #883 for detail

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

There is no plan for this feature