简体   繁体   中英

How will I open xlsx File in C#.NET?

I want to open xlsx File in C#.NET, but it shows error .

But if the file is xls extension then easily i can open it but when file format is xlsx then shows error.

My code is

oXL.Workbooks.Open(Path, 0, false, 5, "", "", false,
               //Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0)

The exception is:

Excel cannot open the file 'New Microsoft Excel Worksheet.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.

Workbooks.Open will fail if you have a version of Excel in your machine that can't read xlsx files (eg Excel 2003).

If you don't need actually Office Interop (so if you just need to read and write files and not use Excel functionalities) then you should take a look at the Office Open XML SDK (v2.5 for .Net 4.5 and v2.0 for .Net 3.5) at the official download site .

You're then able to open an excel file like this:

SpreadsheetDocument ExcelDocument = SpreadsheetDocument.Open(FileName, false); 

and perform read and write operations.

To be clear: The Office Open XML SDK allows for read- and write operations on an Excel (and other Office XML) files, but if you require MS Excel do perform calculations or macros, then this will not solve your issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM