How to convert xls file to xlsx file using C#?
Asked Answered
L

4

14

I was developing an application which read data from an excel file, but when I try to open it, an exception  was thrown if the source file is saved with the xls format (File contains corrupted data error when opening Excel sheet with OpenXML). indeed when I save this file with the xlsx format it works fine. please help me to solve this problem.

Longheaded answered 25/10, 2017 at 16:28 Comment(1)
Can you post the relevant code here?Propjet
S
11

Use Free Spire.XLS dll available via NuGet.

Sample:

Workbook workbook = new Workbook();
workbook.LoadFromFile("Input.xls");
workbook.SaveToFile("Output.xlsx", ExcelVersion.Version2013);
Sweyn answered 10/4, 2018 at 6:19 Comment(6)
workbook object should be used not the class. LoadFromFile and SaveToFile are instance methods.Proxy
Spire.XLS is not free: e-iceblue.com/Introduce/…Proxy
@YawarMurtaza it's free man as far i know using visual studio 2017 maybe it is paid now. did you try it using VS2017?Sweyn
I have just found out that there is a free version available named FreeSpire.XLS nuget.org/packages/FreeSpire.XLS This version can convert XLS files upto Excel 2013 which is good enough since its open xml. There is however a paid version with the name you mentioned that has more functionalities than the free one obviously.Proxy
FreeSpire.XLS has a 200 row limit per sheet.Thoma
@Thoma try the paid version. tqSweyn
T
7

For reliably reading XLS files you could use ExcelDataReader which is a lightweight and fast library written in C# for reading Microsoft Excel files. It supports the import of Excel files all the way back to version 2.0 of Excel (released in 1987!)

Alternatively you could use a file conversion API like Zamzar. This service has been around for 10+ years, and provides a simple REST API for file conversion - it supports XLS to XLSX conversion. You can use it in C# and it has extra features like allowing you to import and export files to and from Amazon S3, FTP servers etc.

Full disclosure: I'm the lead developer for the Zamzar API.

Tonettetoney answered 26/10, 2017 at 20:52 Comment(0)
S
2

You cannot read xls files with OpenXML.

The solution from Microsoft is to read the xls file with Office Interop (but Interop is not recommended to be used on the server), transfer data from Interop step by step to OpenXML.

Another solution is to use an Excel library like EasyXLS and convert between these two Excel file formats:

ExcelDocument workbook = new ExcelDocument();
workbook.easy_LoadXLSFile("Excel.xls");
workbook.easy_WriteXLSXFile("Excel.xlsx");

Find more information about converting xls to xlsx.

I am not quite sure why you need to convert the file and why you don't just read the xls file, using a different technology then OpenXML, for sure.

Sire answered 26/10, 2017 at 8:28 Comment(2)
thank you Mr alex for your reply I have fixed the error with spire.xls ddl availbe in VSLongheaded
There are different reasons. My reason for being here is i allow a user to upload data as CSV, XLS, XLSX and so need to convert the xls to xlsx.Cyprinodont
M
1

XLS is the older Excel file format. XSLX is the newer format stored as OpenXML. XSLX is actually a zip file with the various components stored as files within it. You cannot simply rename the file to get it into the new format. To save the file in XSLX you'll have to save the file into the Excel 2010+ format.

If you're using Excel interop then it is an option on the SaveAs method.

for more info check the function: _Workbook.SaveAs Method

and the property: FileFormat:

Optional Object.
The file format to use when you save the file. For a list of valid choices, 
see the FileFormat property. For an existing file, the default format is the 
last file format specified; for a new file, the default is the format of the 
version of Excel being used.

msdn info here: https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel._workbook.saveas(v=office.11).aspx

Miter answered 25/10, 2017 at 16:30 Comment(1)
thank you for you response but I think that the solution that you proposed to me is not working because I can't create the woorkbookpart since I have not opened the spreadsheet. the program stoped at this part when I try to open the spreadsheet using the directory of the file name SpreadsheetDocument doc = SpreadsheetDocument.Open(filename, false).Longheaded

© 2022 - 2024 — McMap. All rights reserved.