Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Epplus not reading excel file

Below is my code to read excel file.

Code.

FileInfo newFile = new FileInfo("C:\\Excel\\SampleStockTakeExceptionReport.xls");
ExcelPackage pck = new ExcelPackage(newFile);
var ws = pck.Workbook.Worksheets.Add("Content");
ws.View.ShowGridLines = false;
ws.Cells["J12"].Value = "Test Write";
pck.Save();
System.Diagnostics.Process.Start("C:\\Excel\\SampleStockTakeExceptionReport.xls");

When i run the code it throw a runtime error.

Error

System.Exception: Can not open the package. Package is an OLE compound document. If this is an encrypted package, please supply the password ---> System.IO.FileFormatException: File contains corrupted data.
   at MS.Internal.IO.Zip.ZipIOEndOfCentralDirectoryBlock.FindPosition(Stream archiveStream)
   at MS.Internal.IO.Zip.ZipIOEndOfCentralDirectoryBlock.SeekableLoad(ZipIOBlockManager blockManager)
   at MS.Internal.IO.Zip.ZipArchive..ctor(Stream archiveStream, FileMode mode, FileAccess access, Boolean streaming, Boolean ownStream)
   at MS.Internal.IO.Zip.ZipArchive.OpenOnStream(Stream stream, FileMode mode, FileAccess access, Boolean streaming)
   at System.IO.Packaging.ZipPackage..ctor(Stream s, FileMode mode, FileAccess access, Boolean streaming)
   at System.IO.Packaging.Package.Open(Stream stream, FileMode packageMode, FileAccess packageAccess, Boolean streaming)
   at System.IO.Packaging.Package.Open(Stream stream, FileMode packageMode, FileAccess packageAccess)
   at OfficeOpenXml.ExcelPackage.ConstructNewFile(Stream stream, String password)
   --- End of inner exception stack trace ---
   at OfficeOpenXml.ExcelPackage.ConstructNewFile(Stream stream, String password)
   at OfficeOpenXml.ExcelPackage..ctor(FileInfo newFile)
   at Report.Form1.ExportToExcel1(DataTable Tbl, String ExcelFilePath) in C:\SMARTAG_PROJECT\SUREREACH\EXCEL\Report\Report\Form1.cs:line 39

Appreciated if anyone could advice/help on this. Thanks.

like image 303
chinna_82 Avatar asked Jan 03 '13 07:01

chinna_82


People also ask

Does EPPlus support XLS?

EPPlus does not work with the XLS format.

Does EPPlus require Excel?

NET Core from version 2.0. EPPlus has no dependencies to any other library such as Microsoft Excel. The library is designed with the developer in mind.

How do I save an Excel file as XLSX in C#?

To convert XLS to XLSX in C# - simply load an XLS Excel workbook with IronXL, and then save with the ". xlsx" file extension. Note that images, charts, split panes, freezing panes will not always be copied between XLS and XLSX formats.

What is Spire XLS?

Spire. XLS for . NET is a professional Excel . NET API that can be used to create, read, write, convert and print Excel files in any type of . NET ( C#, VB.NET, ASP.NET, .


2 Answers

Epplus does not handle .xls (BIFF8 format) files as far as i know.

It handles the newer .xlsx (Open Office Xml) format.

You can use excellibrary though as it works for xls files.

like image 100
scartag Avatar answered Oct 17 '22 23:10

scartag


In the date of this post EPPLUS (v4.4.1) seems to handle xls files just like it does with xlsx:

Here is an example:

  using (var target = new ExcelPackage(new System.IO.FileInfo("D:\\target.xls")))         {             target.Workbook.Worksheets.Add("worksheet");             target.Workbook.Worksheets.Last().Cells["A1:A12"].Value = "Hi";             target.Save();         } 

also tested your code:

FileInfo newFile = new FileInfo("C:\\Excel\\SampleStockTakeExceptionReport.xls"); ExcelPackage pck = new ExcelPackage(newFile); var ws = pck.Workbook.Worksheets.Add("Content"); ws.View.ShowGridLines = false; ws.Cells["J12"].Value = "Test Write"; pck.Save(); System.Diagnostics.Process.Start("C:\\Excel\\SampleStockTakeExceptionReport.xls"); 

and it works without any issues.

like image 43
Yahya Hussein Avatar answered Oct 18 '22 00:10

Yahya Hussein