So, my current code works 100%, the file is made and it is opened in excel. But upon opening, there are a few errors one must click through in order to open the file. They a
The problem is ID
as the first two bytes of your file. This is the signature for a SYLK file, an old spreadsheet format used by Multiplan (Symbolic Link Interchange - SYLK - file format), a precursor to Microsoft Excel.
The easiest way to resolve this is to avoid ID
as your heading, and use Id
(IIRC, the SYLK signature is case-sensitive) or Key
or some other text value instead
You're saving your file with an xls
extension, but you're not actually using Excel format, simply a csv format; so MS Excel will complain that the format doesn't match the extension.... either change the extension to .csv
to match the actual format of the file you're creating; or create a real BIFF-format Excel file
Any help with formatting the spreadsheet, such as having the cells be big enough for the content would be great.
The format that you're using, csv (with a tab separator) doesn't support formatting of any kind, including column widths. If you want that, then you need to switch to a true spreadsheet format (by coincidence, SYLK is a true spreadsheet format, though it's pretty dated and I wouldn't particularly recommend it) such as BIFF for real xls files, OfficeOpenXML for xlsx files, OASIS for ods files.
Headers that I use for OfficeOpenXML xlsx files:
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="01simple.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0