Home  >  Article  >  php教程  >  PHP read/export CSV file

PHP read/export CSV file

WBOY
WBOYOriginal
2016-08-04 08:56:181235browse

There are often import/export requirements at work. The following are commonly used methods.
To read a CSV file, you can read it in pages, just set the number of lines to read and the number of starting lines.
Export CSV files, implemented in two ways.

/**<br> * Read CSV file<br> * @param string $csv_file csv file path<br> * @param int $lines Read the number of lines<br> * @param int $offset Starting line number<br> * @return array|bool<br> ​*/<br> public function read_csv_lines($csv_file = '', $lines = 0, $offset = 0)<br> {<br>     if (!$fp = fopen($csv_file, 'r')) {<br>         return false;<br>     }<br>     $i = $j = 0;<br>     while (false !== ($line = fgets($fp))) {<br>         if ($i++ < $offset) {<br />             continue;<br />         }<br />         break;<br />     }<br />     $data = array();<br />     while (($j++ < $lines) && !feof($fp)) {<br />         $data[] = fgetcsv($fp);<br />     }<br />     fclose($fp);<br />     return $data;<br /> }<br /> <br /> <br /> /**<br /> * Export CSV file<br /> * @param array $data Data<br /> * @param array $header_data first row of data<br /> * @param string $file_name File name<br /> * @return string<br /> ​*/<br /> public function export_csv_1($data = [], $header_data = [], $file_name = '')<br /> {<br />     header('Content-Type: application/octet-stream');<br />     header('Content-Disposition: attachment; filename=' . $file_name);<br />     if (!empty($header_data)) {<br />         echo iconv('utf-8','gbk//TRANSLIT','"'.implode('","',$header_data).'"'."n");<br />     }<br />     foreach ($data as $key => $value) {<br>         $output = array();<br>         $output[] = $value['id'];<br>         $output[] = $value['name'];<br>         echo iconv('utf-8','gbk//TRANSLIT','"'.implode('","', $output).""n");<br>     }<br> }<br> <br> /**<br> * Export CSV file<br> * @param array $data Data<br> * @param array $header_data first row of data<br> * @param string $file_name File name<br> * @return string<br> ​*/<br> public function export_csv_2($data = [], $header_data = [], $file_name = '')<br> {<br>     header('Content-Type: application/vnd.ms-excel');<br>     header('Content-Disposition: attachment;filename='.$file_name);<br>     header('Cache-Control: max-age=0');<br>     $fp = fopen('php://output', 'a');<br>     if (!empty($header_data)) {<br>         foreach ($header_data as $key => $value) {<br>             $header_data[$key] = iconv('utf-8', 'gbk', $value);<br>         }<br>         fputcsv($fp, $header_data);<br>     }<br>     $num = 0;<br>     //每隔$limit行,刷新一下输出buffer,不要太大,也不要太小<br>$limit = 100000;<br> //Get data line by line without wasting memory<br> $count = count($data);<br> If ($count > 0) {<br> for ($i = 0; $i < $count; $i++) {<br />               $num++;<br /> ​​​​​​ //Refresh the output buffer to prevent problems caused by too much data<br />                if ($limit == $num) {<br />                                                              ob_flush();<br /> flush();<br />                   $num = 0;<br />             }<br />               $row = $data[$i];<br /> foreach ($row as $key => $value) {<br>                $row[$key] = iconv('utf-8', 'gbk', $value);<br>             }<br>                          fputcsv($fp, $row);<br>         }<br> }<br> ​​fclose($fp);<br> }For more [dry information sharing], please follow the PHP engineer subscription account.
PHP read/export CSV file

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