24

I am currently trying to figure out the best way to create a .xls file from PHP. It seems like to me that all I need to do is change the header content type to "application/vnd.ms-excel", and then output data in a comma separated format. Is this the best way to do this?

Thanks for any help!
Metropolis

EDIT

Would it be bad to just output the data as HTML? I already have a class that I would like to extend for this problem, so I would prefer to stay away from a third party solution. I just need to know what the best way to output the data would be in order to get the most out of that file format.

After looking at PHPExcel it seems to be the best solution for this issue. I appreciate all of your answers and I would have given three correct answers if I could lol. But up votes will have to do :)

Metropolis
  • 6,542
  • 19
  • 56
  • 86
  • 1
    When changing header content type to "application/vnd.ms-excel" and then writing an XLS file with either HTML, TAB or CSV formatted text content will work. However this is a sort of a trick because MS Excel will be able to open the file even though it's not a real binary XLS file, but you'll not be able to use any Excel features (like multiple sheets). One approach I used is MS Excel application itself, see [here](http://php.net/manual/en/class.dotnet.php). Another approach I used is [this one](http://www.gemboxsoftware.com/support-center/kb/articles/44-how-to-use-gembox-spreadsheet-in-php). – Mario Z Feb 18 '16 at 10:54

9 Answers9

19

its pretty easy, because excel supports html code. So you just need to add a header in the top of the file.. and then echo your html code!

header ( "Content-type: application/vnd.ms-excel" );
header ( "Content-Disposition: attachment; filename=foo_bar.xls" );
echo "<table><tr><th>Header1</th></tr><tr><td>value1</td></tr></table>";

this will generate an excel file!

Pedro Gouveia
  • 323
  • 2
  • 4
  • And how to generate multiple sheets ? simply close and re-open `` ?
    – Meloman Oct 03 '17 at 13:53
  • 5
    **That's not how this works**. That's not how any of this works. -- Just slapping **fake** MIME headers onto HTML output is not the same as an Excel file. Opening a HTML excerpt with an incorrect file extension will merely trigger a best-effort import function after a couple of Excel warnings. Hardly reliable. – mario Dec 17 '17 at 00:13
  • 1
    hey @mario, but i have test created excel from php use most framework, and return is (example.xls) element table (same from html), why not for this method if this work 100%? – Abdul Aziz Al Basyir Jan 08 '18 at 07:51
18

Depends if you want a CSV file or an XLS file. An XLS file can include formatting information for the cells, as well as row/column locking, protections and other features that are impossible in a CSV file. Also, keep in mind that Excel does not correctly support UTF-8 encoded content when opening CSV files.

If you want a formatted XLS file, then you need a library such as PhpSpreadsheet that can write a file with that formatting, or COM if you're server is running on Windows with Excel installed

Ali Noureddine
  • 324
  • 5
  • 20
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
3

You could use one of the XML formats that Microsoft Office understands.

See Microsoft's OpenXML Developer site for details/specs.

There is a library on called PHPExcel that you can look at to help you with this.

Of course, this all depends on what you mean by:

"get the most out of that file format"

If you just have simple tables with no formatting, CSV files may be all you need; however, if you want to use more features of a spreadsheet, I would recommend taking a look at OpenXML.

Jeff Winkworth
  • 4,874
  • 6
  • 34
  • 33
Robert Groves
  • 7,574
  • 6
  • 38
  • 50
2

You can just output html and newer versions of Excel will read it. I don't know how much formatting you can do with it though.

If you need data typing, rich formatting or formulas, I have had success with PHPExcel.

My preference is to write out CSV files. CSV is a very easy format to write and an easy conversion from existing html table scripts. It also has the advantage of being readable by a wide variety of non-microsoft spreadsheet programs. If you can make CSV the file format of choice for your web application, you will reap rewards when you have to accept a spreadsheet as input. It is much, much easier to read and parse a CSV file than an Excel file.

Jeff Winkworth
  • 4,874
  • 6
  • 34
  • 33
Scott Saunders
  • 29,840
  • 14
  • 57
  • 64
  • 1
    I agree with you about CSV files Scott. If it was up to me that is the format I would be using. But unfortunately I need to use XLS.... – Metropolis Jun 28 '10 at 15:33
1

Disclaimer: I work at Expected Behavior, the company that developed DocRaptor.

That said, you can use DocRaptor to generate XLS files from HTML. DocRaptor is a Ruby on Rails project, but you can use it with PHP. Here's our PHP example:

DocRaptor PHP example

Here's another link to DocRaptor's home page:

HTML to Excel with DocRaptor

illbzo1
  • 480
  • 3
  • 13
1

Try the XLS file wrapper, ala this post on SO.

Community
  • 1
  • 1
cam8001
  • 1,581
  • 1
  • 11
  • 22
1

Take a look at this PEAR package: Spreadsheet Excel Writer

Davide Gualano
  • 12,813
  • 9
  • 44
  • 65
-1

You can start using this files. It will create a excel file and help you download it.

You can add rows like this in the

01-simple-download-xlsx.php

$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'Roll')
            ->setCellValue('B1', 'Name')
            ->setCellValue('C1', 'Total Classes')
            ->setCellValue('D1', 'Class Attended')
        ->setCellValue('E1', 'Percentage');

You can print data from a database like this using a while loop.

$i=2;   
while ($row = $database->fetch_array($result)) 
 { 

       $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A'.$i, $row[0])
            ->setCellValue('B'.$i, $row[1])
            ->setCellValue('C'.$i, $row[2])
            ->setCellValue('D'.$i, $total)
        ->setCellValue('E'.$i, round(($row[2]/$total)*100));
          $i++;
 }
Raj Nandan Sharma
  • 3,694
  • 3
  • 32
  • 42
-1

Yes, that is one possible way - if you need a plain CSV file that will open in Ms Excel.

If you need a rich XLS file, there are many options including using third party DLLs such as 'CarlosAg.ExcelXmlWriter.dll' to create the XLS file. You can search for samples of using this dll on the net or Look here

Julius A
  • 38,062
  • 26
  • 74
  • 96
  • what about linux, external libraries are not the way to go, you dont know for how long they will be maintained nor what's inside – user7082181 Feb 19 '20 at 12:09