PHP writes from database import to Excel

Source: Internet
Author: User

Is the principle of the same page principle! Select a certain amount of data and then become an array, then write directly to the file. Next continue to select the data that is not selected after the array, and then write the file! this resolves a memory overflow. But many CPUs still have a test! Because I just learned PHP (PHP training PHP Tutorial) Soon, the skill is not deep! You can only write such a thing!

Source!
Excel (Excel Training) class
PHP Code
Class excel{

var $header = "<?xml version=" 1.0 "encoding=" Utf-8 "?>
<workbook xmlns= "Urn:schemas-microsoft-com:office:spreadsheet"
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 = "</Workbook>";
var $lines = array ();
var $worksheet _title = "Table1";
function AddRow ($array) {
$cells = "";
foreach ($array as $k = = $v):
if (Is_numeric ($v)) {
if (substr ($v, 0, 1) = = 0) {
$cells. = "<cell><data ss:type=" String ">". $v. "</data></cell>n";
} else {
$cells. = "<cell><data ss:type=" number ">". $v. "</data></cell>n";
}
} else {
$cells. = "<cell><data ss:type=" String ">". $v. "</data></cell>n";
}
Endforeach;
$this->lines[] = "<row>n". $cells. "</row>n";
Unset ($arry);
}

function Setworksheettitle ($title) {

$title = Preg_replace ("/[\|:| /|?| *| [|]] /"," ", $title);

$title = substr ($title, 0, 31);

$this->worksheet_title = $title;

}


function Generatexml ($filename) {

       //Deliver header (as recommended in PHP manual)
   & nbsp;    header ("Content-type:application/vnd.ms-excel; Charset=utf-8 ");
        Header ("Content-disposition:inline; Filename= "". $filename. ". xls");

Print out document to the browser
Need to use stripslashes for the damn ">"
Echo stripslashes ($this->header);
echo "N<worksheet ss:name=" ". $this->worksheet_title. ">n<table>n";
echo "<column ss:index=" 1 "ss:autofitwidth=" 0 "ss:width=" "/>n";
echo Implode ("n", $this->lines);
echo "</table>n</worksheet>n";
Echo $this->footer;
Exit

}

function Write ($filename)//Focus
{

$content = stripslashes ($this->header);
$content. = "N<worksheet ss:name=" ". $this->worksheet_title. ">n<table>n";
$content. = "<column ss:index=" 1 "ss:autofitwidth=" 0 "ss:width=" "/>n";
$content. = Implode ("n", $this->lines);
$content. = "</table>n</worksheet>n";
$content. = $this->footer;//excel File
Error_log ($content, 3, $filename);
if (!file_exists ($filename))//Determine if there are no files
{
fopen ($filename, ' a ');
}
$fp = fopen ($filename, ' a ');
Fwrite ($fp, $content);//write file
Fclose ($FP);
unset ($this->lines);//emptying of in-memory data
}

}

Page
PHP Code
Include_once "./include/class.excel.php";//Call the Excel class
Require_once './include/class.zipfile.php ';//Call the Big Bag class
$xls = new excel;//instantiation
$w =explode ("Limit", $where _str);//Put Where
$p = 6000; Paging principle
$a = $ip _list_count/$p;//Paging principle
if ($ip _list_count% $p ==0)//Paging principle

else//Paging principle

for ($i =0; $i <= $a; $i + +)//Loop Write
{
$s =6000* $i;
$ip =$_sg[' db ']->fetch_all ("select * from Main_info". $w [0]. "Limit". $s. ",". $p);//Call yourself to write the database (database training database authentication) method, write out the array
$xls->addarray ($IP);//Call the AddArray method in the Excel class
xml1= $xls->write ("./". $i. ". XLS ");//Call the Write method in the Excel class
Unset ($IP);
Unset ($xml 1);
Sleep (1);
}

PHP writes from database import to Excel

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.