Question

SimpleCodes on Wed, 14 Jun 2017 15:17:01


Hi All,

I have a windows forms application which reads the excel from  and put it in  shared folder.

After this users can view all the file inside the shared folder and download the particular file.

But when I upload and download the xlsx file it working fine , but when I try to read or  write a .xls file.

Its throwing the below error.

I can see lot of posts in forum, but they are suggesting to use only .xlsx file , but I want to use .xls file .what needs to be done.

Is anybody faced this issue, please let me know what is the fix

thanks

An unhandled exception of type 'System.IO.FileFormatException' occurred in WindowsBase.dll

{"File contains corrupted data."

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.OpenOnFile(String path, FileMode mode, FileAccess access, FileShare share, Boolean streaming)
   at System.IO.Packaging.ZipPackage..ctor(String path, FileMode mode, FileAccess access, FileShare share, Boolean streaming)
   at System.IO.Packaging.Package.Open(String path, FileMode packageMode, FileAccess packageAccess, FileShare packageShare, Boolean streaming)
   at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.OpenCore(String path, Boolean readWriteMode)
   at DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open(String path, Boolean isEditable, OpenSettings openSettings)
   at DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open(String path, Boolean isEditable)
   at ReadExcel.ReadXl.ReadExcelSheet(String fname, Boolean firstRowIsHeade) in I:\TestApps\ReadExcel\ReadExcel\ReadXl.cs:line 72
   at ReadExcel.Program.Main(String[] args) in I:\TestApps\ReadExcel\ReadExcel\Program.cs:line 20
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

Sponsored



Replies

Brian Muth on Wed, 14 Jun 2017 15:24:18


*.xlsx files are actually XML files that have been zipped into the *.zip format. However, *.xls files are the earlier Excel data files that are stored in a format proprietary to Microsoft. You cannot use OpenXML to read them.

However, you can use the Excel XX.X Object Library to read *.xls files using COM.

Read Excel File in C#

How to read an Excel file using C#

Wendy Zang on Sun, 18 Jun 2017 13:03:16


Hi SimpleCodes,

Thank you for posting here.

For your question is more related to open XML, I will move it to  Open XML Format SDK forum for suitable support.

Thanks for your understanding and cooperation.

Best Regards,

Wendy

Chenchen Li on Mon, 19 Jun 2017 05:48:33


Hello,

Starting with the 2007 Microsoft Office system, Microsoft Office uses the XML-based file formats, such as .docx, .xlsx, and .pptx. For more information, please visit Open XML Formats and file name extensions

.xls (Excel file 2003 -2007) doesn't support Open XML format, so It is impossible to use Open XML library to open this kind of files. I suggest you convert it into .xlsx/.xlsm, then you could manipulate its content using Open XML. If you have large number of .xls documents, you could automate Excel application to save these document into .xlsx/.xlsm.

Please visit How to automate Microsoft Excel from Visual Basic & Workbook.SaveAs Method (Excel)

Regards,

Celeste