PHP Export Excel Class

Source: Internet
Author: User

/**

* Excel Export Class

*

* How to use

$excel =new Excel ();

*//Set Code:

* $excel->setencode ("Utf-8", "gb2312"); If you do not transcode, the parameters can be written, such as $excel->setencode ("Utf-8", "utf-8");

*//Set title bar

* $titlearr =array ("A", "B", "C", "D");

*//Set Content Bar

* $contentarr =array (

* 1=>array ("ab", "AC", "ad", "AE"),

* 2=>array ("abc", "ACC", "ADC", "AEC"),

* 3=>array ("abd", "ACD", "Add", "AED"),

* 4=>array ("Abe", "Ace", "Ade", "AEE"),

* );

* $excel->getexcel ($titlearr, $contentarr, "abc");

*/

Class Excel {

var $inEncode; Page encoding is generally


var $outEncode; Encoding of Excel files is generally


Public Function __construct () {


}

/**

* Set the encoding

*/

Public Function Setencode ($incode, $outcode) {

$this->inencode= $incode;


$this->outencode= $outcode;

}

/**

* Set the title bar of Excel

*/

Public Function Settitle ($titlearr) {

$title = "";

foreach ($titlearr as $v) {

if ($this->inencode!= $this->outencode) {

$title. =iconv ($this->inencode, $this->outencode, $v). " \ t ";

}

else{

$title. = $v. " \ t ";

}

}

$title. = "\ n";

return $title;

}

/**

* Set Excel Content

*/

Public Function Setrow ($array) {

$content = "";

foreach ($array as $k = = $v) {

foreach ($v as $vs) {

if ($this->inencode!= $this->outencode) {

$content. =iconv ($this->inencode, $this->outencode, $vs). " \ t ";

}

else{

$content. = $vs. "\ t";

}

}

$content. = "\ n";

}

return $content;

}

/**

* Generate and download Excel automatically

* $titlearr title bar Array

* $array Content Array

* $filename file name (empty, the current date is the name)

*/

Public Function Getexcel ($titlearr, $array, $filename = ") {

if ($filename = = ") {

$filename =date ("y-m-d");

}

$title = $this->settitle ($titlearr);

$content = $this->setrow ($array);

Header ("Content-type:application/vnd.ms-excel");

Header ("Content-disposition:filename=". $filename. ". XLS ");

echo $title;

Echo $content;

}

}


PHP Export Excel Class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.