Solved

Generating Excel in php

Posted on 2016-09-29
5
48 Views
Last Modified: 2016-10-04
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
Comment
Question by:Richard Korts
  • 2
  • 2
5 Comments
 
LVL 2

Accepted Solution

by:
manuverhaegen earned 250 total points
ID: 41822383
<?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
 
LVL 34

Assisted Solution

by:gr8gonzo
gr8gonzo earned 250 total points
ID: 41822402
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
 

Author Comment

by:Richard Korts
ID: 41822608
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
 

Author Comment

by:Richard Korts
ID: 41823775
manuverhaegen

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

Thanks
0
 
LVL 2

Expert Comment

by:manuverhaegen
ID: 41828630
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now