Epplus不读取excel文件

问题描述:

下面是我读取excel文件的代码。Epplus不读取excel文件

代码。

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"); 

当我运行代码时,它会引发运行时错误。

错误

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 

感激,如果任何人都可以在这个意见/帮助。谢谢。

就我所知,Epplus不处理.xls(BIFF8格式)文件。

它处理较新的.xlsx(Open Office Xml)格式。

您可以使用excellibrary,因为它适用于xls文件。

+0

谢谢@scarttag .. – Rakeshyadvanshi

在这篇文章中EPPLUS(v4.4.1)的日期似乎处理的XLS文件,就像它与XLSX作用:

下面是一个例子:

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(); 
     } 

也测试代码:

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"); 

它的工作原理没有任何问题。