2013-01-03 51 views
18

Dưới đây là mã của tôi để đọc tệp excel.Epplus không đọc tệp excel

Mã.

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

Khi tôi chạy mã, nó sẽ phát ra lỗi thời gian chạy.

Lỗi

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 

đánh giá cao nếu có ai có thể tư vấn/giúp đỡ về vấn đề này. Cảm ơn.

Trả lời

30

Epplus không xử lý các tệp .xls (định dạng BIFF8) theo như tôi biết.

Nó xử lý định dạng .xlsx (Open Office Xml) mới hơn.

Bạn có thể sử dụng excellibrary mặc dù vì nó hoạt động cho các tệp xls.

+0

cảm ơn @scarttag .. – Rakeshyadvanshi

1

Trong ngày bài này EPPLUS (v4.4.1) dường như để xử lý các file xls giống như nó hiện với xlsx:

Dưới đây là một ví dụ:

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

cũng đã thử nghiệm mã của bạn:

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

và nó hoạt động mà không có bất kỳ vấn đề gì.

Các vấn đề liên quan