Solved

Generating Excel in php

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

786 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