[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 81
  • Last Modified:

Generating Excel in php

I have been using this technique for years to generate Excel spreadsheets from a php program.

At the beginning of the php is this code:

<?php
header("Content-type: application/vnd.ms-excel; charset=UTF-8");
$fn = "excel" . date('Y-m-d_h_i') . ".xls";
header("Content-Disposition: attachment; filename=" . $fn);
?>

Open in new window


Then I build an old fashioned HTML table with rows & columns that become the rows & columns of the spreadsheet.

If I change the line: $fn = "excel" . date('Y-m-d_h_i') . ".xls"; to $fn = "excel" . date('Y-m-d_h_i') . ".xlsx";,
will that cause it to generate an xlsx file compatible with current Excel?

Or is there a better way?

Thanks
0
Richard Korts
Asked:
Richard Korts
  • 2
  • 2
2 Solutions
 
manuverhaegenSystem & Storage AdminCommented:
<?php
require_once 'Spreadsheet/Excel/Writer.php';
// Creating a workbook
$workbook = new Spreadsheet_Excel_Writer();
// sending HTTP headers
$workbook->send('test.xls');
// Creating a worksheet
$worksheet =& $workbook->addWorksheet('My worksheet');
// The actual data
$worksheet->write(0, 0, 'text col');
$worksheet->write(0, 1, 'text col');
$worksheet->write(1, 0, 'my tekst');
$worksheet->write(1, 1, 30);
// Let's send the file
$workbook->close();
?>
0
 
gr8gonzoConsultantCommented:
Technically, what you're doing now is you're relying on Microsoft's method of trying to automatically figure out what you want to do. An HTML table is not an .xls file and it would likely break with any Excel-compatible programs.

Manuverhaegen's method is generating a proper .xls file using a library that writes the proper output. Newer versions of Excel will be compatible with this output and should work fine.

An .xlsx file is actually a ZIP file that contains a bunch of XML files. It's very possible that Excel will still fail to open the file as .xlsx but then look at it and say, "This looks like an HTML table, so I'll import it that way."

Personally, if it's working fine for you today, I wouldn't change anything. Otherwise, if you want to actually generate true XLSX files, there's probably some libraries out there that can help (Google around for them or check phpclasses or the PEAR repositories). If you want to go it the long way, you could look at an existing XLSX file and look at the files within and create your own XLSX file manually, but that would be pretty time-consuming to do.
0
 
Richard KortsAuthor Commented:
I found this: https://phpexcel.codeplex.com/ and did a test, it works great & Excel opens the output perfectly. Excel 2016 on my Windows 10 machine to be exact, to be exact.

It looks similar to manuverhaegen's but I don't know where the class Spreadsheet/Excel/Writer.php comes from.

I think I will try phpExcel.

I failed to mention in my question, I really need to save it to a file on the server, not auto download. It appears phpExcel can do either.
0
 
Richard KortsAuthor Commented:
manuverhaegen

Can you point me to where I can get the classes you are using?

Thanks
0
 
manuverhaegenSystem & Storage AdminCommented:
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now