Excel Spreadsheet File Handling

Hi everyone. I wrote a short C++ program which creates a .xls file and stores some stuff in the file. When I open the .xls file, Excel gives me this error: https://i.imgur.com/s0Y0jBn.png

Please don't suggest .xlsx extension. It doesn't work. And I don't want to use libraries to make it work. .csv works flawlessly but if I need to search the file, I need to use a lot of file.ignore() statements and that's confusing. I just need to make the .xls file work without the warning error popping up every time I open the file.
Did you write the data in the XLS file format? Putting .xls at the end of the filename is not enough.

http://en.wikipedia.org/wiki/Microsoft_Excel_file_format#File_formats
Last edited on
Please don't suggest .xlsx extension.

Why not? Do you need to support old versions of Excel??

The .xls BIFF file format (Binary Interchange File Format) is proprietary, so while infomation on it is available it's not released officially by Microsoft (but the OpenOffice guys have the details here if you're esp. keen:
https://www.openoffice.org/sc/excelfileformat.pdf )


Edit: actually, Microsoft have made the format specification available:

Information on the older .XLS BIFF file format (Binary Interchange File Format) can be found here:

[MS-XLS]: Excel Binary File Format (.xls) Structure
https://msdn.microsoft.com/en-us/library/office/cc313154%28v=office.12%29.aspx

And also here (provided by the OpenOffice guys);
https://www.openoffice.org/sc/excelfileformat.pdf )

Whereas the newer .XLSX files use the Office Open XML file format which, as the name says, is an open standard; so the format is officially published:

Standard ECMA-376
Office Open XML File Formats
http://www.ecma-international.org/publications/standards/Ecma-376.htm

plus Wikipedia.org page for completeness:
Office Open XML
http://en.wikipedia.org/wiki/Office_Open_XML

Do you need to support old versions of Excel??

Andy

PS A bit more info:

An .xlsx file is a structured .zip file containing xml and binary data.
http://en.wikipedia.org/wiki/Zip_%28file_format%29
http://en.wikipedia.org/wiki/XML

A .xls file is an OLE compound storage file
http://en.wikipedia.org/wiki/COM_Structured_Storage
Last edited on
Sorry for the late reply guys. I had no internet connection for last two days

@Peter

What do you mean by "write the data in the XLS file format"? If you're talking about using tabs and endlines to move between cells, then yes. And yes, I saved the file in .xls format. What else should I have done?

@Andy

I tried using .xlsx extension. The file created by my code isn't opening in Excel. This is the error: https://i.imgur.com/0SCe97D.png
I'm using .xls because it works perfectly except for that annoying warning message I have to see everytime I open the file.
Last edited on
It sounds like you don't fully understand the difference between a file extension and a file format.

The file extension is something you put at the end of the filename. On Windows, when double clicking on a file, it will choose what program to open the file with depending on the file extension. The program itself will often use the file extension to know what kind of file it is.

The file format describes how data is storing in the file. By using the .xls file extension you make Excel expect that the file is stored in the XLS format, but a text file with values separated by tabs and newlines is not how XLS files are stored so it gives you a warning. It is still able to show the file because Excel can open files stored in such format. I think if you use the file extension .txt instead it will not give you a warning.
Last edited on
@ OP: To add to what Peter87 said about the file format and extensions; the format that you are describing is called 'tab delimited'. If you change the extension of your file to '.tab' then Excel should be able to open it, you just won't be able to save any of Excel's more useful features to the file.
Last edited on
@Peter

Thanks for the explanation. But I know that much. I read somewhere that if I open csv file in notepad, I'd be able to see how Excel stored data in csv files and then use that format to save data in my code. When I tried that with xls files, notepad showed those weird ASCII characters you see when you open an incompatible file with notepad (like a picture or audio/video file). As you'd expect, I couldn't find out how to properly format my ofstream data. I know I could use external libraries to manipulate xls files but I don't want to make it that complex. That's why I was asking if I could somehow avoid that warning message. But it seems like that won't be the case anymore.

@Computergeek

Thanks for the suggestion. I didn't know about tab delimited format and .tab extension. Seems like I'm gonna be using this in my project and probably ditch csv and xls. Cheers!

I wrote code ages ago that successfully wrote files conforming to the .xlsx file format. This is a compound file in .zip file format consisting of folders and .xml files. So, if you can create a .zip archive file with sub-folders and .xml files then as long as you conform to the required files structure you will be able to create an .xlsx file with c++
Topic archived. No new replies allowed.