ASP NET generated excel file has different format than extension

0 votes

I have an outdated Asp.net 2.0 web application. I need to create an excel file with a specified table format (the best way if I can load the template and only put data in code behind). I'm trying to utilize this library to accomplish this. CarlosAg.ExcelXmlWriter: 

http://www.carlosag.net/tools/excelxmlwriter/sample

The final file is generated flawlessly, but when I try to access it in MS Excel, a warning window with the message that the file's format is different from the extension appears (xls). I also wonder if I should open it if I don't trust the file's source. The file opened successfully when I selected yes. The same warning—that the file is damaged and cannot be opened—appears on a separate computer running a different version of Excel (no possibility to open). What's remarkable is that I can access that file correctly and without receiving any errors in other office programs, including WPS Office.

    Dim book As Workbook = New Workbook()
    Dim sheet As Worksheet = book.Worksheets.Add("Sample")
    Dim row As WorksheetRow = sheet.Table.Rows.Add()
    row.Cells.Add("Hello World")
    book.Save("D:\Folder\test.xls")
Jan 5, 2023 in Others by Kithuzzz
• 38,000 points
572 views

1 answer to this question.

0 votes
You must include the xlsx file extension.

The legacy (closed) binary format of office files is actually the older Xls file type.

But starting with office 2007, Microsoft switched to using the open XML format for ALL of its office documents (well, ok, outlook pst, and MS Access are exceptions). Try renaming the xlsx document as a zip file if you have any office documents (word, power-point, excel, etc.). You discover that you may now open the zip archive and see, examine, and even edit the XML contained therein.

Therefore, more recent office file formats are actually XML markups, and you can edit them!

In any case, the library you're using unquestionably takes use of the fact that office files are now XML-formatted rather than being stored in a proprietary Microsoft binary format. As a result, these files can now be modified on ANY computer that can handle zip files and text files.

As a result, Excel is alerted to the fact that you are naming the document with a legacy binary file extension (Xls) while opening a plain text XML file. Therefore, you must add the open office XML format extension to that document (xlsx).
answered Jan 5, 2023 by narikkadan
• 63,600 points

Related Questions In Others

0 votes
1 answer

Excel cannot open the file because the file format or file extension is not valid - PHP Maatwebsite

Try this: if (ob_get_length() == 0 ) { ...READ MORE

answered Dec 19, 2022 in Others by narikkadan
• 63,600 points
1,449 views
0 votes
1 answer

Blazor - ClosedXml - excel cannot open the file beacause the file format or extension is not valid

Changed the following code: function BlazorDownloadFile(filename, bytesBase64) { var ...READ MORE

answered Jan 20, 2023 in Others by narikkadan
• 63,600 points
1,414 views
0 votes
1 answer

How to create and download excel document using asp.net

First, download the Open XML Format SDK 2.0. It comes ...READ MORE

answered Oct 3, 2022 in Others by narikkadan
• 63,600 points
2,943 views
0 votes
1 answer

How can I convert excel to PDF by Libreoffice and keep all format from excel file?

"Times New Roman" typeface does not have ...READ MORE

answered Oct 3, 2022 in Others by narikkadan
• 63,600 points
1,648 views
0 votes
1 answer

'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine

In order to fix this problem, you ...READ MORE

answered Feb 10, 2022 in Others by Soham
• 9,710 points
1,724 views
0 votes
1 answer

Deleting duplicate rows in Excel using Epplus

You need to re-think this… the while ...READ MORE

answered Feb 23, 2022 in Database by gaurav
• 23,260 points
1,470 views
0 votes
1 answer

Export DataTable to Excel File

Add Interop References. First we need to ...READ MORE

answered Jun 9, 2022 in JQuery by gaurav
• 23,260 points
773 views
0 votes
1 answer

Program that works in all version of excel

Knowing that all of your users will ...READ MORE

answered Sep 24, 2022 in Others by narikkadan
• 63,600 points
623 views
0 votes
1 answer

How to download SQL data in excel format using asp.net

Solution Download Open XML Download Close XML Library. import this Namespaces using System.IO; using System.Data; using ...READ MORE

answered Nov 14, 2022 in Others by narikkadan
• 63,600 points
1,067 views
0 votes
1 answer

Prompt message if Excel column has some value in asp.net c#

I believe it is wise to use ...READ MORE

answered Nov 24, 2022 in Others by narikkadan
• 63,600 points
653 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP