Metropolis
Metropolis

Reputation: 6622

What is the best way to create XLS file in PHP

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 :)

Upvotes: 24

Views: 76181

Answers (9)

Raj Nandan Sharma
Raj Nandan Sharma

Reputation: 3862

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++;
 }

Upvotes: -1

Mark Baker
Mark Baker

Reputation: 212412

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

Upvotes: 18

Scott Saunders
Scott Saunders

Reputation: 30394

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.

Upvotes: 2

Robert Groves
Robert Groves

Reputation: 7738

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.

Upvotes: 3

Pedro Gouveia
Pedro Gouveia

Reputation: 321

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!

Upvotes: 18

illbzo1
illbzo1

Reputation: 470

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

Upvotes: 1

Julius A
Julius A

Reputation: 39612

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

Upvotes: -1

Davide Gualano
Davide Gualano

Reputation: 13003

Take a look at this PEAR package: Spreadsheet Excel Writer

Upvotes: 1

cam8001
cam8001

Reputation: 1641

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

Upvotes: 1

Related Questions