how to get xls file from a ZipArchiveEntry EPPlus C#

北城以北 提交于 2021-01-29 06:01:56

问题


i'am trying to get an xls file from an ZipArchive but cant get it with EPPLUS

  foreach (ZipArchiveEntry entry in archive.Entries)
                {
                    if (entry != null)
                    {                        
                        string filepath = entry.FullName;
                        FileInfo fileInfo = new FileInfo(filepath);
                        
                        //here i got the excel package with the xls file inside the excelPackage 
                        using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))
                        {                            
                      //but here impossible de get the worksheet or workbook inside or anything else    
                            ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.FirstOrDefault();
                            int totalColomn = worksheet.Dimension.End.Column;
                            int nbrsheet = excelPackage.Workbook.Worksheets.Count();
                        }
                    }
               }

the ExcelPackage i get in debug i see the xls file on debug inside the excelpackage but just when i try to get worksheet it exit without exception code....

same here when trying with entryStream


                        using (var entryStream = entry.Open())
                        {
                       //Cant even get the excelpackage, it crash here without exception
                            using (ExcelPackage excelPackage = new ExcelPackage(entryStream))
                            {
                                ExcelWorksheet worksheetest = excelPackage.Workbook.Worksheets.FirstOrDefault();
                            }
                        }

the stream here seem also strange ... entryStream Debug

Working with .NET CORE Blazor ServerSide, ePPLUS 4.5

Thanks for helping


回答1:


entry.FullName refers to the full path to the file inside the zip archive, while FileInfo describes a file in the filesystem of the OS, which is a completely different thing. You haven't extracted anything to the OS filesystem yet, so the FileInfo won't refer to a file that actually exists.

Try the ExcelPackage constructor that takes a Stream, which you can get directly from a ZipArchiveEntry:

using (var entryStream = entry.Open())
{
    using (ExcelPackage excelPackage = new ExcelPackage(entryStream))
    {
        // ...
    }
}



回答2:


I find the problem.

it was that i tried to get an xls file and the epplus library dont work with it... you have to be careful, EPplus dont work with xls file

So , your solution Jeff is working, it was my fault, didn't specified the extension of my excel file... sorry

-> EPlus with an .xlsx OK, not .xls

My bad. Thanks anyway :-)



来源:https://stackoverflow.com/questions/63795038/how-to-get-xls-file-from-a-ziparchiveentry-epplus-c-sharp

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