Access Protected Excel File with ExcelDataReader and Epplus

安稳与你 提交于 2019-12-07 08:56:54

问题


title pretty much says it all. Looking for a way to access a password protected excel file with ExcelDataReader and Epplus, can't find a proper answer.

If using ExcelDataReader, my code looks like

                excelStream = File.Open(excelFilePath, FileMode.Open, FileAccess.Read);
                excelReader = ExcelReaderFactory.CreateOpenXmlReader(excelStream);
                excelDataSet = excelReader.AsDataSet();

If using EPPlus my connection code looks like

            excelPackage = new ExcelPackage(new FileInfo(excelFilePath));
            excelWorkbook = excelPackage.Workbook;
            excelSheet = excelWorkbook.Worksheets[1];

EPPlus has some protection related methods but i can't figure out how to use them. ExcelDataReader doesnt seem to have any protection related methods.

Any tips appreciated, thanks.

EDIT: I do already know the password


回答1:


With EPPlus you can use

excelPackage = new ExcelPackage(new FileInfo(excelFilePath), "mypassword");

ExcelDataReader now supports opening password protected sheets. I opened an issue on their GitHub asking if they have such support and received a response saying that they do not, but after sometime they added support for some password methods. Details on the password methods they still do not support are in the link.




回答2:


With ExcelDataReadr you can access your protected file like this:

var conf = new ExcelReaderConfiguration { Password = "yourPassword" };
excelReader = ExcelReaderFactory.CreateOpenXmlReader(excelStream, conf);


来源:https://stackoverflow.com/questions/44978634/access-protected-excel-file-with-exceldatareader-and-epplus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!