2013-01-03 12 views
18

एक्सेल फ़ाइल पढ़ने के लिए मेरा कोड नीचे है।एपप्लस एक्सेल फ़ाइल नहीं पढ़ रहा

कोड।

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 

सराहना करता है, तो किसी को भी इस पर/मदद सलाह सकता है। धन्यवाद।

उत्तर

30

एपप्लस जहां तक ​​मुझे पता है .xls (BIFF8 प्रारूप) फ़ाइलों को संभाल नहीं करता है।

यह नए .xlsx (ओपन ऑफिस एक्सएमएल) प्रारूप को संभालता है।

आप excellibrary का उपयोग कर सकते हैं हालांकि यह xls फ़ाइलों के लिए काम करता है।

+0

धन्यवाद @scarttag .. – Rakeshyadvanshi

1

इस पोस्ट 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"); 

और यह बिना किसी समस्या के काम करता है।

संबंधित मुद्दे