Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Generating Excel in php

Posted on 2016-09-29
5
Medium Priority
?
77 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 2

Accepted Solution

by:
manuverhaegen earned 1000 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 35

Assisted Solution

by:gr8gonzo
gr8gonzo earned 1000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
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…
Suggested Courses

636 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