Share via


How to open a password protected Excel file with NPOI

Question

Wednesday, November 13, 2013 10:36 AM

Default password is invalid for docId/saltData/saltHash

using (FileStream file = new FileStream(_path, FileMode.Open, FileAccess.Read))
                {
                    POIFSFileSystem fs = new POIFSFileSystem(file);
                    HSSFWorkbook hssfwb = new HSSFWorkbook(fs); //  Error at here
                    ISheet sheet = hssfwb.GetSheet("VPAB VPAC VPCA VPGA VPG2 VPGB V");
                    for (int row = 0; row <= sheet.LastRowNum; row++)
                    {
                        if (sheet.GetRow(row) != null) //null is when the row only contains empty cells 
                        {
                            Console.WriteLine(string.Format("Row {0} = {1}", row, sheet.GetRow(row).GetCell(0).StringCellValue));
                        }
                    }
                }

then i try below return no encryption info for password protected file

using (FileStream file = new FileStream(_path, FileMode.Open, FileAccess.Read))
                {
                    //DirectoryNode dn = new DirectoryNode();
                    NPOIFSFileSystem fs = new NPOIFSFileSystem(file);

                    EncryptionInfo info = new EncryptionInfo(fs);
                    Decryptor.GetInstance(info).VerifyPassword("hsbc123");
                    //d.verifyPassword(Decryptor.DEFAULT_PASSWORD);
                    XSSFWorkbook wb = new XSSFWorkbook(Decryptor.GetInstance(info).GetDataStream(fs));

one of computer do not have Excel, however need to open it and save as csv file, or just open and read its data

v!

v!

All replies (4)

Thursday, November 14, 2013 9:01 AM

Hi zvzzvz,

Try below code example using the latest version(2.0 beta1) of NPOI:

NPOI.POIFS.FileSystem.POIFSFileSystem nfs =
                new NPOI.POIFS.FileSystem.POIFSFileSystem(
                    new FileStream("C:\\test.xlsx", FileMode.Open, FileAccess.ReadWrite));
            string password = "123";
            EncryptionInfo info = new EncryptionInfo(nfs);
            Decryptor dc = Decryptor.GetInstance(info);
            bool b = dc.VerifyPassword(password);
            if (b)
            {
                Console.WriteLine("Authenticated.");
            }
            else
            {
                Console.WriteLine("Wrong password.");
            }

            Stream excelData = dc.GetDataStream(nfs);

NPOI.HSSF.UserModel namespace and NPOI.POIFS.Crypt namespace are needed.

And I would like you to refer to this thread, the answer from Hans is very good.

For Microsoft.Office.Interop.Excel namespace, to open a excel with password is very simple, just check the MSDN online document here:

Workbooks.Open method

We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.


Thursday, November 21, 2013 3:31 AM

i use dotnet4

get the same error

no such entry: "EncryptionInfo"

v!


Friday, November 22, 2013 2:39 AM

Hi zvzzvz,

Sorry for the delay!

EncryptionInfo class is in the NPOI.POIFS.Crypt name space, located in NPOI.dll, please use the latest version(2.0 beta1) of NPOI.

Thanks.

We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.


Thursday, November 28, 2013 2:37 AM

https://drive.google.com/file/d/0B2D69u2pweEvSWt0TUUwSDhFc0k/edit?usp=sharing

really have error even if using beta 1

Need help!!!!!!!!!!!!!!!!!!

i encrypted again with Excel and save and it return

This document is both encrypted and password protected. The Office Open XML FOrmats available in Office 2007 and later
provide stronger encryption. Do you want to increase the security of this docuemtn by convert to an Office Open XML Format?

then i use another file to test, it can decrypted, only this file has problem, what should i do next?

v!