Home  >  Article  >  Backend Development  >  Implementation method of exporting xls using Simple Excel_PHP tutorial

Implementation method of exporting xls using Simple Excel_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:14:34935browse

Because I wrote an article a few days ago, I used the php-excel-reader class to import excel content. By the way, I want to talk about some excel export issues. I use simple excel, a very simple export xls class, which is very easy to use!
The simple excel source code is as follows:

Copy code The code is as follows:

/**
* Simple excel generating from PHP5
*
* @package Utilities
* @license http://www.opensource.org/licenses/mit-license.php
* @author Oliver Schwarz
* @version 1.0
*/
class Excel_Xml
{
private $header = "n";
private $footer = "
";
private $lines = array();
private $sEncoding;
private $bConvertTypes;
private $sWorksheetTitle;
public function __construct($sEncoding = 'UTF-8', $bConvertTypes = false, $sWorksheetTitle = 'Table1')
{
$ this->bConvertTypes = $bConvertTypes;
$this->setEncoding($sEncoding);
$this->setWorksheetTitle($sWorksheetTitle);
}
public function setEncoding($sEncoding )
{
$this->sEncoding = $sEncoding;
}
public function setWorksheetTitle ($title)
{
$title = preg_replace ("/[\|: |/|?|*|[|]]/", "", $title);
$title = substr ($title, 0, 31);
$this->sWorksheetTitle = $title;
}
private function addRow ($array)
{
$cells = "";
foreach ($array as $k => $v):
$type = 'String';
if ($this->bConvertTypes === true && is_numeric($v)):
$type = 'Number';
endif;
$v = htmlentities( $v, ENT_COMPAT, $this->sEncoding);
$cells .= "" . $v . "< ;/Cell>n";
endforeach;
$this->lines[] = "n" . $cells . "n";
}
public function addArray ($array)
{
foreach ($array as $k => $v)
$this->addRow ($v);
}
public function generateXML ($filename = 'excel-export')
{
$filename = preg_replace('/[^aA-zZ0-9_-]/', '', $filename);
header( "Content-Type: application/vnd.ms-excel; charset=" . $this->sEncoding);
header("Content-Disposition: inline; filename="" . $filename . ".xls"" );
echo stripslashes (sprintf($this->header, $this->sEncoding));
echo "nsWorksheetTitle . "" >nn";
foreach ($this->lines as $line)
echo $line;
echo "
n
n";
echo $this->footer;
}
}
?>

The example of using php is as follows:
Copy the code The code is as follows:

/**
* @author mckee
* @blog www.phpddt.com
*/
require_once 'excel.class.php';
$xls = new Excel_Xml('UTF-8',false,'test');
$data = array(
1 => array('name','address'),
2 = > array('php diandiantong','www.phpddt.com'),
3 => array('Baidu','www.baidu.com')
);
$xls ->addArray($data);
$xls->generateXML('name4test');

?>

The export result is as shown below:
用Simple Excel导出xls

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326247.htmlTechArticleBecause I wrote an article a few days ago, using the php-excel-reader class to import excel content, by the way, I will talk about excel For the export problem, I use simple excel, a very simple export xls class, which is very easy to use! s...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn