内存高效的读取和写入.csv和.xlsx数据

我期待实现内存高效的读取和写入.xlsx / .csv文件。

我目前正在使用phpExcel,并遇到大型文件的麻烦。 (已知问题)

我find下面的库供阅读: https : //github.com/nuovo/spreadsheet-reader

这似乎是可行的。

为了写文件,我现在创build一个数组,然后循环数组来输出文件。 这就是为什么它使用大量的内存? 当使用代码点火器从mysql数据库获取数据时,编写csv / xlsx时不使用大量内存的最佳方法是什么?

这就是我现在所做的:

/* added for excel expert */ function excel_export() { $data = $this->Customer->get_all($this->Customer->count_all())->result_object(); $this->load->helper('report'); $rows = array(); $header_row = $this->_excel_get_header_row(); $header_row[] = lang('customers_customer_id'); $rows[] = $header_row; foreach ($data as $r) { $row = array( $r->first_name, $r->last_name, $r->email, $r->phone_number, $r->address_1, $r->address_2, $r->city, $r->state, $r->zip, $r->country, $r->comments, $r->account_number, $r->taxable ? 'y' : '', $r->company_name, $r->person_id ); $rows[] = $row; } $content = array_to_spreadsheet($rows); force_download('customers_export.'.($this->config->item('spreadsheet_format') == 'XLSX' ? 'xlsx' : 'csv'), $content); exit; } function array_to_spreadsheet($arr) { $CI =& get_instance(); $objPHPExcel = new PHPExcel(); //Default all cells to text $objPHPExcel->getDefaultStyle()->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT); for($k = 0;$k < count($arr);$k++) { for($j = 0;$j < count($arr[$k]); $j++) { $objPHPExcel->getActiveSheet()->setCellValueExplicitByColumnAndRow($j, $k+1, $arr[$k][$j]); } } if ($CI->config->item('spreadsheet_format') == 'XLSX') { $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); } else { $objWriter = new PHPExcel_Writer_CSV($objPHPExcel); } ob_start(); $objWriter->save('php://output'); $excelOutput = ob_get_clean(); return $excelOutput; }