Solved

Generating Excel in php

Posted on 2016-09-29
5
35 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to count occurrences of each item in an array.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

760 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

23 Experts available now in Live!

Get 1:1 Help Now