PHP Generate Excel File _php tutorial

Source: Internet
Author: User
PHP generates Excel files This article collects three PHP generated Excel file code program, the first is a more comprehensive generation function, the following two is very simple, but not as good as the first, OK now look at the principle of generating Excel.

PHP tutorials Generate Excel files
This article collects three PHP generated Excel file code program, the first is a more comprehensive generation function, the following two is very simple, but not as good as the first, OK now look at the principle of generating Excel.
*/

Class excel{


var $header = "

xmlns:x= "Urn:schemas-microsoft-com:office:excel"
Xmlns:ss= "Urn:schemas-microsoft-com:office:spreadsheet"
Xmlns:html= "HTTP://WWW.W3.ORG/TR/REC-HTML40" > ";

var $footer = "";
var $lines = array ();
var $worksheet _title = "Table1";

function AddRow ($array) {


$cells = "";


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

Add a string and a number of judgments to avoid generating an Excel occurrence number with a string stored in the warning
if (Is_numeric ($v)) {
Prevent 0 loss After creating Excel with the first letter 0 o'clock
if (substr ($v, 0, 1) = = 0) {
$cells. = " " . $v. " n ";
} else {
$cells. = " " . $v. " n ";
}
} else {
$cells. = " " . $v. " n ";
}

Endforeach;

Transform $cells content into one row
$this->lines[] = " n ". $cells. " n ";

}

function AddArray ($array) {

Run through the array and add them into rows
foreach ($array as $k = = $v):
$this->addrow ($v);
Endforeach;

}

function Setworksheettitle ($title) {

Strip out special chars first
$title = Preg_replace ("/[\|:| /|?| *| [|]] /"," ", $title);

Now cut it to the allowed length
$title = substr ($title, 0, 31);

Set Title
$this->worksheet_title = $title;

}

function Generatexml ($filename) {


Header ("Content-type:application/vnd.ms-excel; Charset=utf-8 ");
Header ("Content-disposition:inline; Filename= "". $filename. ". xls" ");

echo Strips Tutorial Lashes ($this->header);
echo "n Worksheet_title. "" >n




n "; echo "n"; echo Implode ("n", $this->lines); echo "
Nn ";
Echo $this->footer;

}

}


/**
* Non-frame use method
*
* require_once (' excel.php ');
* $doc = Array (
* 0 = Array (' China ', ' Chinese ', ' Chinese People ', ' 123456 ');
* );
* $xls = new Excel;
* $xls->addarray ($doc);
* $xls->generatexml ("mytest");
*/

?>

Method Two
In fact, when doing real application, you can take the data from the database tutorial, and then follow the end of each column of data plus t, after each row of data to add N method echo out, at the beginning of PHP with the header ("content-type:application/ Vnd.ms-excel "), indicating that the output is an Excel file, with the header (" Content-disposition:filename=test.xls "), and that the output file name is Text.xls. This will be OK.

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

Header ("Content-disposition:filename=test.xls");

echo "Test1";

echo "Test2";

echo "Test1";

echo "Test2";

echo "Test1";

echo "Test2";

echo "Test1";

echo "Test2";

echo "Test1";

echo "Test2";

echo "Test1";

echo "Test2";

?>

Method Three

Header ("Content-type:application/octet-stream");
Header ("Accept-ranges:bytes");
Header ("Content-type:application/vnd.ms-excel");
Header ("Content-disposition:attachment;filename=export_excel_gshjsl.xls");

$tx = ' table header ';
echo $tx. " nn ";
echo "Number". " T ";
echo "Name". " T ";
echo "n";

echo "=" 411481198507150666 "". " T ";
echo "=" 0123456 "". " T ";
echo "n";
?>

http://www.bkjia.com/PHPjc/630801.html www.bkjia.com true http://www.bkjia.com/PHPjc/630801.html techarticle PHP generates Excel files This article collects three PHP generated Excel file code program, the first is a more comprehensive generation function, the following two is very simple, but not as good as the first, good now ...

  • 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.