Search code examples
phpformattingfile-typespreadsheetreadonly

PHP - Read-Only spreadsheet filetype?


I'm using a simple web-based PHP application that outputs a table as a spreadsheet

header("Content-Disposition: attachment; filename=" . $filename . ".xls");
header("Content-Type: application/vnd.ms-excel");

//inserts tab delimited text

But I'm finding the downloaded spreadsheet opens as a read-only file and must be saved locally and (in Excel on Windows) the type changed to XLS (from HTML). Is there a way to set the attribute of filetype correctly so that doing a simple save doesn't require correcting the filetype?

Is the file downloaded read-only by nature of security or is this not normal?

Also I don't like the automatic borders created when opening the spreadsheet in Excel or OpenOffice (on Linux). I would prefer to have no border formatting. Is there a way in the file to specify no added formatting or is this built into those applications?


Solution

  • I don't know which version of Excel you're talking about, so I'll suppose that you're using a 2007 or newer version.

    The extension change problem probably depends on an Office feature called "Extension Hardening"; as far as I know, the only solution is to generate a real Excel file (for example by using the PHPExcel set of classes), and not an HTML file.

    The downloaded files are read-only for security reasons, since they are being opened in the so called "Protected View":

    Files from the Internet and from other potentially unsafe locations can contain viruses, worms, or other kinds of malware, which can harm your computer. To help protect your computer, files from these potentially unsafe locations are opened in Protected View. By using Protected View, you can read a file and inspect its contents while reducing the risks that can occur.

    Finally, a word about borders and formatting: with the old Excel 2000 version, you could format the output by simply adding some XML tags in the header section of the HTML code; see the "Microsoft Office HTML and XML Reference" for further details and examples, but keep in mind that it's quite obsolete, so I don't think that this technique still works with the more recent Excel versions.

    If you want to have more control over the generated ouput, you should not use simple HTML for creating the spreadsheet file.

    On this post you can also find some alternatives to PHPExcel for writing Excel files.