What is the best way to create XLS file in PHP

大憨熊 提交于 2019-11-27 21:14:06
Mark Baker

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

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!

Robert Groves

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.

Scott Saunders

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.

cam8001

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

Take a look at this PEAR package: Spreadsheet Excel Writer

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

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

You can start using this files. It will create a excel file and help you download it. Download Souce code or live demo here : http://purpledesign.in/blog/to-generate-excel-using-php/

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++;
 }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!