What is the best way to create XLS file in PHP

后端 未结 9 1249
予麋鹿
予麋鹿 2020-12-05 14:03

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

相关标签:
9条回答
  • 2020-12-05 14:39

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

    0 讨论(0)
  • 2020-12-05 14:41

    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

    0 讨论(0)
  • 2020-12-05 14:46

    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

    0 讨论(0)
  • 2020-12-05 14:50

    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!

    0 讨论(0)
  • 2020-12-05 14:50

    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.

    0 讨论(0)
  • 2020-12-05 14:59

    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

    0 讨论(0)
提交回复
热议问题