1

I am generating a excel file using sql query and this code -

header("Content-type: application/octet-stream");

header("Content-Disposition: attachment; filename=excelfile.xls");

But when user generates the excel and downloads it and tries to open the file, they are getting this error -

"The file you are trying to open, 'filename.xls', is in a different format     
 than specified by the file extension. Verify that the file is not corrupted 
 and is from a trusted source before opening the file. Do you want to open 
 the file now?"

and on clicking on Yes the file is opening without any problem.

How can I get rid of the problem without bothering the user ?

I have read other threads in this forum but they are unclear or do not seem to work.

Like changing extension to xlsx does not work

Can anyone help ?

You can download sample of excel generated file from here

  • If you open the file in a text editor, you might have some idea of the format it is in. Excel 2007 files will start with the characters `PK`, Excel 97-2003 files will start with something like `ÐÏ`. CSV files will look like plain text delimited by commas. – Phylogenesis Mar 24 '15 at 18:31
  • Phylogenesis, You can download file from [here](http://tcpdemo.me.ht/show.php?opcode=CANCELLBT+Y). It is not showing any characters as you have mentioned –  Mar 25 '15 at 04:33
  • This file is in tab-delimited format. Unfortunately the only extension that automatically parses these files correctly in Excel (without causing the warning mentioned in your question) is `.txt`. Such files will default to being opened in Notepad (or other plain text editor). – Phylogenesis Mar 25 '15 at 09:09
  • Thanks, Phylogenesis, In the answer, kevinsky has provided a code for writing excel in xml style. Will look into that –  Mar 25 '15 at 19:25

2 Answers2

0

By any chance are you generating a file with comma delimiters? Such as "A","B","C".... I got this same error message when you write what is really a .csv file and call it an xls file.

Excel can open these files but the end user will have to do a "Save As" to true Excel format.

If you don't want the error then you have to use a more sophisticated library that will write the excel header files. See here for more information

Edit: I looked at your file. Excel is correct, this is not an excel file. As others have pointed out writing tab separated data and calling it an .xls does not change the omission of header files that really make it an excel file.

This post shows you how to write an excel file in xml style format. You can see from the code that there are quite a few extra things to do. You will have to modify your pl/sql code accordingly.

Community
  • 1
  • 1
kevinskio
  • 4,431
  • 1
  • 22
  • 36
  • kevinsky, You can download file from [here](http://tcpdemo.me.ht/show.php?opcode=CANCELLBT+Y). It is a proper excel file and not a comma delimited file –  Mar 25 '15 at 04:36
  • kevinsky, Thanks for giving post regarding writing excel file in xml style. Will surely look into it –  Mar 25 '15 at 19:24
0

I was having that problem while writing to a .csv file using PL/SQL until I enclosed each field in my header file by " like this:

header_line := '"Last Name"' || ',' || '"First Name"' || ',' || '"Date'"; UTL_FILE.PUT_LINE(outhandle, header_line);

June
  • 1