PHP CSV Export

Source: Internet
Author: User

/**
* Download CSV
* @param unknown $orders _id
* @param unknown $orders _date_start
* @param unknown $orders _date_end
*/
Public Function Csvdown ($orders _id, $orders _date_start, $orders _date_end, $title = ' orderssales ') {

Output Excel file header, you can replace user.csv with the file name you want
Header (' Content-type:application/vnd.ms-excel ');
Header (' Content-disposition:attachment;filename= '. $title. '. CSV "');
Header (' cache-control:max-age=0 ');


Open PHP file handle, php://output direct output to browser
$fp = fopen (' php://output ', ' a ');

Generate a file on the server
/* $title = $title. ". csv";
$filePath = App_path. ' /runtime/temp/'. $title;
$fp = fopen ($filePath, ' a '); */

Output Excel Column name information
$head = Array (' 12 ', ' 34 ', ' Platform ', ' shop ', ' Order Date ', ' Customer ID ', ' Sales ', ' cost ', ' freight (including processing fee ') ', ' Fees & charges ', ' profit ', ' profit Margin ');
foreach ($head as $i = = $v) {
CSV Excel support GBK encoding, be sure to convert otherwise garbled
$head [$i] = iconv (' utf-8 ', ' GBK ', $v);
}

Writes data to a file handle via Fputcsv
Fputcsv ($fp, $head);

Download the content
$saleForms = new \erp\c\orders\saleforms ();

Get the total number of orders
$total = $saleForms->getorderssaletotal ($orders _id, $orders _date_start, $orders _date_end);

$num = 5000;
$count = Ceil ($total/$num);

Counter
$cnt = 0;
Every $limit line, refresh the output buffer, not too big, not too small
$limit = 5000;

for ($i =1; $i <= $count; $i + +) {
$start = ($i-1) * $NUM;
$returnInfo = $saleForms->orderssalerun ($orders _id, $orders _date_start, $orders _date_end, $start, $num);

foreach ($returnInfo [' Items '] as $row) {

$cnt + +;
if ($limit = = $cnt) {//Refresh output buffer to prevent problems caused by excessive data
Ob_flush ();
Flush ();
$cnt = 0;
}
CSV Excel support GBK encoding, be sure to convert otherwise garbled
$temp = Array ();
foreach ($row as $v) {
$temp [] = Iconv (' utf-8 ', ' GBK ', $v);
}
Fputcsv ($fp, $temp);
Unset ($temp);
}
};

Fclose ($FP);
}

If the data is very large

Can change the running memory and PHP request run time to resolve

PHP CSV Export

Related Article

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.